floatundisf.c revision 239138
1214152Sed/*===-- floatundisf.c - Implement __floatundisf ---------------------------=== 2214152Sed * 3214152Sed * The LLVM Compiler Infrastructure 4214152Sed * 5222656Sed * This file is dual licensed under the MIT and the University of Illinois Open 6222656Sed * Source Licenses. See LICENSE.TXT for details. 7214152Sed * 8214152Sed * ===----------------------------------------------------------------------=== 9214152Sed * 10214152Sed * This file implements __floatundisf for the compiler_rt library. 11214152Sed * 12214152Sed *===----------------------------------------------------------------------=== 13214152Sed */ 14214152Sed 15214152Sed/* Returns: convert a to a float, rounding toward even. */ 16214152Sed 17214152Sed/* Assumption: float is a IEEE 32 bit floating point type 18214152Sed * du_int is a 64 bit integral type 19214152Sed */ 20214152Sed 21214152Sed/* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */ 22214152Sed 23222656Sed#include "int_lib.h" 24222656Sed 25239138SandrewARM_EABI_FNALIAS(ul2f, floatundisf) 26222656Sed 27222656SedCOMPILER_RT_ABI float 28214152Sed__floatundisf(du_int a) 29214152Sed{ 30214152Sed if (a == 0) 31214152Sed return 0.0F; 32214152Sed const unsigned N = sizeof(du_int) * CHAR_BIT; 33214152Sed int sd = N - __builtin_clzll(a); /* number of significant digits */ 34214152Sed int e = sd - 1; /* 8 exponent */ 35214152Sed if (sd > FLT_MANT_DIG) 36214152Sed { 37214152Sed /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 38214152Sed * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 39214152Sed * 12345678901234567890123456 40214152Sed * 1 = msb 1 bit 41214152Sed * P = bit FLT_MANT_DIG-1 bits to the right of 1 42214152Sed * Q = bit FLT_MANT_DIG bits to the right of 1 43214152Sed * R = "or" of all bits to the right of Q 44214152Sed */ 45214152Sed switch (sd) 46214152Sed { 47214152Sed case FLT_MANT_DIG + 1: 48214152Sed a <<= 1; 49214152Sed break; 50214152Sed case FLT_MANT_DIG + 2: 51214152Sed break; 52214152Sed default: 53214152Sed a = (a >> (sd - (FLT_MANT_DIG+2))) | 54214152Sed ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG+2) - sd))) != 0); 55214152Sed }; 56214152Sed /* finish: */ 57214152Sed a |= (a & 4) != 0; /* Or P into R */ 58214152Sed ++a; /* round - this step may add a significant bit */ 59214152Sed a >>= 2; /* dump Q and R */ 60214152Sed /* a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits */ 61214152Sed if (a & ((du_int)1 << FLT_MANT_DIG)) 62214152Sed { 63214152Sed a >>= 1; 64214152Sed ++e; 65214152Sed } 66214152Sed /* a is now rounded to FLT_MANT_DIG bits */ 67214152Sed } 68214152Sed else 69214152Sed { 70214152Sed a <<= (FLT_MANT_DIG - sd); 71214152Sed /* a is now rounded to FLT_MANT_DIG bits */ 72214152Sed } 73214152Sed float_bits fb; 74214152Sed fb.u = ((e + 127) << 23) | /* exponent */ 75214152Sed ((su_int)a & 0x007FFFFF); /* mantissa */ 76214152Sed return fb.f; 77214152Sed} 78