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, HFP_DOUBLE, HFP_EXTENDED, HFP_SINGLE, 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, MBF_EXTENDEND, 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?, normalized?, 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



1613
1614
1615
1616
1617
1618
1619
1620
# File 'lib/float-formats/classes.rb', line 1613

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



1706
1707
1708
# File 'lib/float-formats/classes.rb', line 1706

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

.fields_radixObject



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

def self.fields_radix
  exponent_radix
end

.minus_sign_valueObject



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

def self.minus_sign_value
  (-1) % 2
end

.pack(s, m, e) ⇒ Object



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
1695
1696
1697
1698
1699
# File 'lib/float-formats/classes.rb', line 1662

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:



1624
1625
1626
# File 'lib/float-formats/classes.rb', line 1624

def self.radix
  16
end

.total_bitsObject



1631
1632
1633
# File 'lib/float-formats/classes.rb', line 1631

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

.total_bytesObject



1634
1635
1636
# File 'lib/float-formats/classes.rb', line 1634

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

.unpack(v) ⇒ Object



1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
# File 'lib/float-formats/classes.rb', line 1639

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