Skip to content

Commit

Permalink
Flatten cbor and bring back half
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp committed Apr 16, 2024
1 parent daff795 commit 345024b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/cbor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative 'cbor/engines/cbor_engine'
require_relative 'cbor/cbor_engine'

module Aws
# @api private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require_relative '../tagged'
require_relative '../encoder'
require_relative '../decoder'
require_relative 'tagged'
require_relative 'encoder'
require_relative 'decoder'

module Aws
module Cbor
Expand Down
20 changes: 1 addition & 19 deletions gems/aws-sdk-core/lib/aws-sdk-core/cbor/decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,25 +203,7 @@ def read_undefined
# precision - 10 bits
def read_half
read_info
b16 = take(2).unpack1('n')
exp = b16 >> 10 & 0x1f
mant = b16 & 0x3ff
val =
case exp
when 0
Math.ldexp(mant, -24)
when 31
mant.zero? ? Float::INFINITY : Float::NAN
else
# exp bias is 15, but to use ldexp we divide by 1024 (2^10) to get
# exp-15-10
Math.ldexp(1024 + mant, exp - 25)
end
if b16[15] != 0
-val
else
val
end
Half.decode(take(2).unpack1('n'))
end

def read_float
Expand Down
34 changes: 34 additions & 0 deletions gems/aws-sdk-core/lib/aws-sdk-core/cbor/half.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module Aws
module Cbor
# 16 bit IEEE 754 half-precision floats
# Support decoding only
# format:
# sign - 1 bit
# exponent - 5 bits
# precision - 10 bits
module Half
def self.decode(b16)
exp = b16 >> 10 & 0x1f
mant = b16 & 0x3ff
val =
case exp
when 0
Math.ldexp(mant, -24)
when 31
mant.zero? ? Float::INFINITY : Float::NAN
else
# exp bias is 15, but to use ldexp we divide by 1024 (2^10) to get
# exp-15-10
Math.ldexp(1024 + mant, exp - 25)
end
if b16[15] != 0
-val
else
val
end
end
end
end
end
23 changes: 23 additions & 0 deletions gems/aws-sdk-core/spec/aws/cbor/half_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require_relative '../../spec_helper'

module Aws
module Cbor
describe Half do

it 'correctly decodes half precision numbers' do
expect(Cbor::Half.decode(0)).to eq(0)
expect(Cbor::Half.decode(0x3c00)).to eq(1)
expect(Cbor::Half.decode(0x7bff)).to eq(65504)
expect(Cbor::Half.decode(0x3555)).to be_within(0.0001).of(0.3333)
end

it 'decodes inf and nan' do
expect(Cbor::Half.decode(0x7c00)).to eq(Float::INFINITY)
expect(Cbor::Half.decode(0xfc00)).to eq(-Float::INFINITY)
expect(Cbor::Half.decode(0x7c01).nan?).to be true
end
end
end
end

0 comments on commit 345024b

Please sign in to comment.