1 /* Half-float conversion routines. 2 3 Copyright (C) 2008-2016 Free Software Foundation, Inc. 4 Contributed by CodeSourcery. 5 6 This file is free software; you can redistribute it and/or modify it 7 under the terms of the GNU General Public License as published by the 8 Free Software Foundation; either version 3, or (at your option) any 9 later version. 10 11 This file is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 Under Section 7 of GPL version 3, you are granted additional 17 permissions described in the GCC Runtime Library Exception, version 18 3.1, as published by the Free Software Foundation. 19 20 You should have received a copy of the GNU General Public License and 21 a copy of the GCC Runtime Library Exception along with this program; 22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 <http://www.gnu.org/licenses/>. */ 24 25 static inline unsigned short 26 __gnu_f2h_internal(unsigned int a, int ieee) 27 { 28 unsigned short sign = (a >> 16) & 0x8000; 29 int aexp = (a >> 23) & 0xff; 30 unsigned int mantissa = a & 0x007fffff; 31 unsigned int mask; 32 unsigned int increment; 33 34 if (aexp == 0xff) 35 { 36 if (!ieee) 37 return sign; 38 if (mantissa == 0) 39 return sign | 0x7c00; /* Infinity. */ 40 /* Remaining cases are NaNs. Convert SNaN to QNaN. */ 41 return sign | 0x7e00 | (mantissa >> 13); 42 } 43 44 if (aexp == 0 && mantissa == 0) 45 return sign; 46 47 aexp -= 127; 48 49 /* Decimal point between bits 22 and 23. */ 50 mantissa |= 0x00800000; 51 if (aexp < -14) 52 { 53 mask = 0x00ffffff; 54 if (aexp >= -25) 55 mask >>= 25 + aexp; 56 } 57 else 58 mask = 0x00001fff; 59 60 /* Round. */ 61 if (mantissa & mask) 62 { 63 increment = (mask + 1) >> 1; 64 if ((mantissa & mask) == increment) 65 increment = mantissa & (increment << 1); 66 mantissa += increment; 67 if (mantissa >= 0x01000000) 68 { 69 mantissa >>= 1; 70 aexp++; 71 } 72 } 73 74 if (ieee) 75 { 76 if (aexp > 15) 77 return sign | 0x7c00; 78 } 79 else 80 { 81 if (aexp > 16) 82 return sign | 0x7fff; 83 } 84 85 if (aexp < -24) 86 return sign; 87 88 if (aexp < -14) 89 { 90 mantissa >>= -14 - aexp; 91 aexp = -14; 92 } 93 94 /* We leave the leading 1 in the mantissa, and subtract one 95 from the exponent bias to compensate. */ 96 return sign | (((aexp + 14) << 10) + (mantissa >> 13)); 97 } 98 99 unsigned int 100 __gnu_h2f_internal(unsigned short a, int ieee) 101 { 102 unsigned int sign = (unsigned int)(a & 0x8000) << 16; 103 int aexp = (a >> 10) & 0x1f; 104 unsigned int mantissa = a & 0x3ff; 105 106 if (aexp == 0x1f && ieee) 107 return sign | 0x7f800000 | (mantissa << 13); 108 109 if (aexp == 0) 110 { 111 int shift; 112 113 if (mantissa == 0) 114 return sign; 115 116 shift = __builtin_clz(mantissa) - 21; 117 mantissa <<= shift; 118 aexp = -shift; 119 } 120 121 return sign | (((aexp + 0x70) << 23) + (mantissa << 13)); 122 } 123 124 unsigned short 125 __gnu_f2h_ieee(unsigned int a) 126 { 127 return __gnu_f2h_internal(a, 1); 128 } 129 130 unsigned int 131 __gnu_h2f_ieee(unsigned short a) 132 { 133 return __gnu_h2f_internal(a, 1); 134 } 135 136 unsigned short 137 __gnu_f2h_alternative(unsigned int x) 138 { 139 return __gnu_f2h_internal(x, 0); 140 } 141 142 unsigned int 143 __gnu_h2f_alternative(unsigned short a) 144 { 145 return __gnu_h2f_internal(a, 0); 146 } 147