module Half
Constants
- NAN_BYTES
Public Class Methods
decode(b16)
click to toggle source
# File lib/half.rb, line 24 def self.decode(b16) exp = b16 >> 10 & 0x1f mant = b16 & 0x3ff val = if exp == 0 Math.ldexp(mant, -24) elsif exp == 31 mant == 0 ? Float::INFINITY : Math.ldexp(0x400 + mant, -10).cbor_nan_toggle else Math.ldexp(0x400 + mant, exp-25) end if b16[15] != 0 -val else val end end
decode_from_bytes(hs)
click to toggle source
# File lib/half.rb, line 20 def self.decode_from_bytes(hs) b16, = hs.unpack("n") self.decode(b16) end
encode(fv)
click to toggle source
# File lib/half.rb, line 67 def self.encode(fv) self.encode_from_single(fv, [fv].pack("g")) end
encode_from_single(fv, ss)
click to toggle source
# File lib/half.rb, line 59 def self.encode_from_single(fv, ss) if e = self.encode_from_single_bytes(ss) # p e.to_s(16) hs = [e].pack("n") hs if self.decode_from_bytes(hs) == fv end end
encode_from_single_bytes(ss)
click to toggle source
# File lib/half.rb, line 42 def self.encode_from_single_bytes(ss) # single-precision string b32, = ss.unpack("N") s16 = b32 >> 16 & 0x8000 mant = b32 & 0x7fffff exp = b32 >> 23 & 0xff # puts "#{fv} #{s16} #{mant.to_s(16)} #{exp}" if exp == 0 s16 if mant == 0 # 0.0, -0.0 elsif exp >= 103 && exp < 113 # denorm, exp16 = 0 s16 + ((mant + 0x800000) >> (126 - exp)) elsif exp >= 113 && exp <= 142 # normalized s16 + ((exp - 112) << 10) + (mant >> 13) elsif exp == 255 # Inf (handle NaN elsewhere!) s16 + 0x7c00 if mant == 0 # +Inf/-Inf end end