]>
Commit | Line | Data |
---|---|---|
5fd1486c PJ |
1 | /* Half-float conversion routines. Code mostly borrowed from the ARM's |
2 | builtin function. | |
3 | ||
99dee823 | 4 | Copyright (C) 2008-2021 Free Software Foundation, Inc. |
5fd1486c PJ |
5 | Contributed by CodeSourcery. |
6 | ||
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 | |
10 | later version. | |
11 | ||
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. | |
16 | ||
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. | |
20 | ||
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/>. */ | |
25 | ||
26 | static inline unsigned short | |
27 | __gnu_f2h_internal (unsigned int a, int ieee) | |
28 | { | |
29 | unsigned short sign = (a >> 16) & 0x8000; | |
30 | int aexp = (a >> 23) & 0xff; | |
31 | unsigned int mantissa = a & 0x007fffff; | |
32 | unsigned int mask; | |
33 | unsigned int increment; | |
34 | ||
35 | if (aexp == 0xff) | |
36 | { | |
37 | if (!ieee) | |
38 | return sign; | |
39 | if (mantissa == 0) | |
40 | return sign | 0x7c00; /* Infinity. */ | |
41 | /* Remaining cases are NaNs. Convert SNaN to QNaN. */ | |
42 | return sign | 0x7e00 | (mantissa >> 13); | |
43 | } | |
44 | ||
45 | if (aexp == 0 && mantissa == 0) | |
46 | return sign; | |
47 | ||
48 | aexp -= 127; | |
49 | ||
50 | /* Decimal point between bits 22 and 23. */ | |
51 | mantissa |= 0x00800000; | |
52 | if (aexp < -14) | |
53 | { | |
54 | mask = 0x00ffffff; | |
55 | if (aexp >= -25) | |
56 | mask >>= 25 + aexp; | |
57 | } | |
58 | else | |
59 | mask = 0x00001fff; | |
60 | ||
61 | /* Round. */ | |
62 | if (mantissa & mask) | |
63 | { | |
64 | increment = (mask + 1) >> 1; | |
65 | if ((mantissa & mask) == increment) | |
66 | increment = mantissa & (increment << 1); | |
67 | mantissa += increment; | |
68 | if (mantissa >= 0x01000000) | |
69 | { | |
70 | mantissa >>= 1; | |
71 | aexp++; | |
72 | } | |
73 | } | |
74 | ||
75 | if (ieee) | |
76 | { | |
77 | if (aexp > 15) | |
78 | return sign | 0x7c00; | |
79 | } | |
80 | else | |
81 | { | |
82 | if (aexp > 16) | |
83 | return sign | 0x7fff; | |
84 | } | |
85 | ||
86 | if (aexp < -24) | |
87 | return sign; | |
88 | ||
89 | if (aexp < -14) | |
90 | { | |
91 | mantissa >>= -14 - aexp; | |
92 | aexp = -14; | |
93 | } | |
94 | ||
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)); | |
98 | } | |
99 | ||
100 | static unsigned int | |
101 | __gnu_h2f_internal (unsigned short a, int ieee) | |
102 | { | |
103 | unsigned int sign = (unsigned int) (a & 0x8000) << 16; | |
104 | int aexp = (a >> 10) & 0x1f; | |
105 | unsigned int mantissa = a & 0x3ff; | |
106 | ||
107 | if (aexp == 0x1f && ieee) | |
108 | return sign | 0x7f800000 | (mantissa << 13); | |
109 | ||
110 | if (aexp == 0) | |
111 | { | |
112 | int shift; | |
113 | ||
114 | if (mantissa == 0) | |
115 | return sign; | |
116 | ||
117 | shift = __builtin_clz (mantissa) - 21; | |
118 | mantissa <<= shift; | |
119 | aexp = -shift; | |
120 | } | |
121 | ||
122 | return sign | (((aexp + 0x70) << 23) + (mantissa << 13)); | |
123 | } | |
124 | ||
125 | unsigned short | |
126 | __hsail_f32_to_f16 (unsigned int a) | |
127 | { | |
128 | return __gnu_f2h_internal (a, 1); | |
129 | } | |
130 | ||
131 | unsigned int | |
132 | __hsail_f16_to_f32 (unsigned short a) | |
133 | { | |
134 | return __gnu_h2f_internal (a, 1); | |
135 | } |