xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/floatunsisf.c (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===-- lib/floatunsisf.c - uint -> single-precision conversion ---*- C -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements unsigned integer to single-precision conversion for the
10*0b57cec5SDimitry Andric // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
11*0b57cec5SDimitry Andric // mode.
12*0b57cec5SDimitry Andric //
13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric #define SINGLE_PRECISION
16*0b57cec5SDimitry Andric #include "fp_lib.h"
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric #include "int_lib.h"
19*0b57cec5SDimitry Andric 
20*0b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __floatunsisf(unsigned int a) {
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric   const int aWidth = sizeof a * CHAR_BIT;
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric   // Handle zero as a special case to protect clz
25*0b57cec5SDimitry Andric   if (a == 0)
26*0b57cec5SDimitry Andric     return fromRep(0);
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric   // Exponent of (fp_t)a is the width of abs(a).
29*0b57cec5SDimitry Andric   const int exponent = (aWidth - 1) - __builtin_clz(a);
30*0b57cec5SDimitry Andric   rep_t result;
31*0b57cec5SDimitry Andric 
32*0b57cec5SDimitry Andric   // Shift a into the significand field, rounding if it is a right-shift
33*0b57cec5SDimitry Andric   if (exponent <= significandBits) {
34*0b57cec5SDimitry Andric     const int shift = significandBits - exponent;
35*0b57cec5SDimitry Andric     result = (rep_t)a << shift ^ implicitBit;
36*0b57cec5SDimitry Andric   } else {
37*0b57cec5SDimitry Andric     const int shift = exponent - significandBits;
38*0b57cec5SDimitry Andric     result = (rep_t)a >> shift ^ implicitBit;
39*0b57cec5SDimitry Andric     rep_t round = (rep_t)a << (typeWidth - shift);
40*0b57cec5SDimitry Andric     if (round > signBit)
41*0b57cec5SDimitry Andric       result++;
42*0b57cec5SDimitry Andric     if (round == signBit)
43*0b57cec5SDimitry Andric       result += result & 1;
44*0b57cec5SDimitry Andric   }
45*0b57cec5SDimitry Andric 
46*0b57cec5SDimitry Andric   // Insert the exponent
47*0b57cec5SDimitry Andric   result += (rep_t)(exponent + exponentBias) << significandBits;
48*0b57cec5SDimitry Andric   return fromRep(result);
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric 
51*0b57cec5SDimitry Andric #if defined(__ARM_EABI__)
52*0b57cec5SDimitry Andric #if defined(COMPILER_RT_ARMHF_TARGET)
53*0b57cec5SDimitry Andric AEABI_RTABI fp_t __aeabi_ui2f(unsigned int a) { return __floatunsisf(a); }
54*0b57cec5SDimitry Andric #else
55*0b57cec5SDimitry Andric COMPILER_RT_ALIAS(__floatunsisf, __aeabi_ui2f)
56*0b57cec5SDimitry Andric #endif
57*0b57cec5SDimitry Andric #endif
58