Class: Flt::BinaryFormat

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

Overview

Binary floating point formats

Direct Known Subclasses

CDCFormat

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

fields_radix, 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, minus_sign_value, 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, 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

a hidden bit can be define with :hidden_bit



1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
# File 'lib/float-formats/classes.rb', line 1458

def self.define(params)
  @hidden_bit = params[:hidden_bit]
  @hidden_bit = true if @hidden_bit.nil?

  @splitted_fields_supported = true
  define_fields params[:fields]

  @significand_digits = @fields[:significand] + (@hidden_bit ? 1 : 0)
  super  params

end

.pack(s, m, e) ⇒ Object



1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
# File 'lib/float-formats/classes.rb', line 1519

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

  if e==:zero
    e = @zero_encoded_exp || @denormal_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
  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 <<= 1 # m *= radix
      end
    end
    if m<msb && @denormal_encoded_exp
      e = @denormal_encoded_exp
    elsif m==0 # => && @denormal_encoded_exp.nil?
      e = 0
    else
      m &= (radix_power(@significand_digits-1)-1) if @hidden_bit
      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:



1472
1473
1474
# File 'lib/float-formats/classes.rb', line 1472

def self.radix
  2
end

.radix_power(n) ⇒ Object



1475
1476
1477
1478
1479
1480
1481
# File 'lib/float-formats/classes.rb', line 1475

def self.radix_power(n)
  if n>=0
    1 << n
  else
    2**n
  end
end

.total_bitsObject



1483
1484
1485
# File 'lib/float-formats/classes.rb', line 1483

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

.total_bytesObject



1486
1487
1488
# File 'lib/float-formats/classes.rb', line 1486

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

.unpack(v) ⇒ Object



1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
# File 'lib/float-formats/classes.rb', line 1490

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 && !@hidden_bit) ||
     (m==0 && (e==@zero_encoded_exp || e==@denormal_encoded_exp)) ||
     (e==@zero_encoded_exp && @min_encoded_exp>@zero_encoded_exp && !@denormal_encoded_exp)
     # +-zero
     e = :zero
  elsif (e==@denormal_encoded_exp)
    e = decode_exponent(e, :integral_significand)
    e += 1 if @denormal_encoded_exp==@zero_encoded_exp
  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)
    m |= radix_power(@significand_digits-1) if @hidden_bit
  end
  s = sign_to_unit(s)
  [s,m,e]
end