Skip to content

Commit

Permalink
Made _float_tointeger saturate when the result is too big/small
Browse files Browse the repository at this point in the history
  • Loading branch information
totalspectrum committed Aug 1, 2024
1 parent 070ec32 commit abca23e
Show file tree
Hide file tree
Showing 3 changed files with 715 additions and 708 deletions.
1 change: 1 addition & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Version 7.0.0
- Improved some debug error messages
- Only use the stack for variables whose address is actually taken (thanks to Ada for this!)
- Modified _BlockDevice data structure (used for littlefs, but someday may be used for fatfs too)
- Make _float_tointeger() saturate when the result is too big/small

Version 6.9.10
- Fix compilation error when GETWORD is replaced by MOV or MUL
Expand Down
13 changes: 9 additions & 4 deletions sys/float.spin
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,19 @@ pri _float_tointeger(a=float, r) : integer=long | s, x, m
'Convert float to rounded/truncated integer

(s,x,m) := _float_Unpack(a)
if x => -1 and x =< 30 'if exponent not -1..30, result 0
if x > 30
m := (s) ? $8000_0000 : $7FFF_FFFF
elseif x < -1
m := 0
else
m <<= 2 'msb-justify mantissa
m >>= 30 - x 'shift down to 1/2-lsb
m += r 'round (1) or truncate (0)
m >>= 1 'shift down to lsb
m >>= 1 'shift down to lsb
if s 'handle negation
-m
return m 'return integer
-m

return m 'return integer


pri _float_Unpack(single=float) : s, x, m | tmp
Expand Down
Loading

0 comments on commit abca23e

Please sign in to comment.