ShortFloat.st
changeset 12628 200526f05728
parent 12486 f1ef886832f1
child 12629 2683b8a14a8c
--- a/ShortFloat.st	Fri Dec 18 11:16:17 2009 +0100
+++ b/ShortFloat.st	Fri Dec 18 15:40:48 2009 +0100
@@ -1129,6 +1129,91 @@
     "
 ! !
 
+!ShortFloat methodsFor:'private accessing'!
+
+basicAt:index
+    "return an internal byte of the float.
+     The value returned here depends on byte order, float representation etc.
+     Therefore, this method should be used strictly private.
+
+     Notice:
+        the need to redefine this method here is due to the
+        inability of many machines to store floats in non-double aligned memory.
+        Therefore, on some machines, the first 4 bytes of a float are left unused,
+        and the actual float is stored at index 5 .. 12.
+        To hide this at one place, this method knows about that, and returns
+        values as if this filler wasnt present."
+
+%{  /* NOCONTEXT */
+
+    register int indx;
+    unsigned char *cp;
+
+    /*
+     * notice the missing test for self being a nonNilObject -
+     * this can be done since basicAt: is defined both in UndefinedObject
+     * and SmallInteger
+     */
+    if (__isSmallInteger(index)) {
+        indx = __intVal(index) - 1;
+        if (((unsigned)(indx)) < sizeof(float)) {
+            cp = (unsigned char *)(& (__ShortFloatInstPtr(self)->f_floatvalue));
+            RETURN ( __mkSmallInteger(cp[indx] & 0xFF) );
+        }
+    }
+%}.
+    ^ self indexNotIntegerOrOutOfBounds:index
+!
+
+basicAt:index put:value
+    "set an internal byte of the float.
+     The value to be stored here depends on byte order, float representation etc.
+     Therefore, this method should be used strictly private.
+
+     Notice:
+        the need to redefine this method here is due to the
+        inability of many machines to store floats in non-double aligned memory.
+        Therefore, on some machines, the first 4 bytes of a float are left unused,
+        and the actual float is stored at index 5 .. 12.
+        To hide this at one place, this method knows about that, and returns
+        values as if this filler wasnt present."
+
+%{  /* NOCONTEXT */
+    register int indx, val;
+    unsigned char *cp;
+
+    /*
+     * notice the missing test for self being a nonNilObject -
+     * this can be done since basicAt: is defined both in UndefinedObject
+     * and SmallInteger
+     */
+    if (__bothSmallInteger(index, value)) {
+        val = __intVal(value);
+        if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) {
+            indx = __intVal(index) - 1;
+            if (((unsigned)(indx)) < sizeof(float)) {
+                cp = (unsigned char *)(& (__ShortFloatInstPtr(self)->f_floatvalue));
+                cp[indx] = val;
+                RETURN ( value );
+            }
+        }
+    }
+%}.
+    value isInteger ifFalse:[
+        "
+         the object to store should be an integer number
+        "
+        ^ self elementNotInteger
+    ].
+    (value between:0 and:255) ifFalse:[
+        "
+         the object to store must be a bytes value
+        "
+        ^ self elementBoundsError:value
+    ].
+    ^ self indexNotIntegerOrOutOfBounds:index
+! !
+
 !ShortFloat methodsFor:'special access'!
 
 exponent
@@ -1636,9 +1721,9 @@
 !ShortFloat class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.101 2009-11-05 16:26:31 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.102 2009-12-18 14:40:48 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.101 2009-11-05 16:26:31 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.102 2009-12-18 14:40:48 cg Exp $'
 ! !