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.
@@ -1,16 +1,47 @@
1
1
  #ifndef DATA_TYPES_H__
2
2
  #define DATA_TYPES_H__
3
3
 
4
+ #include "./libbin_io.h"
5
+
4
6
  union float_u {
5
7
  float f;
6
8
  uint32_t i;
7
9
  };
8
10
 
11
+ typedef enum {
12
+ SCALAR_NONE = 0,
13
+ SCALAR_HALF, SCALAR_HALF_LE, SCALAR_HALF_BE,
14
+ SCALAR_PGHALF, SCALAR_PGHALF_LE, SCALAR_PGHALF_BE,
15
+ SCALAR_INT8, SCALAR_INT8_LE, SCALAR_INT8_BE,
16
+ SCALAR_UINT8, SCALAR_UINT8_LE, SCALAR_UINT8_BE,
17
+ SCALAR_INT16, SCALAR_INT16_LE, SCALAR_INT16_BE,
18
+ SCALAR_UINT16, SCALAR_UINT16_LE, SCALAR_UINT16_BE,
19
+ SCALAR_INT32, SCALAR_INT32_LE, SCALAR_INT32_BE,
20
+ SCALAR_UINT32, SCALAR_UINT32_LE, SCALAR_UINT32_BE,
21
+ SCALAR_INT64, SCALAR_INT64_LE, SCALAR_INT64_BE,
22
+ SCALAR_UINT64, SCALAR_UINT64_LE, SCALAR_UINT64_BE,
23
+ SCALAR_FLT, SCALAR_FLT_LE, SCALAR_FLT_BE,
24
+ SCALAR_DOUBLE, SCALAR_DOUBLE_LE, SCALAR_DOUBLE_BE,
25
+ SCALAR_STR,
26
+ SCALAR_ZSTR,
27
+ SCALAR_SKIP,
28
+ SCALAR_ALIGN_TO,
29
+ SCALAR_COUNT
30
+ } libbin_scalar_kind_t;
31
+
9
32
  extern VALUE cScalar;
10
33
  extern VALUE cDataShape;
11
34
  extern VALUE cDataRange;
12
35
  extern VALUE cStructure;
36
+ extern ID id_default_big_p;
37
+ extern int libbin_scalar_overridden;
13
38
 
14
- void define_cScalar();
39
+ void define_cScalar(void);
40
+ void libbin_copy_bytes(libbin_io_t *in_io, libbin_io_t *out_io, size_t cnt);
41
+ libbin_scalar_kind_t libbin_lookup_scalar_kind(VALUE klass);
42
+ size_t libbin_scalar_elem_size(libbin_scalar_kind_t kind);
43
+ VALUE libbin_scalar_load_fast(libbin_scalar_kind_t kind, libbin_io_t *io, int input_big, VALUE length);
44
+ void libbin_scalar_dump_fast(libbin_scalar_kind_t kind, VALUE value, libbin_io_t *io, int output_big, VALUE length);
45
+ VALUE libbin_scalar_convert_fast(libbin_scalar_kind_t kind, libbin_io_t *in_io, libbin_io_t *out_io, int input_big, int output_big, VALUE length);
15
46
 
16
47
  #endif
@@ -1,3 +1,18 @@
1
1
  require 'mkmf'
2
2
 
3
+ have_func('rb_str_capacity', 'ruby.h')
4
+ have_func('rb_gc_mark_movable', 'ruby.h')
5
+ have_func('rb_gc_location', 'ruby.h')
6
+ have_func('rb_ext_ractor_safe', 'ruby.h')
7
+ if have_header('ruby/ractor.h')
8
+ have_func('rb_ractor_make_shareable', %w[ruby.h ruby/ractor.h])
9
+ have_func('rb_ractor_shareable_p', %w[ruby.h ruby/ractor.h])
10
+ have_func('rb_ractor_local_storage_value_newkey', %w[ruby.h ruby/ractor.h])
11
+ end
12
+ if have_header('ruby/io/buffer.h')
13
+ have_func('rb_io_buffer_get_bytes', 'ruby/io/buffer.h')
14
+ have_func('rb_io_buffer_get_bytes_for_reading', 'ruby/io/buffer.h')
15
+ have_func('rb_io_buffer_get_bytes_for_writing', 'ruby/io/buffer.h')
16
+ end
17
+
3
18
  create_makefile("libbin_c")
