xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/fixxfdi.c (revision ef84fd3bd8895f4e6be1e38baf19e6dc3255bc64)
1156cd587Sjoerg /* ===-- fixxfdi.c - Implement __fixxfdi -----------------------------------===
2156cd587Sjoerg  *
3156cd587Sjoerg  *      	       The LLVM Compiler Infrastructure
4156cd587Sjoerg  *
5156cd587Sjoerg  * This file is dual licensed under the MIT and the University of Illinois Open
6156cd587Sjoerg  * Source Licenses. See LICENSE.TXT for details.
7156cd587Sjoerg  *
8156cd587Sjoerg  * ===----------------------------------------------------------------------===
9156cd587Sjoerg  *
10156cd587Sjoerg  * This file implements __fixxfdi for the compiler_rt library.
11156cd587Sjoerg  *
12156cd587Sjoerg  * ===----------------------------------------------------------------------===
13156cd587Sjoerg  */
14156cd587Sjoerg 
15156cd587Sjoerg #if !_ARCH_PPC
16156cd587Sjoerg 
17156cd587Sjoerg #include "int_lib.h"
18156cd587Sjoerg 
19156cd587Sjoerg /* Returns: convert a to a signed long long, rounding toward zero. */
20156cd587Sjoerg 
21156cd587Sjoerg /* Assumption: long double is an intel 80 bit floating point type padded with 6 bytes
22*ef84fd3bSjoerg  *             di_int is a 64 bit integral type
23156cd587Sjoerg  *             value in long double is representable in di_int (no range checking performed)
24156cd587Sjoerg  */
25156cd587Sjoerg 
26156cd587Sjoerg /* gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
27156cd587Sjoerg  * 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
28156cd587Sjoerg  */
29156cd587Sjoerg 
30f7f78b33Sjoerg COMPILER_RT_ABI di_int
__fixxfdi(long double a)31156cd587Sjoerg __fixxfdi(long double a)
32156cd587Sjoerg {
33*ef84fd3bSjoerg     const di_int di_max = (di_int)((~(du_int)0) / 2);
34*ef84fd3bSjoerg     const di_int di_min = -di_max - 1;
35156cd587Sjoerg     long_double_bits fb;
36156cd587Sjoerg     fb.f = a;
37156cd587Sjoerg     int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
38156cd587Sjoerg     if (e < 0)
39156cd587Sjoerg         return 0;
40*ef84fd3bSjoerg     if ((unsigned)e >= sizeof(di_int) * CHAR_BIT)
41*ef84fd3bSjoerg         return a > 0 ? di_max : di_min;
42156cd587Sjoerg     di_int s = -(si_int)((fb.u.high.s.low & 0x00008000) >> 15);
43156cd587Sjoerg     di_int r = fb.u.low.all;
44156cd587Sjoerg     r = (du_int)r >> (63 - e);
45156cd587Sjoerg     return (r ^ s) - s;
46156cd587Sjoerg }
47156cd587Sjoerg 
48156cd587Sjoerg #endif /* !_ARCH_PPC */
49