xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/floatdisf.c (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===-- floatdisf.c - Implement __floatdisf -------------------------------===//
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 __floatdisf for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric // Returns: convert a to a float, rounding toward even.
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric // Assumption: float is a IEEE 32 bit floating point type
160b57cec5SDimitry Andric //             di_int is a 64 bit integral type
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric // seee eeee emmm mmmm mmmm mmmm mmmm mmmm
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric #include "int_lib.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric COMPILER_RT_ABI float __floatdisf(di_int a) {
230b57cec5SDimitry Andric   if (a == 0)
240b57cec5SDimitry Andric     return 0.0F;
250b57cec5SDimitry Andric   const unsigned N = sizeof(di_int) * CHAR_BIT;
260b57cec5SDimitry Andric   const di_int s = a >> (N - 1);
270b57cec5SDimitry Andric   a = (a ^ s) - s;
280b57cec5SDimitry Andric   int sd = N - __builtin_clzll(a); // number of significant digits
295ffd83dbSDimitry Andric   si_int e = sd - 1;               // exponent
300b57cec5SDimitry Andric   if (sd > FLT_MANT_DIG) {
310b57cec5SDimitry Andric     //  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
320b57cec5SDimitry Andric     //  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
330b57cec5SDimitry Andric     //                                                12345678901234567890123456
340b57cec5SDimitry Andric     //  1 = msb 1 bit
350b57cec5SDimitry Andric     //  P = bit FLT_MANT_DIG-1 bits to the right of 1
360b57cec5SDimitry Andric     //  Q = bit FLT_MANT_DIG bits to the right of 1
370b57cec5SDimitry Andric     //  R = "or" of all bits to the right of Q
380b57cec5SDimitry Andric     switch (sd) {
390b57cec5SDimitry Andric     case FLT_MANT_DIG + 1:
400b57cec5SDimitry Andric       a <<= 1;
410b57cec5SDimitry Andric       break;
420b57cec5SDimitry Andric     case FLT_MANT_DIG + 2:
430b57cec5SDimitry Andric       break;
440b57cec5SDimitry Andric     default:
450b57cec5SDimitry Andric       a = ((du_int)a >> (sd - (FLT_MANT_DIG + 2))) |
460b57cec5SDimitry Andric           ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG + 2) - sd))) != 0);
470b57cec5SDimitry Andric     };
480b57cec5SDimitry Andric     // finish:
490b57cec5SDimitry Andric     a |= (a & 4) != 0; // Or P into R
500b57cec5SDimitry Andric     ++a;               // round - this step may add a significant bit
510b57cec5SDimitry Andric     a >>= 2;           // dump Q and R
520b57cec5SDimitry Andric     // a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits
530b57cec5SDimitry Andric     if (a & ((du_int)1 << FLT_MANT_DIG)) {
540b57cec5SDimitry Andric       a >>= 1;
550b57cec5SDimitry Andric       ++e;
560b57cec5SDimitry Andric     }
570b57cec5SDimitry Andric     // a is now rounded to FLT_MANT_DIG bits
580b57cec5SDimitry Andric   } else {
590b57cec5SDimitry Andric     a <<= (FLT_MANT_DIG - sd);
600b57cec5SDimitry Andric     // a is now rounded to FLT_MANT_DIG bits
610b57cec5SDimitry Andric   }
620b57cec5SDimitry Andric   float_bits fb;
630b57cec5SDimitry Andric   fb.u = ((su_int)s & 0x80000000) | // sign
640b57cec5SDimitry Andric          ((e + 127) << 23) |        // exponent
650b57cec5SDimitry Andric          ((su_int)a & 0x007FFFFF);  // mantissa
660b57cec5SDimitry Andric   return fb.f;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric #if defined(__ARM_EABI__)
700b57cec5SDimitry Andric #if defined(COMPILER_RT_ARMHF_TARGET)
710b57cec5SDimitry Andric AEABI_RTABI float __aeabi_l2f(di_int a) { return __floatdisf(a); }
720b57cec5SDimitry Andric #else
730b57cec5SDimitry Andric COMPILER_RT_ALIAS(__floatdisf, __aeabi_l2f)
740b57cec5SDimitry Andric #endif
750b57cec5SDimitry Andric #endif
76*349cc55cSDimitry Andric 
77*349cc55cSDimitry Andric #if defined(__MINGW32__) && defined(__arm__)
78*349cc55cSDimitry Andric COMPILER_RT_ALIAS(__floatdisf, __i64tos)
79*349cc55cSDimitry Andric #endif
80