xref: /openbsd-src/gnu/llvm/compiler-rt/lib/builtins/floatunsisf.c (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick //===-- lib/floatunsisf.c - uint -> single-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 unsigned integer to single-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 SINGLE_PRECISION
163cab2bb3Spatrick #include "fp_lib.h"
173cab2bb3Spatrick 
183cab2bb3Spatrick #include "int_lib.h"
193cab2bb3Spatrick 
__floatunsisf(su_int a)20*810390e3Srobert COMPILER_RT_ABI fp_t __floatunsisf(su_int a) {
213cab2bb3Spatrick 
223cab2bb3Spatrick   const int aWidth = sizeof a * CHAR_BIT;
233cab2bb3Spatrick 
243cab2bb3Spatrick   // Handle zero as a special case to protect clz
253cab2bb3Spatrick   if (a == 0)
263cab2bb3Spatrick     return fromRep(0);
273cab2bb3Spatrick 
283cab2bb3Spatrick   // Exponent of (fp_t)a is the width of abs(a).
29*810390e3Srobert   const int exponent = (aWidth - 1) - clzsi(a);
303cab2bb3Spatrick   rep_t result;
313cab2bb3Spatrick 
323cab2bb3Spatrick   // Shift a into the significand field, rounding if it is a right-shift
333cab2bb3Spatrick   if (exponent <= significandBits) {
343cab2bb3Spatrick     const int shift = significandBits - exponent;
353cab2bb3Spatrick     result = (rep_t)a << shift ^ implicitBit;
363cab2bb3Spatrick   } else {
373cab2bb3Spatrick     const int shift = exponent - significandBits;
383cab2bb3Spatrick     result = (rep_t)a >> shift ^ implicitBit;
393cab2bb3Spatrick     rep_t round = (rep_t)a << (typeWidth - shift);
403cab2bb3Spatrick     if (round > signBit)
413cab2bb3Spatrick       result++;
423cab2bb3Spatrick     if (round == signBit)
433cab2bb3Spatrick       result += result & 1;
443cab2bb3Spatrick   }
453cab2bb3Spatrick 
463cab2bb3Spatrick   // Insert the exponent
473cab2bb3Spatrick   result += (rep_t)(exponent + exponentBias) << significandBits;
483cab2bb3Spatrick   return fromRep(result);
493cab2bb3Spatrick }
503cab2bb3Spatrick 
513cab2bb3Spatrick #if defined(__ARM_EABI__)
523cab2bb3Spatrick #if defined(COMPILER_RT_ARMHF_TARGET)
__aeabi_ui2f(unsigned int a)533cab2bb3Spatrick AEABI_RTABI fp_t __aeabi_ui2f(unsigned int a) { return __floatunsisf(a); }
543cab2bb3Spatrick #else
553cab2bb3Spatrick COMPILER_RT_ALIAS(__floatunsisf, __aeabi_ui2f)
563cab2bb3Spatrick #endif
573cab2bb3Spatrick #endif
58