libbin 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/libbin/pghalf.c CHANGED
@@ -42,6 +42,7 @@
42
42
  // guaranteed for future versions.
43
43
  //
44
44
 
45
+ #include <string.h>
45
46
  #include "pghalf.h"
46
47
 
47
48
  // Load immediate
@@ -56,17 +57,13 @@ static inline uint32_t _uint32_dec( uint32_t a )
56
57
  return (a - 1);
57
58
  }
58
59
 
59
- // Increment
60
- static inline uint32_t _uint32_inc( uint32_t a )
61
- {
62
- return (a + 1);
63
- }
64
-
60
+ #ifndef __GNUC__
65
61
  // Complement
66
62
  static inline uint32_t _uint32_not( uint32_t a )
67
63
  {
68
64
  return (~a);
69
65
  }
66
+ #endif
70
67
 
71
68
  // Negate
72
69
  static inline uint32_t _uint32_neg( uint32_t a )
@@ -86,12 +83,6 @@ static inline uint32_t _uint32_and( uint32_t a, uint32_t b )
86
83
  return (a & b);
87
84
  }
88
85
 
89
- // Exclusive Or
90
- static inline uint32_t _uint32_xor( uint32_t a, uint32_t b )
91
- {
92
- return (a ^ b);
93
- }
94
-
95
86
  // And with Complement
96
87
  static inline uint32_t _uint32_andc( uint32_t a, uint32_t b )
97
88
  {
@@ -104,11 +95,13 @@ static inline uint32_t _uint32_or( uint32_t a, uint32_t b )
104
95
  return (a | b);
105
96
  }
106
97
 
98
+ #ifndef __GNUC__
107
99
  // Shift Right Logical
108
100
  static inline uint32_t _uint32_srl( uint32_t a, int sa )
109
101
  {
110
102
  return (a >> sa);
111
103
  }
104
+ #endif
112
105
 
113
106
  // Shift Left Logical
114
107
  static inline uint32_t _uint32_sll( uint32_t a, int sa )
@@ -128,12 +121,6 @@ static inline uint32_t _uint32_sub( uint32_t a, uint32_t b )
128
121
  return (a - b);
129
122
  }
130
123
 
131
- // Multiply
132
- static inline uint32_t _uint32_mul( uint32_t a, uint32_t b )
133
- {
134
- return (a * b);
135
- }
136
-
137
124
  // Select on Sign bit
138
125
  static inline uint32_t _uint32_sels( uint32_t test, uint32_t a, uint32_t b )
139
126
  {
@@ -145,133 +132,13 @@ static inline uint32_t _uint32_sels( uint32_t test, uint32_t a, uint32_t b )
145
132
  return (result);
146
133
  }
147
134
 
148
- // Select Bits on mask
149
- static inline uint32_t _uint32_selb( uint32_t mask, uint32_t a, uint32_t b )
150
- {
151
- const uint32_t sel_a = _uint32_and( a, mask );
152
- const uint32_t sel_b = _uint32_andc( b, mask );
153
- const uint32_t result = _uint32_or( sel_a, sel_b );
154
-
155
- return (result);
156
- }
157
-
158
- // Load Immediate
159
- static inline uint16_t _uint16_li( uint16_t a )
160
- {
161
- return (a);
162
- }
163
-
164
- // Extend sign
165
- static inline uint16_t _uint16_ext( uint16_t a )
166
- {
167
- return (((int16_t)a)>>15);
168
- }
169
-
170
- // Negate
171
- static inline uint16_t _uint16_neg( uint16_t a )
172
- {
173
- return (-a);
174
- }
175
-
176
- // Complement
177
- static inline uint16_t _uint16_not( uint16_t a )
178
- {
179
- return (~a);
180
- }
181
-
182
- // Decrement
183
- static inline uint16_t _uint16_dec( uint16_t a )
184
- {
185
- return (a - 1);
186
- }
187
-
188
- // Shift Left Logical
189
- static inline uint16_t _uint16_sll( uint16_t a, int sa )
190
- {
191
- return (a << sa);
192
- }
193
-
194
- // Shift Right Logical
195
- static inline uint16_t _uint16_srl( uint16_t a, int sa )
196
- {
197
- return (a >> sa);
198
- }
199
-
200
- // Add
201
- static inline uint16_t _uint16_add( uint16_t a, uint16_t b )
202
- {
203
- return (a + b);
204
- }
205
-
206
- // Subtract
207
- static inline uint16_t _uint16_sub( uint16_t a, uint16_t b )
208
- {
209
- return (a - b);
210
- }
211
-
212
- // And
213
- static inline uint16_t _uint16_and( uint16_t a, uint16_t b )
214
- {
215
- return (a & b);
216
- }
217
-
218
- // Or
219
- static inline uint16_t _uint16_or( uint16_t a, uint16_t b )
220
- {
221
- return (a | b);
222
- }
223
-
224
- // Exclusive Or
225
- static inline uint16_t _uint16_xor( uint16_t a, uint16_t b )
226
- {
227
- return (a ^ b);
228
- }
229
-
230
- // And with Complement
231
- static inline uint16_t _uint16_andc( uint16_t a, uint16_t b )
232
- {
233
- return (a & ~b);
234
- }
235
-
236
- // And then Shift Right Logical
237
- static inline uint16_t _uint16_andsrl( uint16_t a, uint16_t b, int sa )
238
- {
239
- return ((a & b) >> sa);
240
- }
241
-
242
- // Shift Right Logical then Mask
243
- static inline uint16_t _uint16_srlm( uint16_t a, int sa, uint16_t mask )
244
- {
245
- return ((a >> sa) & mask);
246
- }
247
-
248
- // Add then Mask
249
- static inline uint16_t _uint16_addm( uint16_t a, uint16_t b, uint16_t mask )
250
- {
251
- return ((a + b) & mask);
252
- }
253
-
254
-
255
- // Select on Sign bit
256
- static inline uint16_t _uint16_sels( uint16_t test, uint16_t a, uint16_t b )
257
- {
258
- const uint16_t mask = _uint16_ext( test );
259
- const uint16_t sel_a = _uint16_and( a, mask );
260
- const uint16_t sel_b = _uint16_andc( b, mask );
261
- const uint16_t result = _uint16_or( sel_a, sel_b );
262
-
263
- return (result);
264
- }
265
-
266
135
  // Count Leading Zeros
