* gcc.target/powerpc/builtins-1-be.c <vclzb>: Rename duplicate test
[official-gcc.git] / libhsail-rt / rt / fp16.c
blobaec1bc1fdc43fbc2ca0ca09abbd437cdc6418dfa
1 /* Half-float conversion routines. Code mostly borrowed from the ARM's
2 builtin function.
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
10 later version.
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;
32 unsigned int mask;
33 unsigned int increment;
35 if (aexp == 0xff)
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);
45 if (aexp == 0 && mantissa == 0)
46 return sign;
48 aexp -= 127;
50 /* Decimal point between bits 22 and 23. */
51 mantissa |= 0x00800000;
52 if (aexp < -14)
54 mask = 0x00ffffff;
55 if (aexp >= -25)
56 mask >>= 25 + aexp;
58 else
59 mask = 0x00001fff;
61 /* Round. */
62 if (mantissa & mask)
64 increment = (mask + 1) >> 1;
65 if ((mantissa & mask) == increment)
66 increment = mantissa & (increment << 1);
67 mantissa += increment;
68 if (mantissa >= 0x01000000)
70 mantissa >>= 1;
71 aexp++;
75 if (ieee)
77 if (aexp > 15)
78 return sign | 0x7c00;
80 else
82 if (aexp > 16)
83 return sign | 0x7fff;
86 if (aexp < -24)
87 return sign;
89 if (aexp < -14)
91 mantissa >>= -14 - aexp;
92 aexp = -14;
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));
100 static unsigned int
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);
110 if (aexp == 0)
112 int shift;
114 if (mantissa == 0)
115 return sign;
117 shift = __builtin_clz (mantissa) - 21;
118 mantissa <<= shift;
119 aexp = -shift;
122 return sign | (((aexp + 0x70) << 23) + (mantissa << 13));
125 unsigned short
126 __hsail_f32_to_f16 (unsigned int a)
128 return __gnu_f2h_internal (a, 1);
131 unsigned int
132 __hsail_f16_to_f32 (unsigned short a)
134 return __gnu_h2f_internal (a, 1);