1 //===--- Round floating point to nearest integer on x86-64 ------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H
10 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H
11 
12 #include "src/__support/architectures.h"
13 
14 #if !defined(LLVM_LIBC_ARCH_X86_64)
15 #error "Invalid include"
16 #endif
17 
18 #if !defined(__SSE4_2__)
19 #error "SSE4.2 instruction set is not supported"
20 #endif
21 
22 #include <immintrin.h>
23 
24 namespace __llvm_libc {
25 namespace fputil {
26 
27 static inline double nearest_integer(double x) {
28   __m128d xmm = _mm_set_sd(x); // NOLINT
29   __m128d ymm =
30       _mm_round_sd(xmm, xmm, _MM_ROUND_NEAREST | _MM_FROUND_NO_EXC); // NOLINT
31   return ymm[0];
32 }
33 
34 } // namespace fputil
35 } // namespace __llvm_libc
36 
37 #endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H
38