Class: Fixnum
Instance Method Summary collapse
-
#byte_swap_64 ⇒ Object
Byte-swaps a 64-bit fixnum.
-
#to_signed_16 ⇒ Object
Converts an unsigned, 16-bit fixnum to a signed, 16-bit value.
-
#to_signed_32 ⇒ Object
Converts a fixnum to a signed, 32-bit value.
Instance Method Details
#byte_swap_64 ⇒ Object
Byte-swaps a 64-bit fixnum. Bignum arithmetic is quite, quite slow. By implementing this in C, we save ourselves innumerable cycles. Also, our result will usually end up being a bignum, even though we start as a fixnum.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'ext/amp/support/support.c', line 45
static VALUE amp_fixnum_byte_swap_64(VALUE self) {
VALUE result = self;
if (little_endian) {
uint64_t val = (uint64_t)FIX2ULONG(self);
val = (((val >> 56)) | ((val & 0x00FF000000000000ll) >> 40) |
((val & 0x0000FF0000000000ll) >> 24) | ((val & 0x000000FF00000000ll) >> 8) |
((val & 0x00000000FF000000ll) << 8 ) | ((val & 0x0000000000FF0000ll) << 24) |
((val & 0x000000000000FF00ll) << 40) | ((val & 0x00000000000000FFll) << 56));
result = rb_ull2inum(val);
}
return result;
}
|
#to_signed_16 ⇒ Object
Converts an unsigned, 16-bit fixnum to a signed, 16-bit value. Converts an unsigned, 16-bit number to its signed equivalent. Since Ruby doesn’t have signed values readily available, this is much much faster in C.
67 68 69 70 71 |
# File 'ext/amp/support/support.c', line 67
static VALUE amp_fixnum_to_signed_16(VALUE self) {
signed short val = (int16_t)FIX2INT(self);
VALUE result = rb_int_new(val);
return result;
}
|
#to_signed_32 ⇒ Object
Converts a fixnum 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
-
This will only be called if the number being converted is smaller than
the fixnum max.
83 84 85 86 |
# File 'ext/amp/support/support.c', line 83
static VALUE amp_fixnum_to_signed_32(VALUE self) {
VALUE result = rb_int_new((int32_t)FIX2LONG(self));
return result;
}
|