47 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H10#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H11 12#include "src/__support/common.h"13#include "src/__support/macros/config.h"14#include "src/__support/macros/properties/architectures.h"15 16#if !defined(LIBC_TARGET_ARCH_IS_X86_64)17#error "Invalid include"18#endif19 20#if !defined(__SSE4_2__)21#error "SSE4.2 instruction set is not supported"22#endif23 24#include <immintrin.h>25 26namespace LIBC_NAMESPACE_DECL {27namespace fputil {28 29LIBC_INLINE float nearest_integer(float x) {30 __m128 xmm = _mm_set_ss(x); // NOLINT31 __m128 ymm =32 _mm_round_ss(xmm, xmm, _MM_ROUND_NEAREST | _MM_FROUND_NO_EXC); // NOLINT33 return ymm[0];34}35 36LIBC_INLINE double nearest_integer(double x) {37 __m128d xmm = _mm_set_sd(x); // NOLINT38 __m128d ymm =39 _mm_round_sd(xmm, xmm, _MM_ROUND_NEAREST | _MM_FROUND_NO_EXC); // NOLINT40 return ymm[0];41}42 43} // namespace fputil44} // namespace LIBC_NAMESPACE_DECL45 46#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H47