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