-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
62 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
.../aws-sdk-core/cbor/engines/cbor_engine.rb → ...core/lib/aws-sdk-core/cbor/cbor_engine.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |