Class: Bignum
Instance Method Summary collapse
-
#byte_swap_64 ⇒ Object
Byte-swaps a 64-bit bignum.
-
#to_signed_16 ⇒ Object
Converts a bignum to a signed, 16-bit value.
-
#to_signed_32 ⇒ Object
Converts a bignum to a signed, 32-bit value.
Instance Method Details
#byte_swap_64 ⇒ Object
Byte-swaps a 64-bit bignum. Bignum arithmetic is quite, quite slow. By implementing this in C, we save ourselves innumerable cycles.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'ext/amp/support/support.c', line 96 static VALUE amp_bignum_byte_swap_64(VALUE self) { VALUE result = self; if (little_endian) { uint64_t val = rb_big2ull(self); val = (((val >> 56)) | ((val & 0x00FF000000000000ull) >> 40) | ((val & 0x0000FF0000000000ull) >> 24) | ((val & 0x000000FF00000000ull) >> 8) | ((val & 0x00000000FF000000) << 8 ) | ((val & 0x0000000000FF0000) << 24) | ((val & 0x000000000000FF00) << 40) | ((val & 0x00000000000000FF) << 56)); result = rb_ull2inum(val); } return result; } |
#to_signed_16 ⇒ Object
Converts a bignum to a signed, 16-bit value. Converts an unsigned, 16-bit number to its signed equivalent. This should actually never be called, because bignums shouldn’t ever be used for 16-bit values. However, it’s provided just to be safe. Since Ruby doesn’t have signed values readily available, this is much much faster in C.
134 135 136 137 138 |
# File 'ext/amp/support/support.c', line 134 static VALUE amp_bignum_to_signed_16(VALUE self) { signed short val = (int16_t)rb_big2ulong(self); // cut off bytes VALUE result = rb_int_new(val); return result; } |
#to_signed_32 ⇒ Object
Converts a bignum to a signed, 32-bit value. Converts an unsigned, 32-bit number to its signed equivalent. Since Ruby doesn’t have signed values readily available, this is much much faster in C.
118 119 120 121 |
# File 'ext/amp/support/support.c', line 118 static VALUE amp_bignum_to_signed_32(VALUE self) { VALUE result = rb_int_new((int32_t)rb_big2ulong(self)); return result; } |