Class: Bignum

Inherits:
Object show all
Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#byte_swap_64Object

Byte-swaps a 64-bit bignum. Bignum arithmetic is quite, quite slow. By implementing this in C, we save ourselves innumerable cycles.

Parameters:

  • self (in)

    the bignum that needs byte swapping

Returns:

  • the byte-swapped bignum (or not swapped if host is big-endian)



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_16Object

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.

Parameters:

  • self (in)

    the 16-bit unsigned short to make signed

Returns:

  • the 16-bit signed equivalent



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_32Object

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.

Parameters:

  • self (in)

    the 32-bit unsigned long to make signed

Returns:

  • the 32-bit signed equivalent



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;
}