1 //===-- floatdisf.c - Implement __floatdisf -------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements __floatdisf for the compiler_rt library. 10 // 11 //===----------------------------------------------------------------------===// 12 13 // Returns: convert a to a float, rounding toward even. 14 15 // Assumption: float is a IEEE 32 bit floating point type 16 // di_int is a 64 bit integral type 17 18 // seee eeee emmm mmmm mmmm mmmm mmmm mmmm 19 20 #include "int_lib.h" 21 22 COMPILER_RT_ABI float __floatdisf(di_int a) { 23 if (a == 0) 24 return 0.0F; 25 const unsigned N = sizeof(di_int) * CHAR_BIT; 26 const di_int s = a >> (N - 1); 27 a = (du_int)(a ^ s) - s; 28 int sd = N - __builtin_clzll(a); // number of significant digits 29 si_int e = sd - 1; // exponent 30 if (sd > FLT_MANT_DIG) { 31 // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 32 // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 33 // 12345678901234567890123456 34 // 1 = msb 1 bit 35 // P = bit FLT_MANT_DIG-1 bits to the right of 1 36 // Q = bit FLT_MANT_DIG bits to the right of 1 37 // R = "or" of all bits to the right of Q 38 switch (sd) { 39 case FLT_MANT_DIG + 1: 40 a <<= 1; 41 break; 42 case FLT_MANT_DIG + 2: 43 break; 44 default: 45 a = ((du_int)a >> (sd - (FLT_MANT_DIG + 2))) | 46 ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG + 2) - sd))) != 0); 47 }; 48 // finish: 49 a |= (a & 4) != 0; // Or P into R 50 ++a; // round - this step may add a significant bit 51 a >>= 2; // dump Q and R 52 // a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits 53 if (a & ((du_int)1 << FLT_MANT_DIG)) { 54 a >>= 1; 55 ++e; 56 } 57 // a is now rounded to FLT_MANT_DIG bits 58 } else { 59 a <<= (FLT_MANT_DIG - sd); 60 // a is now rounded to FLT_MANT_DIG bits 61 } 62 float_bits fb; 63 fb.u = ((su_int)s & 0x80000000) | // sign 64 ((e + 127) << 23) | // exponent 65 ((su_int)a & 0x007FFFFF); // mantissa 66 return fb.f; 67 } 68 69 #if defined(__ARM_EABI__) 70 #if defined(COMPILER_RT_ARMHF_TARGET) 71 AEABI_RTABI float __aeabi_l2f(di_int a) { return __floatdisf(a); } 72 #else 73 COMPILER_RT_ALIAS(__floatdisf, __aeabi_l2f) 74 #endif 75 #endif 76 77 #if defined(__MINGW32__) && defined(__arm__) 78 COMPILER_RT_ALIAS(__floatdisf, __i64tos) 79 #endif 80