Class: Flt::DPDFormat
- Inherits:
-
DecimalFormatBase
- Object
- FormatBase
- DecimalFormatBase
- Flt::DPDFormat
- Defined in:
- lib/float-formats/classes.rb
Overview
DPD (Densely-Packed-Decimal) formats
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
-
.define(params) ⇒ Object
The field that need to be defined (with lenghts given in decimal digits) are * :sign * :combination * :exponent_continuation * :significand_continuation.
- .pack(s, m, e) ⇒ Object
- .pack_fields(*fields) ⇒ Object
-
.total_bits ⇒ Object
:stopdoc:.
- .total_bytes ⇒ Object
- .unpack(v) ⇒ Object
- .unpack_fields(v) ⇒ Object
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
.define(params) ⇒ Object
The field that need to be defined (with lenghts given in decimal digits) are
-
:sign
-
:combination
-
:exponent_continuation
-
:significand_continuation
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 |
# File 'lib/float-formats/classes.rb', line 1290 def self.define(params) @splitted_fields_supported = false define_fields params[:fields] @internal_field_lengths = @field_lengths @internal_field_meaning = @field_meaning @internal_fields = @fields raise "Unsupported DPD format" unless @internal_fields[:combination]==5 && @internal_fields[:sign]==1 && [0,4,7].include?(@internal_fields[:significand_continuation]%10) @exponent_bits = 2 + @internal_fields[:exponent_continuation] extra_bits = (@internal_fields[:significand_continuation] % 10) extra_digits = (extra_bits==0) ? 0 : ((extra_bits==4) ? 1 : 2) @significand_digits = 1 + 3*(@internal_fields[:significand_continuation]/10) + extra_digits define_fields [:type,1,:sign,1,:exponent,@exponent_bits,:significand,@significand_digits] if params[:bias].nil? params[:bias_mode] = :scientific_significand @exp_limit = 3*(2**@internal_fields[:exponent_continuation])-1 params[:max_exp] = @exp_limit/2 params[:min_exp] = -params[:max_exp] params[:max_exp] += 1 if (@exp_limit%2)==1 params[:bias]= -params[:min_exp] end super params end |
.pack(s, m, e) ⇒ Object
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 |
# File 'lib/float-formats/classes.rb', line 1436 def self.pack(s,m,e) msb = radix_power(@significand_digits-1) t = nil if e==:zero e = @zero_encoded_exp m = 0 elsif e==:infinity e = @infinite_encoded_exp || radix_power(@fields[:exponent])-1 m = 0 t = :infinity elsif e==:nan t = :nan e = 0 s = 0 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 && false while m<msb && e>min_exp e -= 1 m *= radix end end if m<msb && @denormal_encoded_exp && false 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, type: t end |
.pack_fields(*fields) ⇒ Object
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 |
# File 'lib/float-formats/classes.rb', line 1376 def self.pack_fields(*fields) fields = fields[0] if fields.size==1 and fields[0].kind_of?(Array) handle_fields fields type,sign,exponent,significand = fields sig_hex = "%0#{@significand_digits-1}d"%significand sig_cont_bits = 4*(@significand_digits-1) sig_msd = sig_hex[0,1].to_i i_significand_continuation,bits = hexbcd_to_dpd(sig_hex[1..-1]) exp_msb = exponent>>(@exponent_bits-2) i_exponent_continuation = exponent&((1<<(@exponent_bits-2))-1) i_sign = ((sign==0) ? 0 : 1) case type when :infinity i_combination = 0x1E when :nan i_combination = 0x1F else if sig_msd&0x8==0 i_combination = sig_msd|(exp_msb<<3) else i_combination = sig_msd|(1<<4)|(exp_msb<<1) end end h = {sign: i_sign, combination: i_combination, exponent_continuation: i_exponent_continuation, significand_continuation: i_significand_continuation} fields = @internal_field_meaning.collect{|f| h[f]} Bytes.from_bitfields(@internal_field_lengths,fields,@endianness) end |
.total_bits ⇒ Object
:stopdoc:
1326 1327 1328 |
# File 'lib/float-formats/classes.rb', line 1326 def self.total_bits @internal_field_lengths.inject{|x,y| x+y} end |
.total_bytes ⇒ Object
1329 1330 1331 |
# File 'lib/float-formats/classes.rb', line 1329 def self.total_bytes (total_bits+7)/8 end |
.unpack(v) ⇒ Object
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 |
# File 'lib/float-formats/classes.rb', line 1412 def self.unpack(v) f = unpack_fields_hash(v) m = f[:significand] e = f[:exponent] s = f[:sign] t = f[:type] m,e = neg_significand_exponent(s,m,e) if s%2==1 if m==0 # +-zero e = :zero elsif t==:infinity # +-inifinity e = :infinity elsif t==:nan # NaN e = :nan else # normalized number e = decode_exponent(e, :integral_significand) end s = sign_to_unit(s) [s,m,e] end |
.unpack_fields(v) ⇒ Object
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 |
# File 'lib/float-formats/classes.rb', line 1334 def self.unpack_fields(v) # convert internal binary fields to bcd decoded fields a = input_bytes(v).to_bitfields(@internal_field_lengths,@endianness) h = {} (0...a.size).each do |i| h[@internal_field_meaning[i]] = a[i] end i_sign = h[:sign] i_combination = h[:combination] i_exponent_continuation = h[:exponent_continuation] i_significand_continuation = h[:significand_continuation] type = nil sign = i_sign==0 ? 0 : 9 a,b,c,d,e = ("%05B"%i_combination).split('').collect{|bit| bit.to_i} exp_msb = 0 sig_msd = 0 if a==0 || b==0 exp_msb = (a<<1)|b sig_msd = (c<<2)|(d<<1)|e elsif c==0 || d==0 exp_msb = (c<<1)|d sig_msd = (1<<3)|e elsif e==0 type = :infinity else type = :nan end hex_sig = sig_msd.to_s hex_sig << dpd_to_hexbcd(i_significand_continuation,@internal_fields[:significand_continuation]) significand = hex_sig.to_i exponent = i_exponent_continuation | (exp_msb << (@exponent_bits-2)) [type,sign,exponent,significand] end |