10b57cec5SDimitry Andric //===-- floatuntixf.c - Implement __floatuntixf ---------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements __floatuntixf for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "int_lib.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #ifdef CRT_HAS_128BIT
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric // Returns: convert a to a long double, rounding toward even.
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric // Assumption: long double is a IEEE 80 bit floating point type padded to 128
200b57cec5SDimitry Andric // bits tu_int is a 128 bit integral type
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric // gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee
230b57cec5SDimitry Andric // eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
240b57cec5SDimitry Andric // mmmm mmmm mmmm
250b57cec5SDimitry Andric 
__floatuntixf(tu_int a)265f757f3fSDimitry Andric COMPILER_RT_ABI xf_float __floatuntixf(tu_int a) {
270b57cec5SDimitry Andric   if (a == 0)
280b57cec5SDimitry Andric     return 0.0;
290b57cec5SDimitry Andric   const unsigned N = sizeof(tu_int) * CHAR_BIT;
300b57cec5SDimitry Andric   int sd = N - __clzti2(a); // number of significant digits
310b57cec5SDimitry Andric   int e = sd - 1;           // exponent
320b57cec5SDimitry Andric   if (sd > LDBL_MANT_DIG) {
330b57cec5SDimitry Andric     //  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
340b57cec5SDimitry Andric     //  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
350b57cec5SDimitry Andric     //                                                12345678901234567890123456
360b57cec5SDimitry Andric     //  1 = msb 1 bit
370b57cec5SDimitry Andric     //  P = bit LDBL_MANT_DIG-1 bits to the right of 1
380b57cec5SDimitry Andric     //  Q = bit LDBL_MANT_DIG bits to the right of 1
390b57cec5SDimitry Andric     //  R = "or" of all bits to the right of Q
400b57cec5SDimitry Andric     switch (sd) {
410b57cec5SDimitry Andric     case LDBL_MANT_DIG + 1:
420b57cec5SDimitry Andric       a <<= 1;
430b57cec5SDimitry Andric       break;
440b57cec5SDimitry Andric     case LDBL_MANT_DIG + 2:
450b57cec5SDimitry Andric       break;
460b57cec5SDimitry Andric     default:
470b57cec5SDimitry Andric       a = (a >> (sd - (LDBL_MANT_DIG + 2))) |
480b57cec5SDimitry Andric           ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG + 2) - sd))) != 0);
490b57cec5SDimitry Andric     };
500b57cec5SDimitry Andric     // finish:
510b57cec5SDimitry Andric     a |= (a & 4) != 0; // Or P into R
520b57cec5SDimitry Andric     ++a;               // round - this step may add a significant bit
530b57cec5SDimitry Andric     a >>= 2;           // dump Q and R
540b57cec5SDimitry Andric     // a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits
550b57cec5SDimitry Andric     if (a & ((tu_int)1 << LDBL_MANT_DIG)) {
560b57cec5SDimitry Andric       a >>= 1;
570b57cec5SDimitry Andric       ++e;
580b57cec5SDimitry Andric     }
590b57cec5SDimitry Andric     // a is now rounded to LDBL_MANT_DIG bits
600b57cec5SDimitry Andric   } else {
610b57cec5SDimitry Andric     a <<= (LDBL_MANT_DIG - sd);
620b57cec5SDimitry Andric     // a is now rounded to LDBL_MANT_DIG bits
630b57cec5SDimitry Andric   }
645f757f3fSDimitry Andric   xf_bits fb;
650b57cec5SDimitry Andric   fb.u.high.s.low = (e + 16383); // exponent
660b57cec5SDimitry Andric   fb.u.low.all = (du_int)a;      // mantissa
670b57cec5SDimitry Andric   return fb.f;
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric #endif
71