xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/builtins/floatundixf.c (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- floatundixf.c - Implement __floatundixf ---------------------------===//
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 __floatundixf for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #if !_ARCH_PPC
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "int_lib.h"
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 du_int is a 64 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
__floatundixf(du_int a)25*5f757f3fSDimitry Andric COMPILER_RT_ABI xf_float __floatundixf(du_int a) {
260b57cec5SDimitry Andric   if (a == 0)
270b57cec5SDimitry Andric     return 0.0;
280b57cec5SDimitry Andric   const unsigned N = sizeof(du_int) * CHAR_BIT;
290b57cec5SDimitry Andric   int clz = __builtin_clzll(a);
300b57cec5SDimitry Andric   int e = (N - 1) - clz; // exponent
31*5f757f3fSDimitry Andric   xf_bits fb;
320b57cec5SDimitry Andric   fb.u.high.s.low = (e + 16383); // exponent
330b57cec5SDimitry Andric   fb.u.low.all = a << clz;       // mantissa
340b57cec5SDimitry Andric   return fb.f;
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric #endif // _ARCH_PPC
38