1353358Sdim//===-- floatundisf.c - Implement __floatundisf ---------------------------===//
2353358Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6353358Sdim//
7353358Sdim//===----------------------------------------------------------------------===//
8353358Sdim//
9353358Sdim// This file implements __floatundisf for the compiler_rt library.
10353358Sdim//
11353358Sdim//===----------------------------------------------------------------------===//
12276789Sdim
13353358Sdim// Returns: convert a to a float, rounding toward even.
14276789Sdim
15353358Sdim// Assumption: float is a IEEE 32 bit floating point type
16353358Sdim//            du_int is a 64 bit integral type
17276789Sdim
18353358Sdim// seee eeee emmm mmmm mmmm mmmm mmmm mmmm
19276789Sdim
20276789Sdim#include "int_lib.h"
21276789Sdim
22353358SdimCOMPILER_RT_ABI float __floatundisf(du_int a) {
23353358Sdim  if (a == 0)
24353358Sdim    return 0.0F;
25353358Sdim  const unsigned N = sizeof(du_int) * CHAR_BIT;
26353358Sdim  int sd = N - __builtin_clzll(a); // number of significant digits
27353358Sdim  int e = sd - 1;                  // 8 exponent
28353358Sdim  if (sd > FLT_MANT_DIG) {
29353358Sdim    //  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
30353358Sdim    //  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
31353358Sdim    //                                                12345678901234567890123456
32353358Sdim    //  1 = msb 1 bit
33353358Sdim    //  P = bit FLT_MANT_DIG-1 bits to the right of 1
34353358Sdim    //  Q = bit FLT_MANT_DIG bits to the right of 1
35353358Sdim    //  R = "or" of all bits to the right of Q
36353358Sdim    switch (sd) {
37353358Sdim    case FLT_MANT_DIG + 1:
38353358Sdim      a <<= 1;
39353358Sdim      break;
40353358Sdim    case FLT_MANT_DIG + 2:
41353358Sdim      break;
42353358Sdim    default:
43353358Sdim      a = (a >> (sd - (FLT_MANT_DIG + 2))) |
44353358Sdim          ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG + 2) - sd))) != 0);
45353358Sdim    };
46353358Sdim    // finish:
47353358Sdim    a |= (a & 4) != 0; // Or P into R
48353358Sdim    ++a;               // round - this step may add a significant bit
49353358Sdim    a >>= 2;           // dump Q and R
50353358Sdim    // a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits
51353358Sdim    if (a & ((du_int)1 << FLT_MANT_DIG)) {
52353358Sdim      a >>= 1;
53353358Sdim      ++e;
54276789Sdim    }
55353358Sdim    // a is now rounded to FLT_MANT_DIG bits
56353358Sdim  } else {
57353358Sdim    a <<= (FLT_MANT_DIG - sd);
58353358Sdim    // a is now rounded to FLT_MANT_DIG bits
59353358Sdim  }
60353358Sdim  float_bits fb;
61353358Sdim  fb.u = ((e + 127) << 23) |       // exponent
62353358Sdim         ((su_int)a & 0x007FFFFF); // mantissa
63353358Sdim  return fb.f;
64276789Sdim}
65321369Sdim
66321369Sdim#if defined(__ARM_EABI__)
67327952Sdim#if defined(COMPILER_RT_ARMHF_TARGET)
68353358SdimAEABI_RTABI float __aeabi_ul2f(du_int a) { return __floatundisf(a); }
69327952Sdim#else
70353358SdimCOMPILER_RT_ALIAS(__floatundisf, __aeabi_ul2f)
71321369Sdim#endif
72327952Sdim#endif
73