xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/floatsidf.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===-- lib/floatsidf.c - integer -> double-precision conversion --*- C -*-===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This file implements integer to double-precision conversion for the
11*0a6a1f1dSLionel Sambuc // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
12*0a6a1f1dSLionel Sambuc // mode.
13*0a6a1f1dSLionel Sambuc //
14*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
15*0a6a1f1dSLionel Sambuc 
16*0a6a1f1dSLionel Sambuc #define DOUBLE_PRECISION
17*0a6a1f1dSLionel Sambuc #include "fp_lib.h"
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc #include "int_lib.h"
20*0a6a1f1dSLionel Sambuc 
ARM_EABI_FNALIAS(i2d,floatsidf)21*0a6a1f1dSLionel Sambuc ARM_EABI_FNALIAS(i2d, floatsidf)
22*0a6a1f1dSLionel Sambuc 
23*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI fp_t
24*0a6a1f1dSLionel Sambuc __floatsidf(int a) {
25*0a6a1f1dSLionel Sambuc 
26*0a6a1f1dSLionel Sambuc     const int aWidth = sizeof a * CHAR_BIT;
27*0a6a1f1dSLionel Sambuc 
28*0a6a1f1dSLionel Sambuc     // Handle zero as a special case to protect clz
29*0a6a1f1dSLionel Sambuc     if (a == 0)
30*0a6a1f1dSLionel Sambuc         return fromRep(0);
31*0a6a1f1dSLionel Sambuc 
32*0a6a1f1dSLionel Sambuc     // All other cases begin by extracting the sign and absolute value of a
33*0a6a1f1dSLionel Sambuc     rep_t sign = 0;
34*0a6a1f1dSLionel Sambuc     if (a < 0) {
35*0a6a1f1dSLionel Sambuc         sign = signBit;
36*0a6a1f1dSLionel Sambuc         a = -a;
37*0a6a1f1dSLionel Sambuc     }
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc     // Exponent of (fp_t)a is the width of abs(a).
40*0a6a1f1dSLionel Sambuc     const int exponent = (aWidth - 1) - __builtin_clz(a);
41*0a6a1f1dSLionel Sambuc     rep_t result;
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc     // Shift a into the significand field and clear the implicit bit.  Extra
44*0a6a1f1dSLionel Sambuc     // cast to unsigned int is necessary to get the correct behavior for
45*0a6a1f1dSLionel Sambuc     // the input INT_MIN.
46*0a6a1f1dSLionel Sambuc     const int shift = significandBits - exponent;
47*0a6a1f1dSLionel Sambuc     result = (rep_t)(unsigned int)a << shift ^ implicitBit;
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc     // Insert the exponent
50*0a6a1f1dSLionel Sambuc     result += (rep_t)(exponent + exponentBias) << significandBits;
51*0a6a1f1dSLionel Sambuc     // Insert the sign bit and return
52*0a6a1f1dSLionel Sambuc     return fromRep(result | sign);
53*0a6a1f1dSLionel Sambuc }
54