Class: Flt::BCDFormat

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

Overview

BCD (Binary-Coded-Decimal) floating point formats

Direct Known Subclasses

C51BCDFormat

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 DecimalFormatBase

decimal_digits_necessary, decimal_digits_stored, decimal_max_exp, decimal_min_exp, radix, radix_log, radix_log10, radix_power

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, #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

.bcd_field?(i) ⇒ Boolean

this has beed added to allow some fields to contain binary integers rather than bcd

Returns:

  • (Boolean)


1214
1215
1216
# File 'lib/float-formats/classes.rb', line 1214

def self.bcd_field?(i)
  true
end

.define(params) ⇒ Object

The fields lengths are defined in decimal digits



1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'lib/float-formats/classes.rb', line 1151

def self.define(params)

  @splitted_fields_supported = false
  define_fields params[:fields]

  @significand_digits = @fields[:significand]
  super  params

end

.pack(s, m, e) ⇒ Object



1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
# File 'lib/float-formats/classes.rb', line 1243

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 = -1 # ?
    #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 *= 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

.pack_fields(*fields) ⇒ Object



1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
# File 'lib/float-formats/classes.rb', line 1197

def self.pack_fields(*fields)
  fields = fields[0] if fields.size==1 and fields[0].kind_of?(Array)
  handle_fields fields
  i = 0
  nibbles = ""
  for l in @field_lengths
    f = fields[i]
    unless f.kind_of?(String)
      fmt = bcd_field?(i) ? 'd' : 'X'
      f = "%0#{l}#{fmt}" % fields[i]
    end
    nibbles << f.reverse
    i += 1
  end
  Bytes.from_hex(nibbles).reverse_byte_nibbles.convert_endianness(:little_endian,@endianness)
end

.total_bitsObject



1167
1168
1169
# File 'lib/float-formats/classes.rb', line 1167

def self.total_bits
  4*total_nibbles
end

.total_bytesObject



1164
1165
1166
# File 'lib/float-formats/classes.rb', line 1164

def self.total_bytes
  (total_nibbles+1)/2
end

.total_nibblesObject

:stopdoc:



1161
1162
1163
# File 'lib/float-formats/classes.rb', line 1161

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

.unpack(v) ⇒ Object

assume @exponent_mode==:radix_complement



1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
# File 'lib/float-formats/classes.rb', line 1220

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
  s = sign_to_unit(s)
  if @infinite_encoded_exp && e==@infinite_encoded_exp
    # +-infinity
    e = :infinity
  elsif @nan_encoded_exp && e==@nan_encoded_exp
    # NaN
    e = :nan
  elsif m==0
    # +-zero
    e = :zero
  else
    # normalized number
      e = decode_exponent(e, :integral_significand)
  end
  [s,m,e]
end

.unpack_fields(v) ⇒ Object



1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
# File 'lib/float-formats/classes.rb', line 1171

def self.unpack_fields(v)
  # bytes have always nibbles in big-endian order
  v = input_bytes(v).convert_endianness(@endianness,:little_endian).reverse_byte_nibbles
  nibbles = v.to_hex
  # now we have a little endian nibble string
  nibble_fields = []
  i = 0
  for l in @field_lengths
    nibble_fields << nibbles[i,l]
    i += l
  end
  # now we conver the nibble strings to numbers
  i = -1
  nibble_fields.collect do |ns|
    i+=1
    if bcd_field?(i)
      if /\A\d+\Z/.match(ns)
        ns.reverse.to_i
      else
        ns.reverse
      end
    else
      ns.reverse.to_i(16)
    end
  end
end