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, nio_read_neutral, #nio_write_neutral, #normal?, num, num_class, 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



1564
1565
1566
1567
1568
1569
1570
1571
# File 'lib/float-formats/classes.rb', line 1564

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



1657
1658
1659
# File 'lib/float-formats/classes.rb', line 1657

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

.fields_radixObject



1578
1579
1580
# File 'lib/float-formats/classes.rb', line 1578

def self.fields_radix
  exponent_radix
end

.minus_sign_valueObject



1653
1654
1655
# File 'lib/float-formats/classes.rb', line 1653

def self.minus_sign_value
  (-1) % 2
end

.pack(s, m, e) ⇒ Object



1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
# File 'lib/float-formats/classes.rb', line 1613

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:



1575
1576
1577
# File 'lib/float-formats/classes.rb', line 1575

def self.radix
  16
end

.total_bitsObject



1582
1583
1584
# File 'lib/float-formats/classes.rb', line 1582

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

.total_bytesObject



1585
1586
1587
# File 'lib/float-formats/classes.rb', line 1585

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

.unpack(v) ⇒ Object



1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
# File 'lib/float-formats/classes.rb', line 1590

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