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