SmallInteger.st
changeset 18410 6806b6ba1316
parent 18401 4eee1a43ffb7
child 18413 1ae6dae275a2
child 18592 43c80b6eb3f9
--- a/SmallInteger.st	Tue May 26 20:55:30 2015 +0200
+++ b/SmallInteger.st	Wed May 27 14:49:13 2015 +0200
@@ -1871,12 +1871,18 @@
 
 %{  /* NOCONTEXT */
 #ifdef __SCHTEAM__
-    int idx = index.intValue() - 1;
-
-    if (idx <= 7) {
-	int byteVal = (int)((self.longValue() >> (idx * 8)) & 0xFF);
-
-	return context._RETURN( STInteger._new(byteVal) );
+    int idx0Based = index.intValue() - 1;
+
+    if (idx0Based <= 7) {
+	long myVal = self.longValue();
+	if (myVal < 0) {
+	    myVal = -myVal;
+	}
+	int byteVal = (int)((myVal >> (idx0Based * 8)) & 0xFF);
+	return __c__._RETURN( STInteger._qnew(byteVal) );
+    }
+    if (idx0Based > 0) {
+	return __c__._RETURN( STInteger._0 );
     }
 #else
     REGISTER INT val;
@@ -1945,7 +1951,22 @@
      for negative ones, the actual bit representation is returned."
 
 %{  /* NOCONTEXT */
-#ifndef __SCHTEAM__
+#ifdef __SCHTEAM__
+    int idx0Based = index.intValue() - 1;
+    long myVal = self.longValue();
+
+    if (idx0Based <= 7) {
+	int byteVal = (int)((myVal >> (idx0Based * 8)) & 0xFF);
+	return __c__._RETURN( STInteger._qnew(byteVal) );
+    }
+    if (idx0Based > 0) {
+	if (myVal < 0) {
+	    return __c__._RETURN( STInteger._M1 );
+	} else {
+	    return __c__._RETURN( STInteger._0 );
+	}
+    }
+#else
     REGISTER INT val;
     INT idx;
 
@@ -2007,7 +2028,7 @@
 !
 
 digitBytes
-    "return a byteArray filled with the receivers bits
+    "return a byteArray filled with the receiver's bits
      (8 bits of the absolute value per element),
      least significant byte is first"
 
@@ -2115,7 +2136,7 @@
 !
 
 digitBytesMSB
-    "return a byteArray filled with the receivers bits
+    "return a byteArray filled with the receiver's bits
      (8 bits of the absolute value per element),
      most significant byte is first"
 
@@ -2228,44 +2249,57 @@
      is returned."
 
 %{  /* NOCONTEXT */
-#ifndef __SCHTEAM__
+#ifdef __SCHTEAM__
+    long val = self.longValue();
+    int offs = 0;
+
+    if (val < 0) val = -val;
+    if ((val & 0xFFFFFFFF00000000L) != 0) {
+	val >>= 32;
+	offs = 4;
+    }
+    if ((val & 0xFFFF0000) != 0) {
+	if ((val & 0xFF000000) != 0) {
+	    offs += 4;
+	} else {
+	    offs += 3;
+	}
+    } else {
+	if ((val & 0x0000FF00)!= 0) {
+	    offs += 2;
+	} else {
+	    offs += 1;
+	}
+    }
+    return __c__._RETURN( STInteger._qnew(offs) );
+#else
     INT val = __intVal(self);
+    int offs = 0;
 
     if (val < 0) {
 	val = -val;
     }
 # if __POINTER_SIZE__ == 8
     if (val & 0xFFFFFFFF00000000L) {
-	if (val & 0xFFFF000000000000L) {
-	    if (val & 0xFF00000000000000L) {
-		RETURN ( __mkSmallInteger(8));
-	    } else {
-		RETURN ( __mkSmallInteger(7));
-	    }
-	} else {
-	    if (val & 0x0000FF0000000000L) {
-		RETURN ( __mkSmallInteger(6));
-	    } else {
-		RETURN ( __mkSmallInteger(5));
-	    }
-	}
+	val >>= 32;
+	offs = 4;
     }
 # endif
 
     if (val & 0xFFFF0000) {
 	if (val & 0xFF000000) {
-	    RETURN ( __mkSmallInteger(4));
+	    RETURN ( __mkSmallInteger(4+offs));
 	} else {
-	    RETURN ( __mkSmallInteger(3));
+	    RETURN ( __mkSmallInteger(3+offs));
 	}
     } else {
 	if (val & 0x0000FF00) {
-	    RETURN ( __mkSmallInteger(2));
+	    RETURN ( __mkSmallInteger(2+offs));
 	} else {
-	    RETURN ( __mkSmallInteger(1));
+	    RETURN ( __mkSmallInteger(1+offs));
 	}
     }
-#endif
+#endif /* not SCHTEAM */
 %}.
     ^ self abs highBit - 1 // 8 + 1
 
@@ -2589,7 +2623,7 @@
 
 %{  /* NOCONTEXT */
 #ifdef __SCHTEAM__
-    return context._RETURN( self.eqP( aNumber ));
+    return context._RETURN( self.eqvP( aNumber ));
 #else
 
     if (aNumber == self) {
@@ -2766,7 +2800,7 @@
 
 %{  /* NOCONTEXT */
 #ifdef __SCHTEAM__
-    return context._RETURN( (self.eqP( aNumber ) == STObject.True) ? STObject.False : STObject.True);
+    return context._RETURN( (self.eqvP( aNumber ) == STObject.True) ? STObject.False : STObject.True);
     /* NOTREACHED */
 #else
 
@@ -3743,12 +3777,17 @@
 asBCD
     "return an integer which represents the BCD encoded value of the receiver;
      that is: each digit of its decimal representation is placed into a nibble
-     of the result. (aka 162 -> 0x162).
+     of the result. (aka 162 -> 0x162). The BCD hex string looks like the original decimal.
      This conversion is useful for some communication protocols,
      or control systems, which represent numbers this way..."
 
 %{  /* NOCONTEXT */
 #ifndef __SCHTEAM__
+    // the following code is a leftover from times when division was expensive;
+    // in modern cpu's, conditional branches are often more expensive than divisions,
+    // so it is questionable, if the effort below is still worth it.
+    // (and asBCD is really used seldom in some serial communication protocols
+    // for control systems)
     int i;
     INT _10000000s = 0, _1000000s = 0;
     INT _100000s = 0, _10000s = 0, _1000s = 0;
@@ -4941,11 +4980,11 @@
 !SmallInteger class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.238 2015-05-24 12:52:47 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.239 2015-05-27 12:49:13 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.238 2015-05-24 12:52:47 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.239 2015-05-27 12:49:13 cg Exp $'
 ! !