xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/floatunsidf.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===-- lib/floatunsidf.c - uint -> 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 unsigned 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(ui2d,floatunsidf)21*0a6a1f1dSLionel Sambuc ARM_EABI_FNALIAS(ui2d, floatunsidf)
22*0a6a1f1dSLionel Sambuc 
23*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI fp_t
24*0a6a1f1dSLionel Sambuc __floatunsidf(unsigned 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) return fromRep(0);
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc     // Exponent of (fp_t)a is the width of abs(a).
32*0a6a1f1dSLionel Sambuc     const int exponent = (aWidth - 1) - __builtin_clz(a);
33*0a6a1f1dSLionel Sambuc     rep_t result;
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc     // Shift a into the significand field and clear the implicit bit.
36*0a6a1f1dSLionel Sambuc     const int shift = significandBits - exponent;
37*0a6a1f1dSLionel Sambuc     result = (rep_t)a << shift ^ implicitBit;
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc     // Insert the exponent
40*0a6a1f1dSLionel Sambuc     result += (rep_t)(exponent + exponentBias) << significandBits;
41*0a6a1f1dSLionel Sambuc     return fromRep(result);
42*0a6a1f1dSLionel Sambuc }
43