1 //===--- Round floating point to nearest integer on x86-64 ------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H
10 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H
12 #include "src/__support/common.h"
13 #include "src/__support/macros/properties/architectures.h"
15 #if !defined(LIBC_TARGET_ARCH_IS_X86_64)
16 #error "Invalid include"
19 #if !defined(__SSE4_2__)
20 #error "SSE4.2 instruction set is not supported"
23 #include <immintrin.h>
25 namespace __llvm_libc
{
28 LIBC_INLINE
float nearest_integer(float x
) {
29 __m128 xmm
= _mm_set_ss(x
); // NOLINT
31 _mm_round_ss(xmm
, xmm
, _MM_ROUND_NEAREST
| _MM_FROUND_NO_EXC
); // NOLINT
35 LIBC_INLINE
double nearest_integer(double x
) {
36 __m128d xmm
= _mm_set_sd(x
); // NOLINT
38 _mm_round_sd(xmm
, xmm
, _MM_ROUND_NEAREST
| _MM_FROUND_NO_EXC
); // NOLINT
43 } // namespace __llvm_libc
45 #endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H