Class: Fixnum

Inherits:
Object show all
Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#byte_swap_64Object

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.

Parameters:

  • self (in)

    the bignum that needs byte swapping

Returns:

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



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_16Object

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.

Parameters:

  • self (in)

    the 16-bit unsigned short to make signed

Returns:

  • the 16-bit signed equivalent



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_32Object

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

  1. This will only be called if the number being converted is smaller than

the fixnum max.

Parameters:

  • self (in)

    the 32-bit unsigned long to make signed

Returns:

  • the 32-bit signed equivalent



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