Module: LibBin

Defined in:
lib/libbin.rb,
lib/libbin/data_types.rb,
ext/libbin/libbin_c.c

Overview

Container module, handles the global state default endianness and error output.

Defined Under Namespace

Modules: RangeRefinement Classes: DataRange, DataShape, Structure

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.bigObject

Returns the value of attribute big.



12
13
14
# File 'lib/libbin.rb', line 12

def big
  @big
end

.outputObject

Returns the value of attribute output.



13
14
15
# File 'lib/libbin.rb', line 13

def output
  @output
end

Class Method Details

.default_big?Boolean

Returns true the default endianness is big

Returns:

  • (Boolean)


17
18
19
# File 'lib/libbin.rb', line 17

def self.default_big?
  @big
end

.half_from_string(str, unpack_str) ⇒ Float

Load (unpacks) a half precision floating point.

Examples:

# Read a half precision floating point value stored in 'str' in little endian fashion
value = half_from_string(str, "S<")

Parameters:

  • str (String)

    the strin to read the number from

  • unpack_str (String)

    the unpack format of the underlying 16bit integer

Returns:

  • (Float)

    the ruby representation of the value



1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
# File 'ext/libbin/libbin_c.c', line 1788

static VALUE half_from_string_p(VALUE self, VALUE str, VALUE pack_str) {
  (void)self;
  Check_Type(str, T_STRING);
  Check_Type(pack_str, T_STRING);
  VALUE arr = rb_funcall(str, rb_intern("unpack"), 1, pack_str);
  uint16_t val = NUM2USHORT(rb_funcall(arr, rb_intern("first"), 0));
  union float_u res;

  res.i = half_to_float(val);
  return DBL2NUM(res.f);
}

.half_to_string(value, pack_str) ⇒ String

Convert a Numeric value to a half precision floating point and pack it into a string.

Examples:

# Stores a number as a half precision floating point value in a big endian fashion
str = half_to_string(value, "S>")

Parameters:

  • value (Numeric)

    the number to convert

  • pack_str (String)

    the pack format to store the underlying 16 bit integer representation of the value

Returns:

  • (String)

    the packed half precision value



1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
# File 'ext/libbin/libbin_c.c', line 1813

static VALUE half_to_string_p(VALUE self, VALUE number, VALUE pack_str) {
  (void)self;
  Check_Type(number, T_FLOAT);
  union float_u val;
  uint16_t res;

  val.f = NUM2DBL(number);
  res = half_from_float(val.i);
  VALUE arr = rb_ary_new3(1, UINT2NUM(res) );

  return rb_funcall(arr, rb_intern("pack"), 1, pack_str);
}

.pghalf_from_string(str, unpack_str) ⇒ Float

Load (unpacks) a half precision floating point as used by PlatinumGames in some formats.

Examples:

# Read a half precision floating point value stored in 'str' in little endian fashion
half_from_string(str, "S<")

Parameters:

  • str (String)

    the strin to read the number from

  • unpack_str (String)

    the unpack format of the underlying 16bit integer

Returns:

  • (Float)

    the ruby representation of the value



1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
# File 'ext/libbin/libbin_c.c', line 1776

static VALUE pghalf_from_string_p(VALUE self, VALUE str, VALUE pack_str) {
  (void)self;
  Check_Type(str, T_STRING);
  Check_Type(pack_str, T_STRING);
  VALUE arr = rb_funcall(str, rb_intern("unpack"), 1, pack_str);
  uint16_t val = NUM2USHORT(rb_funcall(arr, rb_intern("first"), 0));
  union float_u res;

  res.i = pghalf_to_float(val);
  return DBL2NUM(res.f);
}

.pghalf_to_string(value, pack_str) ⇒ String

Convert a Numeric value to a half precision floating point as used by PlatinumGames in some formats, and pack it into a string.

Examples:

# Stores a number as a half precision floating point value in a big endian fashion
str = half_to_string(value, "S>")

Parameters:

  • value (Numeric)

    the number to convert

  • pack_str (String)

    the pack format to store the underlying 16 bit integer representation of the value

Returns:

  • (String)

    the packed half precision value



1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
# File 'ext/libbin/libbin_c.c', line 1800

static VALUE pghalf_to_string_p(VALUE self, VALUE number, VALUE pack_str) {
  (void)self;
  Check_Type(number, T_FLOAT);
  union float_u val;
  uint16_t res;

  val.f = NUM2DBL(number);
  res = pghalf_from_float(val.i);
  VALUE arr = rb_ary_new3(1, UINT2NUM(res) );

  return rb_funcall(arr, rb_intern("pack"), 1, pack_str);
}