Class: Integer

Inherits:
Object show all
Defined in:
lib/amp/dependencies/amp_support/ruby_amp_support.rb,
lib/amp/support/support.rb

Overview

Ruby versions of slow functions we’ve implemented in C

Instance Method Summary collapse

Instance Method Details

#byte_swap_64Integer

Used for byte-swapping a 64-bit double long. Unfortuantely, this will invoke bignum logic, which is ridiculously slow. That’s why we have a C extension.

If the system is little endian, we work some magic. If the system is big endian, we just return self.

Returns:

  • (Integer)

    the number swapped as if it were a 64-bit integer



14
15
16
17
18
19
20
21
22
23
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 14

def byte_swap_64
  if Amp::Support::SYSTEM[:endian] == :little
    ((self >> 56))                        | ((self & 0x00FF000000000000) >> 40) |
      ((self & 0x0000FF0000000000) >> 24) | ((self & 0x000000FF00000000) >> 8 ) |
      ((self & 0x00000000FF000000) << 8 ) | ((self & 0x0000000000FF0000) << 24) |
      ((self & 0x000000000000FF00) << 40) | ((self & 0x00000000000000FF) << 56)
  else
    self
  end
end

#bytesObject Also known as: byte, b

methods for converting between file sizes



601
602
603
# File 'lib/amp/support/support.rb', line 601

def bytes
  self
end

#gigabytesObject Also known as: gigabyte, gb

methods for converting between file sizes



622
623
624
# File 'lib/amp/support/support.rb', line 622

def gigabytes
  1024 * megabytes
end

#kilobytesObject Also known as: kilobyte, kb

methods for converting between file sizes



608
609
610
# File 'lib/amp/support/support.rb', line 608

def kilobytes
  1024 * bytes
end

#megabytesObject Also known as: megabyte, mb

methods for converting between file sizes



615
616
617
# File 'lib/amp/support/support.rb', line 615

def megabytes
  1024 * kilobytes
end

#to_dirstate_symbolObject

Converts an ascii value to a dirstate status symbol. Converts a fixnum, which is an ascii value, to a symbol representing a dirstate entry’s status. Since we don’t like passing around ‘n’, and want to pass around :normal, we need a fast lookup for ascii value -> symbol. The price we pay.

Parameters:

  • self (in)

    the integer ascii value to convert

Returns:

  • a symbol representation of the dirstate status



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 52

def to_dirstate_symbol
  case self
  when 110 # "n".ord
    :normal
  when 63  # "?".ord
    :untracked
  when 97  # "a".ord
    :added
  when 109 # "m".ord
    :merged
  when 114 # "r".ord
    :removed
  else
    raise "No known hg value for #{self}"
  end
end

#to_signed(bits) ⇒ Object

Forces this integer to be negative if it’s supposed to be!

Parameters:

  • bits (Fixnum)

    the number of bits to use - signed shorts are different from signed longs!



634
635
636
637
638
# File 'lib/amp/support/support.rb', line 634

def to_signed(bits)
  return to_signed_16 if bits == 16
  return to_signed_32 if bits == 32
  raise "Unexpected number of bits: #{bits}"
end

#to_signed_16Integer

Returns the number as if it were a signed 16-bit integer. Since unpack() always returns unsigned integers, we have to sign them here.

Returns:

  • (Integer)

    the number overflowed if it would overflow as a 16-bit integer



30
31
32
33
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 30

def to_signed_16
  return self if self < 32785
  return self - 65536
end

#to_signed_32Integer

Returns the number as if it were a signed 32-bit integer. Since unpack() always returns unsigned integers, we have to sign them here.

Returns:

  • (Integer)

    the number overflowed if it would overflow as a 32-bit integer



40
41
42
43
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 40

def to_signed_32
  return self if self < 2147483648
  return self - 4294967296
end