Class: CBOR::Fbox

Inherits:
Box
  • Object
show all
Defined in:
lib/cbor-diag-support.rb

Constant Summary collapse

FLOAT_EI =
{
  "1" => 2,
  "2" => 4,
  "3" => 8
}

Constants inherited from Box

Box::INTEGER_EI

Instance Attribute Summary

Attributes inherited from Box

#options, #value

Instance Method Summary collapse

Methods inherited from Box

#cbor_diagnostic, from_instance, #inspect, make_head, #to_s

Instance Method Details

#make_float(plusbytes, fv) ⇒ Object

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cbor-diag-support.rb', line 132

def make_float(plusbytes, fv)
  ret =
    if fv.nan?
      # | Format   | Sign bit | Exponent | Significand | Zero
      # | binary16 |        1 |        5 |          10 | 42
      # | binary32 |        1 |        8 |          23 | 29
      # | binary64 |        1 |       11 |          52 | 0
      ds = [fv].pack("G")
      firstword = ds.unpack("n").first
      raise "NaN exponent error #{firstword}" unless firstword & 0x7FF0 == 0x7FF0
      iv = ds.unpack("Q>").first
      ret = case plusbytes
            when 2
              if iv & 0x3ffffffffff == 0 # 42 zero, 10 bits fit in half
                [0xf9, (firstword & 0xFC00) + ((iv >> 42) & 0x3ff)].pack("Cn")
              end
            when 4
              if iv & 0x1fffffff == 0 # 29 zero, 23 bits fit in single
                [0xfa, (ds.getbyte(0) << 24) + ((iv >> 29) & 0xffffff)].pack("CN")
              end
            when 8
              "\xfb".b << ds
            end
    else
      if plusbytes == 8
        [0xfb, fv].pack("CG") # double-precision
      else
        ss = [fv].pack("g")         # single-precision
        if ss.unpack("g").first == fv
          if plusbytes == 4
            "\xfa".b << ss
          else
            if hs = Half.encode_from_single(fv, ss)
              "\xf9".b << hs
            end
          end
        end
      end
    end
  raise ArgumentError, "cbor-diagnostic: make_float #{plusbytes.inspect} #{fv.inspect}" unless ret
  ret
end

#to_cborObject



175
176
177
178
179
180
181
182
183
# File 'lib/cbor-diag-support.rb', line 175

def to_cbor
  if ei = options[:ei]
    plusbytes = FLOAT_EI[ei]
    raise ArgumentError, "cbor-diagnostic: unknown encoding indicator _#{ei} for Float':\n" unless plusbytes
    make_float(plusbytes, value)
  else
    CBOR.encode(value)
  end
end