xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/fixxfti.c (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- fixxfti.c - Implement __fixxfti -----------------------------------===//
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 __fixxfti for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "int_lib.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #ifdef CRT_HAS_128BIT
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 ti_int is a 128 bit integral type value in long double is representable
210b57cec5SDimitry Andric // in ti_int
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*5f757f3fSDimitry Andric COMPILER_RT_ABI ti_int __fixxfti(xf_float a) {
280b57cec5SDimitry Andric   const ti_int ti_max = (ti_int)((~(tu_int)0) / 2);
290b57cec5SDimitry Andric   const ti_int ti_min = -ti_max - 1;
30*5f757f3fSDimitry Andric   xf_bits fb;
310b57cec5SDimitry Andric   fb.f = a;
320b57cec5SDimitry Andric   int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
330b57cec5SDimitry Andric   if (e < 0)
340b57cec5SDimitry Andric     return 0;
350b57cec5SDimitry Andric   ti_int s = -(si_int)((fb.u.high.s.low & 0x00008000) >> 15);
360b57cec5SDimitry Andric   ti_int r = fb.u.low.all;
370b57cec5SDimitry Andric   if ((unsigned)e >= sizeof(ti_int) * CHAR_BIT)
380b57cec5SDimitry Andric     return a > 0 ? ti_max : ti_min;
390b57cec5SDimitry Andric   if (e > 63)
400b57cec5SDimitry Andric     r <<= (e - 63);
410b57cec5SDimitry Andric   else
420b57cec5SDimitry Andric     r >>= (63 - e);
430b57cec5SDimitry Andric   return (r ^ s) - s;
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric #endif // CRT_HAS_128BIT
47