xref: /openbsd-src/gnu/llvm/compiler-rt/lib/builtins/floatditf.c (revision 3cab2bb3f667058bece8e38b12449a63a9d73c4b)
1*3cab2bb3Spatrick //===-- lib/floatditf.c - integer -> quad-precision conversion ----*- C -*-===//
2*3cab2bb3Spatrick //
3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*3cab2bb3Spatrick //
7*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
8*3cab2bb3Spatrick //
9*3cab2bb3Spatrick // This file implements di_int to quad-precision conversion for the
10*3cab2bb3Spatrick // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
11*3cab2bb3Spatrick // mode.
12*3cab2bb3Spatrick //
13*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
14*3cab2bb3Spatrick 
15*3cab2bb3Spatrick #define QUAD_PRECISION
16*3cab2bb3Spatrick #include "fp_lib.h"
17*3cab2bb3Spatrick 
18*3cab2bb3Spatrick #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
__floatditf(di_int a)19*3cab2bb3Spatrick COMPILER_RT_ABI fp_t __floatditf(di_int a) {
20*3cab2bb3Spatrick 
21*3cab2bb3Spatrick   const int aWidth = sizeof a * CHAR_BIT;
22*3cab2bb3Spatrick 
23*3cab2bb3Spatrick   // Handle zero as a special case to protect clz
24*3cab2bb3Spatrick   if (a == 0)
25*3cab2bb3Spatrick     return fromRep(0);
26*3cab2bb3Spatrick 
27*3cab2bb3Spatrick   // All other cases begin by extracting the sign and absolute value of a
28*3cab2bb3Spatrick   rep_t sign = 0;
29*3cab2bb3Spatrick   du_int aAbs = (du_int)a;
30*3cab2bb3Spatrick   if (a < 0) {
31*3cab2bb3Spatrick     sign = signBit;
32*3cab2bb3Spatrick     aAbs = ~(du_int)a + 1U;
33*3cab2bb3Spatrick   }
34*3cab2bb3Spatrick 
35*3cab2bb3Spatrick   // Exponent of (fp_t)a is the width of abs(a).
36*3cab2bb3Spatrick   const int exponent = (aWidth - 1) - __builtin_clzll(aAbs);
37*3cab2bb3Spatrick   rep_t result;
38*3cab2bb3Spatrick 
39*3cab2bb3Spatrick   // Shift a into the significand field, rounding if it is a right-shift
40*3cab2bb3Spatrick   const int shift = significandBits - exponent;
41*3cab2bb3Spatrick   result = (rep_t)aAbs << shift ^ implicitBit;
42*3cab2bb3Spatrick 
43*3cab2bb3Spatrick   // Insert the exponent
44*3cab2bb3Spatrick   result += (rep_t)(exponent + exponentBias) << significandBits;
45*3cab2bb3Spatrick   // Insert the sign bit and return
46*3cab2bb3Spatrick   return fromRep(result | sign);
47*3cab2bb3Spatrick }
48*3cab2bb3Spatrick 
49*3cab2bb3Spatrick #endif
50