xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/fixxfdi.c (revision 68d75eff68281c1b445e3010bb975eae07aac225)
10b57cec5SDimitry Andric //===-- fixxfdi.c - Implement __fixxfdi -----------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements __fixxfdi for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #if !_ARCH_PPC
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "int_lib.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric // Returns: convert a to a signed long long, rounding toward zero.
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric // Assumption: long double is an intel 80 bit floating point type padded with 6
200b57cec5SDimitry Andric // bytes di_int is a 64 bit integral type value in long double is representable
210b57cec5SDimitry Andric // in di_int (no range checking performed)
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric // gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee
240b57cec5SDimitry Andric // eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
250b57cec5SDimitry Andric // mmmm mmmm mmmm
260b57cec5SDimitry Andric 
27*68d75effSDimitry Andric #if defined(_MSC_VER) && !defined(__clang__)
28*68d75effSDimitry Andric // MSVC throws a warning about 'unitialized variable use' here,
29*68d75effSDimitry Andric // disable it for builds that warn-as-error
30*68d75effSDimitry Andric #pragma warning(push)
31*68d75effSDimitry Andric #pragma warning(disable : 4700)
32*68d75effSDimitry Andric #endif
33*68d75effSDimitry Andric 
340b57cec5SDimitry Andric COMPILER_RT_ABI di_int __fixxfdi(long double a) {
350b57cec5SDimitry Andric   const di_int di_max = (di_int)((~(du_int)0) / 2);
360b57cec5SDimitry Andric   const di_int di_min = -di_max - 1;
370b57cec5SDimitry Andric   long_double_bits fb;
380b57cec5SDimitry Andric   fb.f = a;
390b57cec5SDimitry Andric   int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
400b57cec5SDimitry Andric   if (e < 0)
410b57cec5SDimitry Andric     return 0;
420b57cec5SDimitry Andric   if ((unsigned)e >= sizeof(di_int) * CHAR_BIT)
430b57cec5SDimitry Andric     return a > 0 ? di_max : di_min;
440b57cec5SDimitry Andric   di_int s = -(si_int)((fb.u.high.s.low & 0x00008000) >> 15);
450b57cec5SDimitry Andric   di_int r = fb.u.low.all;
460b57cec5SDimitry Andric   r = (du_int)r >> (63 - e);
470b57cec5SDimitry Andric   return (r ^ s) - s;
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric 
50*68d75effSDimitry Andric #if defined(_MSC_VER) && !defined(__clang__)
51*68d75effSDimitry Andric #pragma warning(pop)
52*68d75effSDimitry Andric #endif
53*68d75effSDimitry Andric 
540b57cec5SDimitry Andric #endif // !_ARCH_PPC
55