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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +46 -0
- data/README.md +454 -0
- data/ext/libbin/data_types.c +1055 -284
- data/ext/libbin/data_types.h +32 -1
- data/ext/libbin/extconf.rb +15 -0
- data/ext/libbin/half.c +110 -65
- data/ext/libbin/half.h +2 -0
- data/ext/libbin/libbin_c.c +2066 -420
- data/ext/libbin/libbin_c.h +48 -1
- data/ext/libbin/libbin_io.h +506 -0
- data/ext/libbin/pghalf.c +107 -228
- data/ext/libbin/pghalf.h +2 -0
- data/lib/libbin/bitfield.rb +426 -0
- data/lib/libbin/data_shape.rb +387 -0
- data/lib/libbin/data_types.rb +264 -511
- data/lib/libbin/enum.rb +159 -0
- data/lib/libbin/union.rb +314 -0
- data/lib/libbin/utf16.rb +183 -0
- data/lib/libbin/version.rb +4 -0
- data/lib/libbin.rb +165 -14
- data/libbin.gemspec +23 -4
- metadata +46 -5
data/ext/libbin/data_types.h
CHANGED
|
@@ -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
|
data/ext/libbin/extconf.rb
CHANGED
|
@@ -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
|
-
|
|
271
|
-
|
|
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
|
|
341
|
-
const
|
|
342
|
-
const uint32_t
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
const uint32_t
|
|
347
|
-
const uint32_t
|
|
348
|
-
const
|
|
349
|
-
const uint32_t
|
|
350
|
-
const uint32_t
|
|
351
|
-
|
|
352
|
-
const uint32_t
|
|
353
|
-
const uint32_t
|
|
354
|
-
const uint32_t
|
|
355
|
-
const uint32_t
|
|
356
|
-
|
|
357
|
-
const uint32_t
|
|
358
|
-
const uint32_t
|
|
359
|
-
const
|
|
360
|
-
const uint32_t
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
const
|
|
375
|
-
const
|
|
376
|
-
const
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
const
|
|
381
|
-
const
|
|
382
|
-
const
|
|
383
|
-
const uint32_t
|
|
384
|
-
const
|
|
385
|
-
|
|
386
|
-
const
|
|
387
|
-
const
|
|
388
|
-
const
|
|
389
|
-
const
|
|
390
|
-
|
|
391
|
-
const
|
|
392
|
-
const
|
|
393
|
-
const
|
|
394
|
-
const
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
return (uint16_t)(
|
|
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
|
|