module BSON::Decimal128::Builder

Helper module for parsing String, Integer, Float, BigDecimal, and Decimal128 objects into other objects.

@api private

@since 4.2.0

Constants

INFINITY_MASK

Infinity mask.

@since 4.2.0

NAN_MASK

NaN mask.

@since 4.2.0

SIGN_BIT_MASK

Signed bit mask.

@since 4.2.0

SNAN_MASK

SNaN mask.

@since 4.2.0

TWO_HIGHEST_BITS_SET

The two highest bits of the 64 high order bits.

@since 4.2.0

Public Instance Methods

parts_to_bits(significand, exponent, is_negative) click to toggle source

Convert parts representing a Decimal128 into the corresponding bits.

@param [ Integer ] significand The significand. @param [ Integer ] exponent The exponent. @param [ true, false ] is_negative Whether the value is negative.

@return [ Array ] Tuple of the low and high bits.

@since 4.2.0

# File lib/bson/decimal128/builder.rb, line 62
def parts_to_bits(significand, exponent, is_negative)
  validate_range!(exponent, significand)
  exponent = exponent + Decimal128::EXPONENT_OFFSET
  high = significand >> 64
  low = (high << 64) ^ significand

  if high >> 49 == 1
    high = high & 0x7fffffffffff
    high |= TWO_HIGHEST_BITS_SET
    high |= (exponent & 0x3fff) << 47
  else
    high |= exponent << 49
  end

  if is_negative
    high |= SIGN_BIT_MASK
  end


  [ low, high ]
end

Private Instance Methods

valid_exponent?(exponent) click to toggle source
# File lib/bson/decimal128/builder.rb, line 96
def valid_exponent?(exponent)
  exponent <= Decimal128::MAX_EXPONENT && exponent >= Decimal128::MIN_EXPONENT
end
valid_significand?(significand) click to toggle source
# File lib/bson/decimal128/builder.rb, line 92
def valid_significand?(significand)
  significand.to_s.length <= Decimal128::MAX_DIGITS_OF_PRECISION
end
validate_range!(exponent, significand) click to toggle source
# File lib/bson/decimal128/builder.rb, line 86
def validate_range!(exponent, significand)
  unless valid_significand?(significand) && valid_exponent?(exponent)
    raise Decimal128::InvalidRange.new
  end
end