xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/fixunsxfsi.c (revision ef84fd3bd8895f4e6be1e38baf19e6dc3255bc64)
1156cd587Sjoerg /* ===-- fixunsxfsi.c - Implement __fixunsxfsi -----------------------------===
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 __fixunsxfsi 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 unsigned int, rounding toward zero.
20156cd587Sjoerg  *          Negative values all become zero.
21156cd587Sjoerg  */
22156cd587Sjoerg 
23156cd587Sjoerg /* Assumption: long double is an intel 80 bit floating point type padded with 6 bytes
24156cd587Sjoerg  *             su_int is a 32 bit integral type
25156cd587Sjoerg  *             value in long double is representable in su_int or is negative
26156cd587Sjoerg  */
27156cd587Sjoerg 
28156cd587Sjoerg /* gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
29156cd587Sjoerg  * 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
30156cd587Sjoerg  */
31156cd587Sjoerg 
32f7f78b33Sjoerg COMPILER_RT_ABI su_int
__fixunsxfsi(long double a)33156cd587Sjoerg __fixunsxfsi(long double a)
34156cd587Sjoerg {
35156cd587Sjoerg     long_double_bits fb;
36156cd587Sjoerg     fb.f = a;
37156cd587Sjoerg     int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
38156cd587Sjoerg     if (e < 0 || (fb.u.high.s.low & 0x00008000))
39156cd587Sjoerg         return 0;
40*ef84fd3bSjoerg     if ((unsigned)e > sizeof(su_int) * CHAR_BIT)
41*ef84fd3bSjoerg         return ~(su_int)0;
42156cd587Sjoerg     return fb.u.low.s.high >> (31 - e);
43156cd587Sjoerg }
44156cd587Sjoerg 
45156cd587Sjoerg #endif /* !_ARCH_PPC */
46