Stephen Canon | b6d4e2e | 2010-07-03 00:56:03 +0000 | [diff] [blame] | 1 | //===-- lib/floatunsisf.c - uint -> single-precision conversion ---*- C -*-===// |
Stephen Canon | 09009c5 | 2010-07-02 23:05:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | 9ad441f | 2010-11-16 22:13:33 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Stephen Canon | 09009c5 | 2010-07-02 23:05:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements unsigned integer to single-precision conversion for the |
| 11 | // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even |
| 12 | // mode. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define SINGLE_PRECISION |
| 17 | #include "fp_lib.h" |
| 18 | |
Anton Korobeynikov | 37b97d1 | 2011-04-19 17:51:24 +0000 | [diff] [blame] | 19 | #include "int_lib.h" |
| 20 | |
Chandler Carruth | 0193b74 | 2012-06-22 21:09:15 +0000 | [diff] [blame] | 21 | ARM_EABI_FNALIAS(ui2f, floatunsisf) |
Anton Korobeynikov | 37b97d1 | 2011-04-19 17:51:24 +0000 | [diff] [blame] | 22 | |
Stephen Canon | b6d4e2e | 2010-07-03 00:56:03 +0000 | [diff] [blame] | 23 | fp_t __floatunsisf(unsigned int a) { |
Stephen Canon | 09009c5 | 2010-07-02 23:05:46 +0000 | [diff] [blame] | 24 | |
| 25 | const int aWidth = sizeof a * CHAR_BIT; |
| 26 | |
| 27 | // Handle zero as a special case to protect clz |
| 28 | if (a == 0) return fromRep(0); |
| 29 | |
| 30 | // Exponent of (fp_t)a is the width of abs(a). |
| 31 | const int exponent = (aWidth - 1) - __builtin_clz(a); |
| 32 | rep_t result; |
| 33 | |
| 34 | // Shift a into the significand field, rounding if it is a right-shift |
| 35 | if (exponent <= significandBits) { |
| 36 | const int shift = significandBits - exponent; |
| 37 | result = (rep_t)a << shift ^ implicitBit; |
| 38 | } else { |
| 39 | const int shift = exponent - significandBits; |
| 40 | result = (rep_t)a >> shift ^ implicitBit; |
| 41 | rep_t round = (rep_t)a << (typeWidth - shift); |
| 42 | if (round > signBit) result++; |
| 43 | if (round == signBit) result += result & 1; |
| 44 | } |
| 45 | |
| 46 | // Insert the exponent |
| 47 | result += (rep_t)(exponent + exponentBias) << significandBits; |
| 48 | return fromRep(result); |
| 49 | } |