xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/floatunsisf.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===-- lib/floatunsisf.c - uint -> single-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 single-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 SINGLE_PRECISION
17*0a6a1f1dSLionel Sambuc #include "fp_lib.h"
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc #include "int_lib.h"
20*0a6a1f1dSLionel Sambuc 
ARM_EABI_FNALIAS(ui2f,floatunsisf)21*0a6a1f1dSLionel Sambuc ARM_EABI_FNALIAS(ui2f, floatunsisf)
22*0a6a1f1dSLionel Sambuc 
23*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI fp_t
24*0a6a1f1dSLionel Sambuc __floatunsisf(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, rounding if it is a right-shift
36*0a6a1f1dSLionel Sambuc     if (exponent <= significandBits) {
37*0a6a1f1dSLionel Sambuc         const int shift = significandBits - exponent;
38*0a6a1f1dSLionel Sambuc         result = (rep_t)a << shift ^ implicitBit;
39*0a6a1f1dSLionel Sambuc     } else {
40*0a6a1f1dSLionel Sambuc         const int shift = exponent - significandBits;
41*0a6a1f1dSLionel Sambuc         result = (rep_t)a >> shift ^ implicitBit;
42*0a6a1f1dSLionel Sambuc         rep_t round = (rep_t)a << (typeWidth - shift);
43*0a6a1f1dSLionel Sambuc         if (round > signBit) result++;
44*0a6a1f1dSLionel Sambuc         if (round == signBit) result += result & 1;
45*0a6a1f1dSLionel Sambuc     }
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc     // Insert the exponent
48*0a6a1f1dSLionel Sambuc     result += (rep_t)(exponent + exponentBias) << significandBits;
49*0a6a1f1dSLionel Sambuc     return fromRep(result);
50*0a6a1f1dSLionel Sambuc }
51