1 /* Half-float conversion routines. Code mostly borrowed from the ARM's
4 Copyright (C) 2008-2018 Free Software Foundation, Inc.
5 Contributed by CodeSourcery.
7 This file is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 This file is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 static inline unsigned short
27 __gnu_f2h_internal (unsigned int a
, int ieee
)
29 unsigned short sign
= (a
>> 16) & 0x8000;
30 int aexp
= (a
>> 23) & 0xff;
31 unsigned int mantissa
= a
& 0x007fffff;
33 unsigned int increment
;
40 return sign
| 0x7c00; /* Infinity. */
41 /* Remaining cases are NaNs. Convert SNaN to QNaN. */
42 return sign
| 0x7e00 | (mantissa
>> 13);
45 if (aexp
== 0 && mantissa
== 0)
50 /* Decimal point between bits 22 and 23. */
51 mantissa
|= 0x00800000;
64 increment
= (mask
+ 1) >> 1;
65 if ((mantissa
& mask
) == increment
)
66 increment
= mantissa
& (increment
<< 1);
67 mantissa
+= increment
;
68 if (mantissa
>= 0x01000000)
91 mantissa
>>= -14 - aexp
;
95 /* We leave the leading 1 in the mantissa, and subtract one
96 from the exponent bias to compensate. */
97 return sign
| (((aexp
+ 14) << 10) + (mantissa
>> 13));
101 __gnu_h2f_internal (unsigned short a
, int ieee
)
103 unsigned int sign
= (unsigned int) (a
& 0x8000) << 16;
104 int aexp
= (a
>> 10) & 0x1f;
105 unsigned int mantissa
= a
& 0x3ff;
107 if (aexp
== 0x1f && ieee
)
108 return sign
| 0x7f800000 | (mantissa
<< 13);
117 shift
= __builtin_clz (mantissa
) - 21;
122 return sign
| (((aexp
+ 0x70) << 23) + (mantissa
<< 13));
126 __hsail_f32_to_f16 (unsigned int a
)
128 return __gnu_f2h_internal (a
, 1);
132 __hsail_f16_to_f32 (unsigned short a
)
134 return __gnu_h2f_internal (a
, 1);