xref: /aosp_15_r20/external/compiler-rt/lib/builtins/floatsitf.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- lib/floatsitf.c - integer -> 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 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)
__floatsitf(int a)20*7c3d14c8STreehugger Robot COMPILER_RT_ABI fp_t __floatsitf(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)
26*7c3d14c8STreehugger Robot         return fromRep(0);
27*7c3d14c8STreehugger Robot 
28*7c3d14c8STreehugger Robot     // All other cases begin by extracting the sign and absolute value of a
29*7c3d14c8STreehugger Robot     rep_t sign = 0;
30*7c3d14c8STreehugger Robot     unsigned aAbs = (unsigned)a;
31*7c3d14c8STreehugger Robot     if (a < 0) {
32*7c3d14c8STreehugger Robot         sign = signBit;
33*7c3d14c8STreehugger Robot         aAbs = ~(unsigned)a + 1U;
34*7c3d14c8STreehugger Robot     }
35*7c3d14c8STreehugger Robot 
36*7c3d14c8STreehugger Robot     // Exponent of (fp_t)a is the width of abs(a).
37*7c3d14c8STreehugger Robot     const int exponent = (aWidth - 1) - __builtin_clz(aAbs);
38*7c3d14c8STreehugger Robot     rep_t result;
39*7c3d14c8STreehugger Robot 
40*7c3d14c8STreehugger Robot     // Shift a into the significand field and clear the implicit bit.
41*7c3d14c8STreehugger Robot     const int shift = significandBits - exponent;
42*7c3d14c8STreehugger Robot     result = (rep_t)aAbs << shift ^ implicitBit;
43*7c3d14c8STreehugger Robot 
44*7c3d14c8STreehugger Robot     // Insert the exponent
45*7c3d14c8STreehugger Robot     result += (rep_t)(exponent + exponentBias) << significandBits;
46*7c3d14c8STreehugger Robot     // Insert the sign bit and return
47*7c3d14c8STreehugger Robot     return fromRep(result | sign);
48*7c3d14c8STreehugger Robot }
49*7c3d14c8STreehugger Robot 
50*7c3d14c8STreehugger Robot #endif
51