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