1*0a6a1f1dSLionel Sambuc /* ===-- floattixf.c - Implement __floattixf -------------------------------=== 2*0a6a1f1dSLionel Sambuc * 3*0a6a1f1dSLionel Sambuc * The LLVM Compiler Infrastructure 4*0a6a1f1dSLionel Sambuc * 5*0a6a1f1dSLionel Sambuc * This file is dual licensed under the MIT and the University of Illinois Open 6*0a6a1f1dSLionel Sambuc * Source Licenses. See LICENSE.TXT for details. 7*0a6a1f1dSLionel Sambuc * 8*0a6a1f1dSLionel Sambuc * ===----------------------------------------------------------------------=== 9*0a6a1f1dSLionel Sambuc * 10*0a6a1f1dSLionel Sambuc * This file implements __floattixf for the compiler_rt library. 11*0a6a1f1dSLionel Sambuc * 12*0a6a1f1dSLionel Sambuc * ===----------------------------------------------------------------------=== 13*0a6a1f1dSLionel Sambuc */ 14*0a6a1f1dSLionel Sambuc 15*0a6a1f1dSLionel Sambuc #include "int_lib.h" 16*0a6a1f1dSLionel Sambuc 17*0a6a1f1dSLionel Sambuc #ifdef CRT_HAS_128BIT 18*0a6a1f1dSLionel Sambuc 19*0a6a1f1dSLionel Sambuc /* Returns: convert a to a long double, rounding toward even. */ 20*0a6a1f1dSLionel Sambuc 21*0a6a1f1dSLionel Sambuc /* Assumption: long double is a IEEE 80 bit floating point type padded to 128 bits 22*0a6a1f1dSLionel Sambuc * ti_int is a 128 bit integral type 23*0a6a1f1dSLionel Sambuc */ 24*0a6a1f1dSLionel Sambuc 25*0a6a1f1dSLionel Sambuc /* gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee | 26*0a6a1f1dSLionel Sambuc * 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm 27*0a6a1f1dSLionel Sambuc */ 28*0a6a1f1dSLionel Sambuc 29*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI long double __floattixf(ti_int a)30*0a6a1f1dSLionel Sambuc__floattixf(ti_int a) 31*0a6a1f1dSLionel Sambuc { 32*0a6a1f1dSLionel Sambuc if (a == 0) 33*0a6a1f1dSLionel Sambuc return 0.0; 34*0a6a1f1dSLionel Sambuc const unsigned N = sizeof(ti_int) * CHAR_BIT; 35*0a6a1f1dSLionel Sambuc const ti_int s = a >> (N-1); 36*0a6a1f1dSLionel Sambuc a = (a ^ s) - s; 37*0a6a1f1dSLionel Sambuc int sd = N - __clzti2(a); /* number of significant digits */ 38*0a6a1f1dSLionel Sambuc int e = sd - 1; /* exponent */ 39*0a6a1f1dSLionel Sambuc if (sd > LDBL_MANT_DIG) 40*0a6a1f1dSLionel Sambuc { 41*0a6a1f1dSLionel Sambuc /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 42*0a6a1f1dSLionel Sambuc * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 43*0a6a1f1dSLionel Sambuc * 12345678901234567890123456 44*0a6a1f1dSLionel Sambuc * 1 = msb 1 bit 45*0a6a1f1dSLionel Sambuc * P = bit LDBL_MANT_DIG-1 bits to the right of 1 46*0a6a1f1dSLionel Sambuc * Q = bit LDBL_MANT_DIG bits to the right of 1 47*0a6a1f1dSLionel Sambuc * R = "or" of all bits to the right of Q 48*0a6a1f1dSLionel Sambuc */ 49*0a6a1f1dSLionel Sambuc switch (sd) 50*0a6a1f1dSLionel Sambuc { 51*0a6a1f1dSLionel Sambuc case LDBL_MANT_DIG + 1: 52*0a6a1f1dSLionel Sambuc a <<= 1; 53*0a6a1f1dSLionel Sambuc break; 54*0a6a1f1dSLionel Sambuc case LDBL_MANT_DIG + 2: 55*0a6a1f1dSLionel Sambuc break; 56*0a6a1f1dSLionel Sambuc default: 57*0a6a1f1dSLionel Sambuc a = ((tu_int)a >> (sd - (LDBL_MANT_DIG+2))) | 58*0a6a1f1dSLionel Sambuc ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0); 59*0a6a1f1dSLionel Sambuc }; 60*0a6a1f1dSLionel Sambuc /* finish: */ 61*0a6a1f1dSLionel Sambuc a |= (a & 4) != 0; /* Or P into R */ 62*0a6a1f1dSLionel Sambuc ++a; /* round - this step may add a significant bit */ 63*0a6a1f1dSLionel Sambuc a >>= 2; /* dump Q and R */ 64*0a6a1f1dSLionel Sambuc /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */ 65*0a6a1f1dSLionel Sambuc if (a & ((tu_int)1 << LDBL_MANT_DIG)) 66*0a6a1f1dSLionel Sambuc { 67*0a6a1f1dSLionel Sambuc a >>= 1; 68*0a6a1f1dSLionel Sambuc ++e; 69*0a6a1f1dSLionel Sambuc } 70*0a6a1f1dSLionel Sambuc /* a is now rounded to LDBL_MANT_DIG bits */ 71*0a6a1f1dSLionel Sambuc } 72*0a6a1f1dSLionel Sambuc else 73*0a6a1f1dSLionel Sambuc { 74*0a6a1f1dSLionel Sambuc a <<= (LDBL_MANT_DIG - sd); 75*0a6a1f1dSLionel Sambuc /* a is now rounded to LDBL_MANT_DIG bits */ 76*0a6a1f1dSLionel Sambuc } 77*0a6a1f1dSLionel Sambuc long_double_bits fb; 78*0a6a1f1dSLionel Sambuc fb.u.high.s.low = ((su_int)s & 0x8000) | /* sign */ 79*0a6a1f1dSLionel Sambuc (e + 16383); /* exponent */ 80*0a6a1f1dSLionel Sambuc fb.u.low.all = (du_int)a; /* mantissa */ 81*0a6a1f1dSLionel Sambuc return fb.f; 82*0a6a1f1dSLionel Sambuc } 83*0a6a1f1dSLionel Sambuc 84*0a6a1f1dSLionel Sambuc #endif /* CRT_HAS_128BIT */ 85