#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Sat, 07 Dec 2019 18:46:20 +0100
changeset 5362 e3b499db0368
parent 5361 2c50d797336c
child 5363 1fa66e549cee
#FEATURE by cg class: IEEEFloat comment/format in: #ceilingAsFloat #fractionPart class: IEEEFloat class comment/format in: #size:exponentSize:fraction:exponent:signBit: changed: #fromFloat:
IEEEFloat.st
--- a/IEEEFloat.st	Sat Dec 07 18:46:00 2019 +0100
+++ b/IEEEFloat.st	Sat Dec 07 18:46:20 2019 +0100
@@ -38,9 +38,33 @@
 !IEEEFloat class methodsFor:'instance creation'!
 
 fromFloat:aFloat
-    |nB f|
+    |nB f mantissaInclInt biasedExponent intBitMask matissaMask|
 
     nB := aFloat byteSize.
+
+    "/ if there are integer bits in the characteristic (extended x86 floats),
+    "/ we need one more byte and there is no hidden integer bit;    
+    aFloat numBitsInIntegerPart > 0 ifTrue:[
+        mantissaInclInt := aFloat mantissaBits.
+        biasedExponent := aFloat exponent + aFloat eBias.
+        matissaMask := (1 bitShift:aFloat numBitsInMantissa)-1. 
+        intBitMask := (1 bitShift:aFloat numBitsInMantissa-1). 
+        (mantissaInclInt bitTest:intBitMask) ifTrue:[
+            "/ turn it off to make it hidden
+            mantissaInclInt := mantissaInclInt bitClear:intBitMask.
+            mantissaInclInt := mantissaInclInt bitShift:1.
+            biasedExponent := biasedExponent - 1.
+        ] ifFalse:[
+            ((mantissaInclInt = 0) and:[biasedExponent == aFloat eBias]) ifTrue:[
+                biasedExponent := 0.
+            ] ifFalse:[
+                self halt.
+            ].
+        ].
+        ^ self size:nB*8 exponentSize:aFloat numBitsInExponent 
+            fraction:mantissaInclInt exponent:biasedExponent signBit:(aFloat < 0 ifTrue:1 ifFalse:0)
+    ].
+
     f := self basicNew:nB.
     1 to:nB do:[:i |
         f basicAt:i put:(aFloat byteAt:i)
@@ -55,6 +79,10 @@
      self fromFloat:2.0 asShortFloat
      self fromFloat:1.0 asLongFloat
      self fromFloat:2.0 asLongFloat
+     self fromFloat:3.0 asLongFloat
+     self fromFloat:101.0 asLongFloat
+     self fromFloat:0.0 asLongFloat
+     self fromFloat:0.5 asLongFloat
     "
 !
 
@@ -79,13 +107,15 @@
     "
 !
 
-size:numBits exponentSize:exponentSize fraction:anInteger exponent:exp signBit:signBit
+size:numBits exponentSize:exponentSize fraction:normalizedFraction exponent:biasedExponent signBit:signBit
     ^ ((self basicNew:(numBits // 8)) exponentSize:exponentSize) 
-            setFraction:anInteger exponent:exp signBit:signBit
+            setFraction:normalizedFraction exponent:biasedExponent signBit:signBit
 
     "
      self size:256 exponentSize:19 fromInteger:1
      self size:256 exponentSize:19 fromInteger:2
+     self size:64 exponentSize:11 fraction:0 exponent:(0 + Float eBias) signBit:0
+     self size:64 exponentSize:11 fraction:0 exponent:0 signBit:0
     "
 !
 
@@ -961,9 +991,11 @@
      It may be useful, if the result is to be further used in another float-operation."
 
     self halt.
+    ^ super ceilingAsFloat.
 
     "
      1.5 asIEEEFloat ceilingAsFloat
+     3.0 asIEEEFloat ceilingAsFloat
     "
 !
 
@@ -979,10 +1011,13 @@
         "/ integer part is zero
         ^ self
     ].
+    nM := self numBitsInMantissa. 
     m := self mantissaBits.
+    "/ bring in the hidden bit
+    m := m bitOr:(1 bitShift:nM).
+
     n := e - eB + 1.
     e := eB - 1.
-    nM := self numBitsInMantissa. 
     m := (m bitShift:n) bitAnd:(1 bitShift:nM)-1.
     "/ normalize
     [m highBit == nM] whileTrue:[
@@ -993,7 +1028,9 @@
         setFraction:m exponent:e signBit:(self negative ifTrue:[1] ifFalse:[0]).
 
     "
-     1.5 asIEEEFloat ceilingAsFloat
+     1.5 asIEEEFloat fractionPart
+     1.0 asIEEEFloat fractionPart
+     3.0 asIEEEFloat fractionPart
     "
 !