267
136
  static inline uint32_t _uint32_cntlz( uint32_t x )
268
137
  {
269
138
  #ifdef __GNUC__
270
- /* NOTE: __builtin_clz is undefined for x == 0 */
271
- /* On PowerPC, this will map to insn: cntlzw */
272
- /* On Pentium, this will map to insn: clz */
273
- uint32_t is_x_nez_msb = _uint32_neg( x );
274
- uint32_t nlz = __builtin_clz( x );
139
+ /* NOTE: __builtin_clz is undefined for x == 0; x | 1u avoids UB branchlessly */
140
+ uint32_t is_x_nez_msb = _uint32_or( x, _uint32_neg( x ) );
141
+ uint32_t nlz = (uint32_t)__builtin_clz( x | 1u );
275
142
  uint32_t result = _uint32_sels( is_x_nez_msb, nlz, 0x00000020 );
276
143
  return (result);
277
144
  #else
@@ -305,99 +172,111 @@ static inline uint32_t _uint32_cntlz( uint32_t x )
305
172
  #endif
306
173
  }
307
174
 
308
- // Count Leading Zeros
309
- static inline uint16_t _uint16_cntlz( uint16_t x )
175
+ static inline uint32_t _sel32( uint32_t cond, uint32_t a, uint32_t b )
310
176
  {
311
- #ifdef __GNUC__
312
- uint16_t nlz32 = (uint16_t)_uint32_cntlz( (uint32_t)x );
313
- uint32_t nlz = _uint32_sub( nlz32, 16 );
314
- return (nlz);
315
- #else
316
- const uint16_t x0 = _uint16_srl( x, 1 );
317
- const uint16_t x1 = _uint16_or( x, x0 );
318
- const uint16_t x2 = _uint16_srl( x1, 2 );
319
- const uint16_t x3 = _uint16_or( x1, x2 );
320
- const uint16_t x4 = _uint16_srl( x3, 4 );
321
- const uint16_t x5 = _uint16_or( x3, x4 );
322
- const uint16_t x6 = _uint16_srl( x5, 8 );
323
- const uint16_t x7 = _uint16_or( x5, x6 );
324
- const uint16_t x8 = _uint16_not( x7 );
325
- const uint16_t x9 = _uint16_srlm( x8, 1, 0x5555 );
326
- const uint16_t xA = _uint16_sub( x8, x9 );
327
- const uint16_t xB = _uint16_and( xA, 0x3333 );
328
- const uint16_t xC = _uint16_srlm( xA, 2, 0x3333 );
329
- const uint16_t xD = _uint16_add( xB, xC );
330
- const uint16_t xE = _uint16_srl( xD, 4 );
331
- const uint16_t xF = _uint16_addm( xD, xE, 0x0f0f );
332
- const uint16_t x10 = _uint16_srl( xF, 8 );
333
- const uint16_t x11 = _uint16_addm( xF, x10, 0x001f );
334
- return ( x11 );
335
- #endif
177
+ const uint32_t mask = -(uint32_t)(cond != 0);
178
+ return (a & mask) | (b & ~mask);
179
+ }
180
+
181
+ static inline uint64_t _sel64( uint64_t cond, uint64_t a, uint64_t b )
182
+ {
183
+ const uint64_t mask = -(uint64_t)(cond != 0);
184
+ return (a & mask) | (b & ~mask);
336
185
  }
