xref: /aosp_15_r20/external/compiler-rt/lib/builtins/floatunsitf.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- lib/floatunsitf.c - uint -> quad-precision conversion -----*- C -*-===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is dual licensed under the MIT and the University of Illinois Open
6*7c3d14c8STreehugger Robot // Source Licenses. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file implements unsigned integer to quad-precision conversion for the
11*7c3d14c8STreehugger Robot // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
12*7c3d14c8STreehugger Robot // mode.
13*7c3d14c8STreehugger Robot //
14*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
15*7c3d14c8STreehugger Robot 
16*7c3d14c8STreehugger Robot #define QUAD_PRECISION
17*7c3d14c8STreehugger Robot #include "fp_lib.h"
18*7c3d14c8STreehugger Robot 
19*7c3d14c8STreehugger Robot #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
__floatunsitf(unsigned int a)20*7c3d14c8STreehugger Robot COMPILER_RT_ABI fp_t __floatunsitf(unsigned int a) {
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot     const int aWidth = sizeof a * CHAR_BIT;
23*7c3d14c8STreehugger Robot 
24*7c3d14c8STreehugger Robot     // Handle zero as a special case to protect clz
25*7c3d14c8STreehugger Robot     if (a == 0) return fromRep(0);
26*7c3d14c8STreehugger Robot 
27*7c3d14c8STreehugger Robot     // Exponent of (fp_t)a is the width of abs(a).
28*7c3d14c8STreehugger Robot     const int exponent = (aWidth - 1) - __builtin_clz(a);
29*7c3d14c8STreehugger Robot     rep_t result;
30*7c3d14c8STreehugger Robot 
31*7c3d14c8STreehugger Robot     // Shift a into the significand field and clear the implicit bit.
32*7c3d14c8STreehugger Robot     const int shift = significandBits - exponent;
33*7c3d14c8STreehugger Robot     result = (rep_t)a << shift ^ implicitBit;
34*7c3d14c8STreehugger Robot 
35*7c3d14c8STreehugger Robot     // Insert the exponent
36*7c3d14c8STreehugger Robot     result += (rep_t)(exponent + exponentBias) << significandBits;
37*7c3d14c8STreehugger Robot     return fromRep(result);
38*7c3d14c8STreehugger Robot }
39*7c3d14c8STreehugger Robot 
40*7c3d14c8STreehugger Robot #endif
41