1 //===-- floatundisf.c - Implement __floatundisf ---------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements __floatundisf for the compiler_rt library.
11 //===----------------------------------------------------------------------===//
13 // Returns: convert a to a float, rounding toward even.
15 // Assumption: float is a IEEE 32 bit floating point type
16 // du_int is a 64 bit integral type
18 // seee eeee emmm mmmm mmmm mmmm mmmm mmmm
22 COMPILER_RT_ABI float __floatundisf(du_int a) {
25 const unsigned N = sizeof(du_int) * CHAR_BIT;
26 int sd = N - __builtin_clzll(a); // number of significant digits
27 int e = sd - 1; // 8 exponent
28 if (sd > FLT_MANT_DIG) {
29 // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
30 // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
31 // 12345678901234567890123456
33 // P = bit FLT_MANT_DIG-1 bits to the right of 1
34 // Q = bit FLT_MANT_DIG bits to the right of 1
35 // R = "or" of all bits to the right of Q
37 case FLT_MANT_DIG + 1:
40 case FLT_MANT_DIG + 2:
43 a = (a >> (sd - (FLT_MANT_DIG + 2))) |
44 ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG + 2) - sd))) != 0);
47 a |= (a & 4) != 0; // Or P into R
48 ++a; // round - this step may add a significant bit
49 a >>= 2; // dump Q and R
50 // a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits
51 if (a & ((du_int)1 << FLT_MANT_DIG)) {
55 // a is now rounded to FLT_MANT_DIG bits
57 a <<= (FLT_MANT_DIG - sd);
58 // a is now rounded to FLT_MANT_DIG bits
61 fb.u = ((e + 127) << 23) | // exponent
62 ((su_int)a & 0x007FFFFF); // mantissa
66 #if defined(__ARM_EABI__)
67 #if defined(COMPILER_RT_ARMHF_TARGET)
68 AEABI_RTABI float __aeabi_ul2f(du_int a) { return __floatundisf(a); }
70 COMPILER_RT_ALIAS(__floatundisf, __aeabi_ul2f)