337
186
 
338
187
  uint16_t
339
188
  pghalf_from_float( uint32_t f )
340
189
  {
341
- const uint32_t one = _uint32_li( 0x00000001 );
342
- const uint32_t f_s_mask = _uint32_li( 0x80000000 ); //bit 31
343
- const uint32_t f_e_mask = _uint32_li( 0x7f800000 ); //bits 30-23
344
- const uint32_t f_m_mask = _uint32_li( 0x007fffff ); //bits 22-0
345
- const uint32_t f_m_hidden_bit = _uint32_li( 0x00800000 ); //1<<f_e_pos
346
- const uint32_t f_m_round_bit = _uint32_li( 0x00002000 ); //1<<(f_e_pos - h_e_pos - 1)
347
- const uint32_t f_snan_mask = _uint32_li( 0x7fc00000 ); //f_e_mask + 1 << (f_e_pos - 1)
348
- const uint32_t f_e_pos = _uint32_li( 0x00000017 ); //23
349
- const uint32_t h_e_pos = _uint32_li( 0x00000009 ); //9
350
- const uint32_t h_e_mask = _uint32_li( 0x00007e00 ); //bits 14-9
351
- const uint32_t h_snan_mask = _uint32_li( 0x00007f00 ); //h_e_mask + 1 << (h_e_pos - 1)
352
- const uint32_t h_e_mask_value = _uint32_li( 0x0000003f ); //h_e_mask >> 9
353
- const uint32_t f_h_s_pos_offset = _uint32_li( 0x00000010 ); //f_s_pos - h_s_pos
354
- const uint32_t f_h_bias_offset = _uint32_li( 0x00000050 ); //f_bias - h_bias
355
- const uint32_t f_h_m_pos_offset = _uint32_li( 0x0000000e ); //f_e_pos - h_e_pos
356
- const uint32_t h_nan_min = _uint32_li( 0x00007e01 ); //h_e_mask + 1
357
- const uint32_t f_h_e_biased_flag = _uint32_li( 0x000000af ); //f_bias + h_bias + 1
358
- const uint32_t f_s = _uint32_and( f, f_s_mask );
359
- const uint32_t f_e = _uint32_and( f, f_e_mask );
360
- const uint16_t h_s = _uint32_srl( f_s, f_h_s_pos_offset );
361
- const uint32_t f_m = _uint32_and( f, f_m_mask );
362
- const uint16_t f_e_amount = _uint32_srl( f_e, f_e_pos );
363
- const uint32_t f_e_half_bias = _uint32_sub( f_e_amount, f_h_bias_offset );
364
- const uint32_t f_snan = _uint32_and( f, f_snan_mask );
365
- const uint32_t f_m_round_mask = _uint32_and( f_m, f_m_round_bit );
366
- const uint32_t f_m_round_offset = _uint32_sll( f_m_round_mask, one );
367
- const uint32_t f_m_rounded = _uint32_add( f_m, f_m_round_offset );
368
- const uint32_t f_m_denorm_sa = _uint32_sub( one, f_e_half_bias );
369
- const uint32_t f_m_with_hidden = _uint32_or( f_m_rounded, f_m_hidden_bit );
370
- const uint32_t f_m_denorm = _uint32_srl( f_m_with_hidden, f_m_denorm_sa );
371
- const uint32_t h_m_denorm = _uint32_srl( f_m_denorm, f_h_m_pos_offset );
372
- const uint32_t f_m_rounded_overflow = _uint32_and( f_m_rounded, f_m_hidden_bit );
373
- const uint32_t m_nan = _uint32_srl( f_m, f_h_m_pos_offset );
374
- const uint32_t h_em_nan = _uint32_or( h_e_mask, m_nan );
375
- const uint32_t h_e_norm_overflow_offset = _uint32_inc( f_e_half_bias );
376
- const uint32_t h_e_norm_overflow = _uint32_sll( h_e_norm_overflow_offset, h_e_pos );
377
- const uint32_t h_e_norm = _uint32_sll( f_e_half_bias, h_e_pos );
378
- const uint32_t h_m_norm = _uint32_srl( f_m_rounded, f_h_m_pos_offset );
379
- const uint32_t h_em_norm = _uint32_or( h_e_norm, h_m_norm );
380
- const uint32_t is_h_ndenorm_msb = _uint32_sub( f_h_bias_offset, f_e_amount );
381
- const uint32_t is_f_e_flagged_msb = _uint32_sub( f_h_e_biased_flag, f_e_half_bias );
382
- const uint32_t is_h_denorm_msb = _uint32_not( is_h_ndenorm_msb );
383
- const uint32_t is_f_m_eqz_msb = _uint32_dec( f_m );
384
- const uint32_t is_h_nan_eqz_msb = _uint32_dec( m_nan );
385
- const uint32_t is_f_inf_msb = _uint32_and( is_f_e_flagged_msb, is_f_m_eqz_msb );
386
- const uint32_t is_f_nan_underflow_msb = _uint32_and( is_f_e_flagged_msb, is_h_nan_eqz_msb );
387
- const uint32_t is_e_overflow_msb = _uint32_sub( h_e_mask_value, f_e_half_bias );
388
- const uint32_t is_h_inf_msb = _uint32_or( is_e_overflow_msb, is_f_inf_msb );
389
- const uint32_t is_f_nsnan_msb = _uint32_sub( f_snan, f_snan_mask );
390
- const uint32_t is_m_norm_overflow_msb = _uint32_neg( f_m_rounded_overflow );
391
- const uint32_t is_f_snan_msb = _uint32_not( is_f_nsnan_msb );
392
- const uint32_t h_em_overflow_result = _uint32_sels( is_m_norm_overflow_msb, h_e_norm_overflow, h_em_norm );
393
- const uint32_t h_em_nan_result = _uint32_sels( is_f_e_flagged_msb, h_em_nan, h_em_overflow_result );
394
- const uint32_t h_em_nan_underflow_result = _uint32_sels( is_f_nan_underflow_msb, h_nan_min, h_em_nan_result );
395
- const uint32_t h_em_inf_result = _uint32_sels( is_h_inf_msb, h_e_mask, h_em_nan_underflow_result );
396
- const uint32_t h_em_denorm_result = _uint32_sels( is_h_denorm_msb, h_m_denorm, h_em_inf_result );
397
- const uint32_t h_em_snan_result = _uint32_sels( is_f_snan_msb, h_snan_mask, h_em_denorm_result );
398
- const uint32_t h_result = _uint32_or( h_s, h_em_snan_result );
399
-
400
- return (uint16_t)(h_result);
190
+ const uint32_t sign = (f >> 16) & 0x8000u;
191
+ const int32_t f_exp = (int32_t)((f >> 23) & 0xffu);
192
+ const uint32_t f_mant = f & 0x007fffffu;
193
+ const int32_t h_exp = (f_exp - 127) + 47;
194
+
195
+ /* Unified branchless normal + subnormal IEEE-754 round-to-nearest-even */
196
+ const uint32_t is_sub = (uint32_t)(h_exp <= 0);
197
+ const uint32_t sig = 0x00800000u | f_mant;
198
+ const int32_t raw_sh = (int32_t)_sel32(is_sub, (uint32_t)(15 - h_exp), 14u);
199
+ const uint32_t shift = _sel32((uint32_t)(raw_sh > 24), 24u, (uint32_t)raw_sh);
200
+ const uint32_t exp_b = _sel32(is_sub, 0u, (uint32_t)((h_exp - 1) & 0x7fff) << 9);
201
+
202
+ const uint32_t mant = sig >> shift;
203
+ const uint32_t rem = sig & ((1u << shift) - 1u);
204
+ const uint32_t half_b = 1u << (shift - 1u);
205
+ const uint32_t inc = (uint32_t)((rem > half_b) | ((rem == half_b) & (mant & 1u)));
206
+
207
+ const uint32_t is_special = (uint32_t)(f_exp == 0xff);
208
+ const uint32_t is_nan = is_special & (uint32_t)(f_mant != 0);
209
+ const uint32_t is_inf = (is_special & (uint32_t)(f_mant == 0)) | (uint32_t)(h_exp >= 63);
210
+ const uint32_t is_zero = (uint32_t)((f_exp == 0) | (raw_sh > 24));
211
+
212
+ uint32_t res = _sel32(is_zero, 0u, exp_b + mant + inc);
213
+ res = _sel32(is_inf, 0x7e00u, res);
214
+ res = _sel32(is_nan, 0x7f00u, res);
215
+ return (uint16_t)(sign | res);
216
+ }
217
+
218
+ uint16_t
219
+ pghalf_from_double( double d )
220
+ {
221
+ uint64_t u;
222
+ memcpy(&u, &d, sizeof(u));
223
+
224
+ const uint64_t sign = (u >> 48) & 0x8000u;
225
+ const int32_t d_exp = (int32_t)((u >> 52) & 0x7ffu);
226
+ const uint64_t d_mant = u & 0x000fffffffffffffull;
227
+ const int32_t h_exp = (d_exp - 1023) + 47;
228
+
229
+ /* Unified branchless single-step IEEE-754 round-to-nearest-even (no double rounding) */
230
+ const uint64_t is_sub = (uint64_t)(h_exp <= 0);
231
+ const uint64_t sig = 0x0010000000000000ull | d_mant;
232
+ const int32_t raw_sh = (int32_t)_sel64(is_sub, (uint64_t)(44 - h_exp), 43ull);
233
+ const uint32_t shift = (uint32_t)_sel64((uint64_t)(raw_sh > 53), 53ull, (uint64_t)raw_sh);
234
+ const uint64_t exp_b = _sel64(is_sub, 0ull, (uint64_t)((h_exp - 1) & 0x7fff) << 9);
235
+
236
+ const uint64_t mant = sig >> shift;
237
+ const uint64_t rem = sig & ((1ull << shift) - 1ull);
238
+ const uint64_t half_b = 1ull << (shift - 1u);
239
+ const uint64_t inc = (uint64_t)((rem > half_b) | ((rem == half_b) & (mant & 1ull)));
240
+
241
+ const uint64_t is_special = (uint64_t)(d_exp == 0x7ff);
242
+ const uint64_t is_nan = is_special & (uint64_t)(d_mant != 0);
243
+ const uint64_t is_inf = (is_special & (uint64_t)(d_mant == 0)) | (uint64_t)(h_exp >= 63);
244
+ const uint64_t is_zero = (uint64_t)((d_exp == 0) | (raw_sh > 53));
245
+
246
+ uint64_t res = _sel64(is_zero, 0ull, exp_b + mant + inc);
247
+ res = _sel64(is_inf, 0x7e00u, res);
248
+ res = _sel64(is_nan, 0x7f00u, res);
249
+ return (uint16_t)(sign | res);
250
+ }
251
+
252
+ double
253
+ pghalf_to_double( uint16_t h )
254
+ {
255
+ const uint64_t sign = (uint64_t)(h & 0x8000u) << 48;
256
+ const uint32_t h_exp = (h >> 9) & 0x3fu;
257
+ const uint32_t h_mant = h & 0x01ffu;
258
+
259
+ const uint64_t norm_bits = ((uint64_t)(h_exp + (1023u - 47u)) << 52) | ((uint64_t)h_mant << 43);
260
+
261
+ const uint32_t nlz = _uint32_cntlz(h_mant | 1u);
262
+ const uint32_t shift = nlz - 22u; /* in [1, 9] for all h_mant in [0, 511] */
263
+ const uint32_t sub_m = (h_mant << shift) & 0x01ffu;
264
+ const uint64_t sub_exp = (uint64_t)(1023 - 46 - (int32_t)shift);
265
+ const uint64_t sub_bits = (sub_exp << 52) | ((uint64_t)sub_m << 43);
266
+
267
+ const uint64_t is_exp_zero = (uint64_t)(h_exp == 0);
268
+ const uint64_t is_exp_max = (uint64_t)(h_exp == 0x3fu);
269
+ const uint64_t is_mant_zero = (uint64_t)(h_mant == 0);
270
+
271
+ const uint64_t zero_or_sub = _sel64(is_mant_zero, 0ull, sub_bits);
272
+ const uint64_t inf_or_nan = _sel64(is_mant_zero, 0x7ff0000000000000ull,
273
+ 0x7ff8000000000000ull | ((uint64_t)h_mant << 43));
274
+ const uint64_t d_bits = sign | _sel64(is_exp_max, inf_or_nan,
275
+ _sel64(is_exp_zero, zero_or_sub, norm_bits));
276
+
277
+ double d;
278
+ memcpy(&d, &d_bits, sizeof(d));
279
+ return d;
401
280
  }
402
281
 
403
282
  uint32_t
data/ext/libbin/pghalf.h CHANGED
@@ -5,5 +5,7 @@
5
5
 
6
6
  uint32_t pghalf_to_float( uint16_t h );
7
7
  uint16_t pghalf_from_float( uint32_t f );
8
+ double pghalf_to_double( uint16_t h );
9
+ uint16_t pghalf_from_double( double d );
8
10
 
9
11
  #endif /* PGHALF_H */