xref: /openbsd-src/gnu/llvm/compiler-rt/lib/builtins/floatsitf.c (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick //===-- lib/floatsitf.c - integer -> quad-precision conversion ----*- C -*-===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file implements integer to quad-precision conversion for the
103cab2bb3Spatrick // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
113cab2bb3Spatrick // mode.
123cab2bb3Spatrick //
133cab2bb3Spatrick //===----------------------------------------------------------------------===//
143cab2bb3Spatrick 
153cab2bb3Spatrick #define QUAD_PRECISION
163cab2bb3Spatrick #include "fp_lib.h"
173cab2bb3Spatrick 
183cab2bb3Spatrick #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
__floatsitf(si_int a)19*810390e3Srobert COMPILER_RT_ABI fp_t __floatsitf(si_int a) {
203cab2bb3Spatrick 
213cab2bb3Spatrick   const int aWidth = sizeof a * CHAR_BIT;
223cab2bb3Spatrick 
233cab2bb3Spatrick   // Handle zero as a special case to protect clz
243cab2bb3Spatrick   if (a == 0)
253cab2bb3Spatrick     return fromRep(0);
263cab2bb3Spatrick 
273cab2bb3Spatrick   // All other cases begin by extracting the sign and absolute value of a
283cab2bb3Spatrick   rep_t sign = 0;
29*810390e3Srobert   su_int aAbs = (su_int)a;
303cab2bb3Spatrick   if (a < 0) {
313cab2bb3Spatrick     sign = signBit;
32*810390e3Srobert     aAbs = ~(su_int)a + (su_int)1U;
333cab2bb3Spatrick   }
343cab2bb3Spatrick 
353cab2bb3Spatrick   // Exponent of (fp_t)a is the width of abs(a).
36*810390e3Srobert   const int exponent = (aWidth - 1) - clzsi(aAbs);
373cab2bb3Spatrick   rep_t result;
383cab2bb3Spatrick 
393cab2bb3Spatrick   // Shift a into the significand field and clear the implicit bit.
403cab2bb3Spatrick   const int shift = significandBits - exponent;
413cab2bb3Spatrick   result = (rep_t)aAbs << shift ^ implicitBit;
423cab2bb3Spatrick 
433cab2bb3Spatrick   // Insert the exponent
443cab2bb3Spatrick   result += (rep_t)(exponent + exponentBias) << significandBits;
453cab2bb3Spatrick   // Insert the sign bit and return
463cab2bb3Spatrick   return fromRep(result | sign);
473cab2bb3Spatrick }
483cab2bb3Spatrick 
493cab2bb3Spatrick #endif
50