Michal Gorny | 9fcfdd0 | 2017-01-06 18:46:35 +0000 | [diff] [blame] | 1 | //===-- lib/floatuntitf.c - uint128 -> quad-precision conversion --*- C -*-===// |
| 2 | // |
Chandler Carruth | 7a739a0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Michal Gorny | 9fcfdd0 | 2017-01-06 18:46:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements tu_int to quad-precision conversion for the |
| 10 | // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even |
| 11 | // mode. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define QUAD_PRECISION |
| 16 | #include "fp_lib.h" |
| 17 | #include "int_lib.h" |
| 18 | |
| 19 | /* Returns: convert a tu_int to a fp_t, rounding toward even. */ |
| 20 | |
| 21 | /* Assumption: fp_t is a IEEE 128 bit floating point type |
| 22 | * tu_int is a 128 bit integral type |
| 23 | */ |
| 24 | |
| 25 | /* seee eeee eeee eeee mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | |
| 26 | * mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm |
| 27 | */ |
| 28 | |
| 29 | #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) |
| 30 | COMPILER_RT_ABI fp_t |
| 31 | __floatuntitf(tu_int a) { |
| 32 | if (a == 0) |
| 33 | return 0.0; |
| 34 | const unsigned N = sizeof(tu_int) * CHAR_BIT; |
| 35 | int sd = N - __clzti2(a); /* number of significant digits */ |
| 36 | int e = sd - 1; /* exponent */ |
| 37 | if (sd > LDBL_MANT_DIG) { |
| 38 | /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx |
| 39 | * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR |
| 40 | * 12345678901234567890123456 |
| 41 | * 1 = msb 1 bit |
| 42 | * P = bit LDBL_MANT_DIG-1 bits to the right of 1 |
| 43 | * Q = bit LDBL_MANT_DIG bits to the right of 1 |
| 44 | * R = "or" of all bits to the right of Q |
| 45 | */ |
| 46 | switch (sd) { |
| 47 | case LDBL_MANT_DIG + 1: |
| 48 | a <<= 1; |
| 49 | break; |
| 50 | case LDBL_MANT_DIG + 2: |
| 51 | break; |
| 52 | default: |
| 53 | a = (a >> (sd - (LDBL_MANT_DIG+2))) | |
| 54 | ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0); |
| 55 | }; |
| 56 | /* finish: */ |
| 57 | a |= (a & 4) != 0; /* Or P into R */ |
| 58 | ++a; /* round - this step may add a significant bit */ |
| 59 | a >>= 2; /* dump Q and R */ |
| 60 | /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */ |
| 61 | if (a & ((tu_int)1 << LDBL_MANT_DIG)) { |
| 62 | a >>= 1; |
| 63 | ++e; |
| 64 | } |
| 65 | /* a is now rounded to LDBL_MANT_DIG bits */ |
| 66 | } else { |
| 67 | a <<= (LDBL_MANT_DIG - sd); |
| 68 | /* a is now rounded to LDBL_MANT_DIG bits */ |
| 69 | } |
| 70 | |
| 71 | long_double_bits fb; |
| 72 | fb.u.high.all = (du_int)(e + 16383) << 48 /* exponent */ |
| 73 | | ((a >> 64) & 0x0000ffffffffffffLL); /* significand */ |
| 74 | fb.u.low.all = (du_int)(a); |
| 75 | return fb.f; |
| 76 | } |
| 77 | |
| 78 | #endif |