1 /* fp16.c
2  *
3  * Copyright 2021 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: LGPL-2.1-or-later
19  */
20 
21 #include "config.h"
22 
23 #include "fp16private.h"
24 
25 static inline guint
as_uint(const float x)26 as_uint (const float x)
27 {
28   return *(guint*)&x;
29 }
30 
31 static inline float
as_float(const guint x)32 as_float (const guint x)
33 {
34   return *(float*)&x;
35 }
36 
37 // IEEE-754 16-bit floating-point format (without infinity): 1-5-10
38 
39 static inline float
half_to_float(const guint16 x)40 half_to_float (const guint16 x)
41 {
42   const guint e = (x&0x7C00)>>10; // exponent
43   const guint m = (x&0x03FF)<<13; // mantissa
44   const guint v = as_uint((float)m)>>23;
45   return as_float((x&0x8000)<<16 | (e!=0)*((e+112)<<23|m) | ((e==0)&(m!=0))*((v-37)<<23|((m<<(150-v))&0x007FE000)));
46 }
47 
48 static inline guint16
float_to_half(const float x)49 float_to_half (const float x)
50 {
51   const guint b = as_uint(x)+0x00001000; // round-to-nearest-even
52   const guint e = (b&0x7F800000)>>23; // exponent
53   const guint m = b&0x007FFFFF; // mantissa
54   return (b&0x80000000)>>16 | (e>112)*((((e-112)<<10)&0x7C00)|m>>13) | ((e<113)&(e>101))*((((0x007FF000+m)>>(125-e))+1)>>1) | (e>143)*0x7FFF; // sign : normalized : denormalized : saturate
55 }
56 
57 void
float_to_half4_c(const float f[4],guint16 h[4])58 float_to_half4_c (const float f[4],
59                   guint16     h[4])
60 {
61   h[0] = float_to_half (f[0]);
62   h[1] = float_to_half (f[1]);
63   h[2] = float_to_half (f[2]);
64   h[3] = float_to_half (f[3]);
65 }
66 
67 void
half_to_float4_c(const guint16 h[4],float f[4])68 half_to_float4_c (const guint16 h[4],
69                   float         f[4])
70 {
71   f[0] = half_to_float (h[0]);
72   f[1] = half_to_float (h[1]);
73   f[2] = half_to_float (h[2]);
74   f[3] = half_to_float (h[3]);
75 }
76 
77 #ifdef HAVE_F16C
78 
79 #if defined(_MSC_VER) && !defined(__clang__)
80 /* based on info from https://walbourn.github.io/directxmath-f16c-and-fma/ */
81 static gboolean
have_f16c_msvc(void)82 have_f16c_msvc (void)
83 {
84   static gboolean result = FALSE;
85   static gsize inited = 0;
86 
87   if (g_once_init_enter (&inited))
88     {
89       int cpuinfo[4] = { -1 };
90 
91       __cpuid (cpuinfo, 0);
92 
93       if (cpuinfo[0] > 0)
94         {
95           __cpuid (cpuinfo, 1);
96 
97           if ((cpuinfo[2] & 0x8000000) != 0)
98             result = (cpuinfo[2] & 0x20000000) != 0;
99         }
100 
101       g_once_init_leave (&inited, 1);
102     }
103 
104   return result;
105 }
106 
107 void
float_to_half4(const float f[4],guint16 h[4])108 float_to_half4 (const float f[4], guint16 h[4])
109 {
110   if (have_f16c_msvc ())
111     float_to_half4_f16c (f, h);
112   else
113     float_to_half4_c (f, h);
114 }
115 
116 void
half_to_float4(const guint16 h[4],float f[4])117 half_to_float4 (const guint16 h[4], float f[4])
118 {
119   if (have_f16c_msvc ())
120     half_to_float4_f16c (h, f);
121   else
122     half_to_float4_c (h, f);
123 }
124 
125 #else
126 
127 void float_to_half4 (const float f[4], guint16 h[4]) __attribute__((ifunc ("resolve_float_to_half4")));
128 void half_to_float4 (const guint16 h[4], float f[4]) __attribute__((ifunc ("resolve_half_to_float4")));
129 
130 static void *
resolve_float_to_half4(void)131 resolve_float_to_half4 (void)
132 {
133   __builtin_cpu_init ();
134   if (__builtin_cpu_supports ("f16c"))
135     return float_to_half4_f16c;
136   else
137     return float_to_half4_c;
138 }
139 
140 static void *
resolve_half_to_float4(void)141 resolve_half_to_float4 (void)
142 {
143   __builtin_cpu_init ();
144   if (__builtin_cpu_supports ("f16c"))
145     return half_to_float4_f16c;
146   else
147     return half_to_float4_c;
148 }
149 
150 #endif
151 
152 #else /* ! HAVE_F16C */
153 
154 #if defined(__APPLE__) || (defined(_MSC_VER) && !defined(__clang__))
155 // turns out aliases don't work on Darwin nor Visual Studio
156 
157 void
float_to_half4(const float f[4],guint16 h[4])158 float_to_half4 (const float f[4],
159                 guint16     h[4])
160 {
161   float_to_half4_c (f, h);
162 }
163 
164 void
half_to_float4(const guint16 h[4],float f[4])165 half_to_float4 (const guint16 h[4],
166                 float         f[4])
167 {
168   half_to_float4_c (h, f);
169 }
170 
171 #else
172 
173 void float_to_half4 (const float f[4], guint16 h[4]) __attribute__((alias ("float_to_half4_c")));
174 void half_to_float4 (const guint16 h[4], float f[4]) __attribute__((alias ("half_to_float4_c")));
175 
176 #endif
177 
178 #endif  /* HAVE_F16C */
179