Class: Flt::HexadecimalFormat

Inherits:
FieldsInBitsFormatBase show all
Defined in:
lib/float-formats/classes.rb

Overview

Hexadecimal floating point format

Constant Summary

Constants included from Flt

APPLE, IEEE_128, IEEE_128_BE, IEEE_DEC128, IEEE_DEC32, IEEE_DEC64, IEEE_DOUBLE, IEEE_D_BE, IEEE_EXTENDED, IEEE_HALF, IEEE_H_BE, IEEE_QUAD, IEEE_Q_BE, IEEE_SINGLE, IEEE_S_BE, IEEE_X_BE, IEEE_binaryx, RPL, RPL_X

Instance Attribute Summary

Attributes inherited from FormatBase

#exponent, #sign, #significand

Class Method Summary collapse

Methods inherited from FieldsInBitsFormatBase

pack_fields, unpack_fields

Methods inherited from FormatBase

#<=>, arithmetic, arithmetic_type, bias, canonicalized, context, #convert_to, decimal_digits_necessary, decimal_digits_stored, decimal_max_exp, decimal_min_exp, endianness, epsilon, #form_class, #fp_format, from, from_bits, from_bits_text, from_bytes, from_hex, from_number, from_text, gradual_underflow?, half_epsilon, #infinite?, infinity, #initialize, join, max_value, maximum_integral_significand, min_normalized_value, min_value, minimum_normalized_integral_significand, #minus, nan, #nan?, #next_minus, #next_plus, #normal?, num, num_class, numerals_conversion, pack_fields_hash, radix_log, radix_log10, radix_max_exp, radix_min_exp, radix_power, rounding_mode, sign_from_unit, sign_to_unit, #split, strict_epsilon, #subnormal?, switch_sign_value, #to, #to_a, #to_bits, #to_bits_text, #to_bytes, #to_hex, #to_num, #to_text, #ulp, unpack_fields_hash, zero, #zero?

Methods included from Flt

#*, #+, #-, #-@, #/, bcd2dpd, bitnot, convert_bytes, dbl_from_float, dbl_from_text, dbl_to_float, dpd2bcd, dpd_to_hexbcd, float_bin, float_dec, float_from_integral_sign_significand_exponent, float_from_integral_significand_exponent, float_shortest_dec, float_significant_dec, float_to_integral_sign_significand_exponent, float_to_integral_significand_exponent, hex_from_float, hex_to_float, hexbcd_to_dpd, sgl_from_float, sgl_from_text, sgl_to_float

Constructor Details

This class inherits a constructor from Flt::FormatBase

Class Method Details

.define(params) ⇒ Object



1608
1609
1610
1611
1612
1613
1614
1615
# File 'lib/float-formats/classes.rb', line 1608

def self.define(params)
  @hidden_bit = false
  @splitted_fields_supported = true
  define_fields params[:fields]
  @significand_digits = @fields[:significand]/4
  params[:exponent_radix] = 2
  super  params
end

.exponent_digitsObject



1701
1702
1703
# File 'lib/float-formats/classes.rb', line 1701

def self.exponent_digits
  @fields[exponent]/4
end

.fields_radixObject



1622
1623
1624
# File 'lib/float-formats/classes.rb', line 1622

def self.fields_radix
  exponent_radix
end

.minus_sign_valueObject



1697
1698
1699
# File 'lib/float-formats/classes.rb', line 1697

def self.minus_sign_value
  (-1) % 2
end

.pack(s, m, e) ⇒ Object



1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
# File 'lib/float-formats/classes.rb', line 1657

def self.pack(s,m,e)
  msb = radix_power(@significand_digits-1)

  if e==:zero
    e = @zero_encoded_exp
    m = 0
  elsif e==:infinity
    e = @infinite_encoded_exp || radix_power(@fields[:exponent])-1
    m = 0
  elsif e==:nan
    e = @nan_encoded_exp || radix_power(@fields[:exponent])-1
    s = minus_sign_value # ?
    m = radix_power(@significand_digits-2) if m==0
  elsif e==:denormal
    e = @denormal_encoded_exp || @zero_encoded_exp
  else
    # TODO: try to adjust m to keep e in range if out of valid range
    # TODO: reduce m and adjust e if m too big

    min_exp = radix_min_exp(:integral_significand)
    if m>0
      while m<msb && e>min_exp
        e -= 1
        m <<= 4 # m *= radix
      end
    end
    if m<msb && @denormal_encoded_exp
      e = @denormal_encoded_exp
    elsif m==0 # => && @denormal_encoded_exp.nil?
      e = 0
    else
      e = encode_exponent(e, :integral_significand)
    end
  end
  s = sign_from_unit(s)
  m,e = neg_significand_exponent(0,m,e) if s%2==1
  pack_fields_hash sign: s, significand: m, exponent: e
end

.radixObject

:stopdoc:



1619
1620
1621
# File 'lib/float-formats/classes.rb', line 1619

def self.radix
  16
end

.total_bitsObject



1626
1627
1628
# File 'lib/float-formats/classes.rb', line 1626

def self.total_bits
  @field_lengths.inject{|x,y| x+y}
end

.total_bytesObject



1629
1630
1631
# File 'lib/float-formats/classes.rb', line 1629

def self.total_bytes
  (total_bits+7)/8
end

.unpack(v) ⇒ Object



1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
# File 'lib/float-formats/classes.rb', line 1634

def self.unpack(v)
  f = unpack_fields_hash(v)
  m = f[:significand]
  e = f[:exponent]
  s = f[:sign]
  m,e = neg_significand_exponent(s,m,e) if s%2==1
  if m==0
    # +-zero
    e = :zero
  elsif @infinite_encoded_exp && e==@infinite_encoded_exp && m==0
    # +-inifinity
    e = :infinity
  elsif @nan_encoded_exp && e==@nan_encoded_exp && m!=0
    # NaN
    e = :nan
  else
    # normalized number
    e = decode_exponent(e, :integral_significand)
  end
  s = sign_to_unit(s)
  [s,m,e]
end