xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/floattixf.c (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- floattixf.c - Implement __floattixf -------------------------------===//
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 __floattixf 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 ti_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 
26*5f757f3fSDimitry Andric COMPILER_RT_ABI xf_float __floattixf(ti_int a) {
270b57cec5SDimitry Andric   if (a == 0)
280b57cec5SDimitry Andric     return 0.0;
290b57cec5SDimitry Andric   const unsigned N = sizeof(ti_int) * CHAR_BIT;
300b57cec5SDimitry Andric   const ti_int s = a >> (N - 1);
310b57cec5SDimitry Andric   a = (a ^ s) - s;
320b57cec5SDimitry Andric   int sd = N - __clzti2(a); // number of significant digits
330b57cec5SDimitry Andric   int e = sd - 1;           // exponent
340b57cec5SDimitry Andric   if (sd > LDBL_MANT_DIG) {
350b57cec5SDimitry Andric     //  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
360b57cec5SDimitry Andric     //  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
370b57cec5SDimitry Andric     //                                                12345678901234567890123456
380b57cec5SDimitry Andric     //  1 = msb 1 bit
390b57cec5SDimitry Andric     //  P = bit LDBL_MANT_DIG-1 bits to the right of 1
400b57cec5SDimitry Andric     //  Q = bit LDBL_MANT_DIG bits to the right of 1
410b57cec5SDimitry Andric     //  R = "or" of all bits to the right of Q
420b57cec5SDimitry Andric     switch (sd) {
430b57cec5SDimitry Andric     case LDBL_MANT_DIG + 1:
440b57cec5SDimitry Andric       a <<= 1;
450b57cec5SDimitry Andric       break;
460b57cec5SDimitry Andric     case LDBL_MANT_DIG + 2:
470b57cec5SDimitry Andric       break;
480b57cec5SDimitry Andric     default:
490b57cec5SDimitry Andric       a = ((tu_int)a >> (sd - (LDBL_MANT_DIG + 2))) |
500b57cec5SDimitry Andric           ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG + 2) - sd))) != 0);
510b57cec5SDimitry Andric     };
520b57cec5SDimitry Andric     // finish:
530b57cec5SDimitry Andric     a |= (a & 4) != 0; // Or P into R
540b57cec5SDimitry Andric     ++a;               // round - this step may add a significant bit
550b57cec5SDimitry Andric     a >>= 2;           // dump Q and R
560b57cec5SDimitry Andric     // a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits
570b57cec5SDimitry Andric     if (a & ((tu_int)1 << LDBL_MANT_DIG)) {
580b57cec5SDimitry Andric       a >>= 1;
590b57cec5SDimitry Andric       ++e;
600b57cec5SDimitry Andric     }
610b57cec5SDimitry Andric     // a is now rounded to LDBL_MANT_DIG bits
620b57cec5SDimitry Andric   } else {
630b57cec5SDimitry Andric     a <<= (LDBL_MANT_DIG - sd);
640b57cec5SDimitry Andric     // a is now rounded to LDBL_MANT_DIG bits
650b57cec5SDimitry Andric   }
66*5f757f3fSDimitry Andric   xf_bits fb;
670b57cec5SDimitry Andric   fb.u.high.s.low = ((su_int)s & 0x8000) | // sign
680b57cec5SDimitry Andric                     (e + 16383);           // exponent
690b57cec5SDimitry Andric   fb.u.low.all = (du_int)a;                // mantissa
700b57cec5SDimitry Andric   return fb.f;
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric #endif // CRT_HAS_128BIT
74