data/ext/libbin/half.c CHANGED
@@ -41,6 +41,7 @@
41
41
  // guaranteed for future versions.
42
42
  //
43
43
 
44
+ #include <string.h>
44
45
  #include "half.h"
45
46
 
46
47
  // Load immediate
@@ -172,11 +173,13 @@ static inline uint16_t _uint16_neg( uint16_t a )
172
173
  return (-a);
173
174
  }
174
175
 
176
+ #ifndef __GNUC__
175
177
  // Complement
176
178
  static inline uint16_t _uint16_not( uint16_t a )
177
179
  {
178
180
  return (~a);
179
181
  }
182
+ #endif
180
183
 
181
184
  // Decrement
182
185
  static inline uint16_t _uint16_dec( uint16_t a )
@@ -244,11 +247,13 @@ static inline uint16_t _uint16_srlm( uint16_t a, int sa, uint16_t mask )
244
247
  return ((a >> sa) & mask);
245
248
  }
246
249
 
250
+ #ifndef __GNUC__
247
251
  // Add then Mask
248
252
  static inline uint16_t _uint16_addm( uint16_t a, uint16_t b, uint16_t mask )
249
253
  {
250
254
  return ((a + b) & mask);
251
255
  }
256
+ #endif
252
257
 
253
258
 
254
259
  // Select on Sign bit
@@ -266,11 +271,9 @@ static inline uint16_t _uint16_sels( uint16_t test, uint16_t a, uint16_t b )
266
271
  static inline uint32_t _uint32_cntlz( uint32_t x )
267
272
  {
268
273
  #ifdef __GNUC__
269
- /* NOTE: __builtin_clz is undefined for x == 0 */
270
- /* On PowerPC, this will map to insn: cntlzw */
271
- /* On Pentium, this will map to insn: clz */
272
- uint32_t is_x_nez_msb = _uint32_neg( x );
273
- uint32_t nlz = __builtin_clz( x );
274
+ /* NOTE: __builtin_clz is undefined for x == 0; x | 1u avoids UB branchlessly */
275
+ uint32_t is_x_nez_msb = _uint32_or( x, _uint32_neg( x ) );
276
+ uint32_t nlz = (uint32_t)__builtin_clz( x | 1u );
274
277
  uint32_t result = _uint32_sels( is_x_nez_msb, nlz, 0x00000020 );
275
278
  return (result);
276
279
  #else
@@ -334,69 +337,111 @@ static inline uint16_t _uint16_cntlz( uint16_t x )
334
337
  #endif
335
338
  }
336
339
 
340
+ static inline uint32_t _sel32( uint32_t cond, uint32_t a, uint32_t b )
341
+ {
342
+ const uint32_t mask = -(uint32_t)(cond != 0);
343
+ return (a & mask) | (b & ~mask);
344
+ }
345
+
346
+ static inline uint64_t _sel64( uint64_t cond, uint64_t a, uint64_t b )
347
+ {
348
+ const uint64_t mask = -(uint64_t)(cond != 0);
349
+ return (a & mask) | (b & ~mask);
350
+ }
351
+
337
352
  uint16_t
338
353
  half_from_float( uint32_t f )
