xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/fixxfdi.c (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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 
2768d75effSDimitry Andric #if defined(_MSC_VER) && !defined(__clang__)
28349cc55cSDimitry Andric // MSVC throws a warning about 'uninitialized variable use' here,
2968d75effSDimitry Andric // disable it for builds that warn-as-error
3068d75effSDimitry Andric #pragma warning(push)
3168d75effSDimitry Andric #pragma warning(disable : 4700)
3268d75effSDimitry Andric #endif
3368d75effSDimitry Andric 
34*5f757f3fSDimitry Andric COMPILER_RT_ABI di_int __fixxfdi(xf_float a) {
350b57cec5SDimitry Andric   const di_int di_max = (di_int)((~(du_int)0) / 2);
360b57cec5SDimitry Andric   const di_int di_min = -di_max - 1;
37*5f757f3fSDimitry Andric   xf_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 
5068d75effSDimitry Andric #if defined(_MSC_VER) && !defined(__clang__)
5168d75effSDimitry Andric #pragma warning(pop)
5268d75effSDimitry Andric #endif
5368d75effSDimitry Andric 
540b57cec5SDimitry Andric #endif // !_ARCH_PPC
55