339
354
  {
340
- const uint32_t one = _uint32_li( 0x00000001 );
341
- const uint32_t f_s_mask = _uint32_li( 0x80000000 );
342
- const uint32_t f_e_mask = _uint32_li( 0x7f800000 );
343
- const uint32_t f_m_mask = _uint32_li( 0x007fffff );
344
- const uint32_t f_m_hidden_bit = _uint32_li( 0x00800000 );
345
- const uint32_t f_m_round_bit = _uint32_li( 0x00001000 );
346
- const uint32_t f_snan_mask = _uint32_li( 0x7fc00000 );
347
- const uint32_t f_e_pos = _uint32_li( 0x00000017 );
348
- const uint32_t h_e_pos = _uint32_li( 0x0000000a );
349
- const uint32_t h_e_mask = _uint32_li( 0x00007c00 );
350
- const uint32_t h_snan_mask = _uint32_li( 0x00007e00 );
351
- const uint32_t h_e_mask_value = _uint32_li( 0x0000001f );
352
- const uint32_t f_h_s_pos_offset = _uint32_li( 0x00000010 );
353
- const uint32_t f_h_bias_offset = _uint32_li( 0x00000070 );
354
- const uint32_t f_h_m_pos_offset = _uint32_li( 0x0000000d );
355
- const uint32_t h_nan_min = _uint32_li( 0x00007c01 );
356
- const uint32_t f_h_e_biased_flag = _uint32_li( 0x0000008f );
357
- const uint32_t f_s = _uint32_and( f, f_s_mask );
358
- const uint32_t f_e = _uint32_and( f, f_e_mask );
359
- const uint16_t h_s = _uint32_srl( f_s, f_h_s_pos_offset );
360
- const uint32_t f_m = _uint32_and( f, f_m_mask );
361
- const uint16_t f_e_amount = _uint32_srl( f_e, f_e_pos );
362
- const uint32_t f_e_half_bias = _uint32_sub( f_e_amount, f_h_bias_offset );
363
- const uint32_t f_snan = _uint32_and( f, f_snan_mask );
364
- const uint32_t f_m_round_mask = _uint32_and( f_m, f_m_round_bit );
365
- const uint32_t f_m_round_offset = _uint32_sll( f_m_round_mask, one );
366
- const uint32_t f_m_rounded = _uint32_add( f_m, f_m_round_offset );
367
- const uint32_t f_m_denorm_sa = _uint32_sub( one, f_e_half_bias );
368
- const uint32_t f_m_with_hidden = _uint32_or( f_m_rounded, f_m_hidden_bit );
369
- const uint32_t f_m_denorm = _uint32_srl( f_m_with_hidden, f_m_denorm_sa );
370
- const uint32_t h_m_denorm = _uint32_srl( f_m_denorm, f_h_m_pos_offset );
371
- const uint32_t f_m_rounded_overflow = _uint32_and( f_m_rounded, f_m_hidden_bit );
372
- const uint32_t m_nan = _uint32_srl( f_m, f_h_m_pos_offset );
373
- const uint32_t h_em_nan = _uint32_or( h_e_mask, m_nan );
374
- const uint32_t h_e_norm_overflow_offset = _uint32_inc( f_e_half_bias );
375
- const uint32_t h_e_norm_overflow = _uint32_sll( h_e_norm_overflow_offset, h_e_pos );
376
- const uint32_t h_e_norm = _uint32_sll( f_e_half_bias, h_e_pos );
377
- const uint32_t h_m_norm = _uint32_srl( f_m_rounded, f_h_m_pos_offset );
378
- const uint32_t h_em_norm = _uint32_or( h_e_norm, h_m_norm );
379
- const uint32_t is_h_ndenorm_msb = _uint32_sub( f_h_bias_offset, f_e_amount );
380
- const uint32_t is_f_e_flagged_msb = _uint32_sub( f_h_e_biased_flag, f_e_half_bias );
381
- const uint32_t is_h_denorm_msb = _uint32_not( is_h_ndenorm_msb );
382
- const uint32_t is_f_m_eqz_msb = _uint32_dec( f_m );
383
- const uint32_t is_h_nan_eqz_msb = _uint32_dec( m_nan );
384
- const uint32_t is_f_inf_msb = _uint32_and( is_f_e_flagged_msb, is_f_m_eqz_msb );
385
- const uint32_t is_f_nan_underflow_msb = _uint32_and( is_f_e_flagged_msb, is_h_nan_eqz_msb );
386
- const uint32_t is_e_overflow_msb = _uint32_sub( h_e_mask_value, f_e_half_bias );
387
- const uint32_t is_h_inf_msb = _uint32_or( is_e_overflow_msb, is_f_inf_msb );
388
- const uint32_t is_f_nsnan_msb = _uint32_sub( f_snan, f_snan_mask );
389
- const uint32_t is_m_norm_overflow_msb = _uint32_neg( f_m_rounded_overflow );
390
- const uint32_t is_f_snan_msb = _uint32_not( is_f_nsnan_msb );
391
- const uint32_t h_em_overflow_result = _uint32_sels( is_m_norm_overflow_msb, h_e_norm_overflow, h_em_norm );
392
- const uint32_t h_em_nan_result = _uint32_sels( is_f_e_flagged_msb, h_em_nan, h_em_overflow_result );
393
- const uint32_t h_em_nan_underflow_result = _uint32_sels( is_f_nan_underflow_msb, h_nan_min, h_em_nan_result );
394
- const uint32_t h_em_inf_result = _uint32_sels( is_h_inf_msb, h_e_mask, h_em_nan_underflow_result );
395
- const uint32_t h_em_denorm_result = _uint32_sels( is_h_denorm_msb, h_m_denorm, h_em_inf_result );
396
- const uint32_t h_em_snan_result = _uint32_sels( is_f_snan_msb, h_snan_mask, h_em_denorm_result );
397
- const uint32_t h_result = _uint32_or( h_s, h_em_snan_result );
398
-
399
- return (uint16_t)(h_result);
355
+ const uint32_t sign = (f >> 16) & 0x8000u;
356
+ const int32_t f_exp = (int32_t)((f >> 23) & 0xffu);
357
+ const uint32_t f_mant = f & 0x007fffffu;
358
+ const int32_t h_exp = (f_exp - 127) + 15;
359
+
360
+ /* Unified branchless normal + subnormal IEEE-754 round-to-nearest-even */
361
+ const uint32_t is_sub = (uint32_t)(h_exp <= 0);
362
+ const uint32_t sig = 0x00800000u | f_mant;
363
+ const int32_t raw_sh = (int32_t)_sel32(is_sub, (uint32_t)(14 - h_exp), 13u);
364
+ const uint32_t shift = _sel32((uint32_t)(raw_sh > 24), 24u, (uint32_t)raw_sh);
365
+ const uint32_t exp_b = _sel32(is_sub, 0u, (uint32_t)((h_exp - 1) & 0x7fff) << 10);
366
+
367
+ const uint32_t mant = sig >> shift;
368
+ const uint32_t rem = sig & ((1u << shift) - 1u);
369
+ const uint32_t half_b = 1u << (shift - 1u);
370
+ const uint32_t inc = (uint32_t)((rem > half_b) | ((rem == half_b) & (mant & 1u)));
371
+
372
+ const uint32_t is_special = (uint32_t)(f_exp == 0xff);
373
+ const uint32_t is_nan = is_special & (uint32_t)(f_mant != 0);
374
+ const uint32_t is_inf = (is_special & (uint32_t)(f_mant == 0)) | (uint32_t)(h_exp >= 31);
375
+ const uint32_t is_zero = (uint32_t)((f_exp == 0) | (raw_sh > 24));
376
+
377
+ uint32_t res = _sel32(is_zero, 0u, exp_b + mant + inc);
378
+ res = _sel32(is_inf, 0x7c00u, res);
379
+ res = _sel32(is_nan, 0x7e00u, res);
380
+ return (uint16_t)(sign | res);
381
+ }
382
+
383
+ uint16_t
384
+ half_from_double( double d )
385
+ {
386
+ uint64_t u;
387
+ memcpy(&u, &d, sizeof(u));
388
+
389
+ const uint64_t sign = (u >> 48) & 0x8000u;
390
+ const int32_t d_exp = (int32_t)((u >> 52) & 0x7ffu);
391
+ const uint64_t d_mant = u & 0x000fffffffffffffull;
392
+ const int32_t h_exp = (d_exp - 1023) + 15;
393
+
394
+ /* Unified branchless single-step IEEE-754 round-to-nearest-even (no double rounding) */
395
+ const uint64_t is_sub = (uint64_t)(h_exp <= 0);
396
+ const uint64_t sig = 0x0010000000000000ull | d_mant;
397
+ const int32_t raw_sh = (int32_t)_sel64(is_sub, (uint64_t)(43 - h_exp), 42ull);
398
+ const uint32_t shift = (uint32_t)_sel64((uint64_t)(raw_sh > 53), 53ull, (uint64_t)raw_sh);
399
+ const uint64_t exp_b = _sel64(is_sub, 0ull, (uint64_t)((h_exp - 1) & 0x7fff) << 10);
400
+
401
+ const uint64_t mant = sig >> shift;
402
+ const uint64_t rem = sig & ((1ull << shift) - 1ull);
403
+ const uint64_t half_b = 1ull << (shift - 1u);
404
+ const uint64_t inc = (uint64_t)((rem > half_b) | ((rem == half_b) & (mant & 1ull)));
405
+
406
+ const uint64_t is_special = (uint64_t)(d_exp == 0x7ff);
407
+ const uint64_t is_nan = is_special & (uint64_t)(d_mant != 0);
408
+ const uint64_t is_inf = (is_special & (uint64_t)(d_mant == 0)) | (uint64_t)(h_exp >= 31);
409
+ const uint64_t is_zero = (uint64_t)((d_exp == 0) | (raw_sh > 53));
410
+
411
+ uint64_t res = _sel64(is_zero, 0ull, exp_b + mant + inc);
412
+ res = _sel64(is_inf, 0x7c00u, res);
413
+ res = _sel64(is_nan, 0x7e00u, res);
414
+ return (uint16_t)(sign | res);
415
+ }
416
+
417
+ double
418
+ half_to_double( uint16_t h )
419
+ {
420
+ const uint64_t sign = (uint64_t)(h & 0x8000u) << 48;
421
+ const uint32_t h_exp = (h >> 10) & 0x1fu;
422
+ const uint32_t h_mant = h & 0x03ffu;
423
+
424
+ const uint64_t norm_bits = ((uint64_t)(h_exp + (1023u - 15u)) << 52) | ((uint64_t)h_mant << 42);
425
+
426
+ const uint32_t nlz = _uint32_cntlz(h_mant | 1u);
427
+ const uint32_t shift = nlz - 21u; /* in [1, 10] for all h_mant in [0, 1023] */
428
+ const uint32_t sub_m = (h_mant << shift) & 0x03ffu;
429
+ const uint64_t sub_exp = (uint64_t)(1023 - 14 - (int32_t)shift);
430
+ const uint64_t sub_bits = (sub_exp << 52) | ((uint64_t)sub_m << 42);
431
+
432
+ const uint64_t is_exp_zero = (uint64_t)(h_exp == 0);
433
+ const uint64_t is_exp_max = (uint64_t)(h_exp == 0x1fu);
434
+ const uint64_t is_mant_zero = (uint64_t)(h_mant == 0);
435
+
436
+ const uint64_t zero_or_sub = _sel64(is_mant_zero, 0ull, sub_bits);
437
+ const uint64_t inf_or_nan = _sel64(is_mant_zero, 0x7ff0000000000000ull,
438
+ 0x7ff8000000000000ull | ((uint64_t)h_mant << 42));
439
+ const uint64_t d_bits = sign | _sel64(is_exp_max, inf_or_nan,
440
+ _sel64(is_exp_zero, zero_or_sub, norm_bits));
441
+
442
+ double d;
443
+ memcpy(&d, &d_bits, sizeof(d));
444
+ return d;
400
445
  }
401
446
 
402
447
  uint32_t
data/ext/libbin/half.h CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
  uint32_t half_to_float( uint16_t h );
7
7
  uint16_t half_from_float( uint32_t f );
8
+ double half_to_double( uint16_t h );
9
+ uint16_t half_from_double( double d );
8
10
  uint16_t half_add( uint16_t arg0, uint16_t arg1 );
9
11
  uint16_t half_mul( uint16_t arg0, uint16_t arg1 );
10
12