can check index against low..hi with a single compare&branch
authorClaus Gittinger <cg@exept.de>
Tue, 09 Apr 1996 22:47:05 +0200
changeset 1143 27325100bdf4
parent 1142 3964108b86ca
child 1144 edef70614ae1
can check index against low..hi with a single compare&branch
Array.st
String.st
--- a/Array.st	Sun Apr 07 04:13:20 1996 +0200
+++ b/Array.st	Tue Apr 09 22:47:05 1996 +0200
@@ -238,18 +238,23 @@
 %{  /* NOCONTEXT */
 
     REGISTER int indx;
+    REGISTER OBJ slf;
     REGISTER unsigned int nIndex;
-    OBJ cls;
+    REGISTER OBJ cls;
 
     if (__isSmallInteger(index)) {
 	indx = __intVal(index) - 1;
-	if (indx >= 0) {
-	    nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
-	    if ((cls = __qClass(self)) != Array)
-		indx += __intVal(__ClassInstPtr(cls)->c_ninstvars);
-	    if (indx < nIndex) {
-		RETURN ( __InstPtr(self)->i_instvars[indx] );
-	    }
+	slf = self;
+
+	/* 
+	 * thanks to Patterson/Hennesey - this can be done with a single
+	 * compare ...
+	 */
+	nIndex = __BYTES2OBJS__(__qSize(slf) - OHDR_SIZE);
+	if ((cls = __qClass(slf)) != Array)
+	    indx += __intVal(__ClassInstPtr(cls)->c_ninstvars);
+	if ((unsigned)indx < (unsigned)nIndex) {
+	    RETURN ( __InstPtr(slf)->i_instvars[indx] );
 	}
     }
 %}.
@@ -265,20 +270,24 @@
 %{  /* NOCONTEXT */
 
     REGISTER int indx;
+    REGISTER OBJ slf;
     REGISTER unsigned int nIndex;
-    OBJ cls;
+    REGISTER OBJ cls;
 
     if (__isSmallInteger(index)) {
 	indx = __intVal(index) - 1;
-	if (indx >= 0) {
-	    nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
-	    if ((cls = __qClass(self)) != Array)
-		indx += __intVal(__ClassInstPtr(cls)->c_ninstvars);
-	    if (indx < nIndex) {
-		__InstPtr(self)->i_instvars[indx] = anObject;
-		__STORE(self, anObject);
-		RETURN ( anObject );
-	    }
+	slf = self;
+
+	/* thanks to Patterson/Hennesey - this can be done with a single
+	 * compare ...
+	 */
+	nIndex = __BYTES2OBJS__(__qSize(slf) - OHDR_SIZE);
+	if ((cls = __qClass(slf)) != Array)
+	    indx += __intVal(__ClassInstPtr(cls)->c_ninstvars);
+	if ((unsigned)indx < (unsigned)nIndex) {
+	    __InstPtr(slf)->i_instvars[indx] = anObject;
+	    __STORE(slf, anObject);
+	    RETURN ( anObject );
 	}
     }
 %}.
@@ -292,18 +301,23 @@
 %{  /* NOCONTEXT */
 
     REGISTER int indx;
+    REGISTER OBJ slf;
     REGISTER unsigned int nIndex;
-    OBJ cls;
+    REGISTER OBJ cls;
 
     if (__isSmallInteger(index)) {
 	indx = __intVal(index) - 1;
-	if (indx >= 0) {
-	    nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
-	    if ((cls = __qClass(self)) != Array)
-		indx += __intVal(__ClassInstPtr(cls)->c_ninstvars);
-	    if (indx < nIndex) {
-		RETURN ( __InstPtr(self)->i_instvars[indx] );
-	    }
+	slf = self;
+
+	/* 
+	 * thanks to Patterson/Hennesey - this can be done with a single
+	 * compare ...
+	 */
+	nIndex = __BYTES2OBJS__(__qSize(slf) - OHDR_SIZE);
+	if ((cls = __qClass(slf)) != Array)
+	    indx += __intVal(__ClassInstPtr(cls)->c_ninstvars);
+	if ((unsigned)indx < (unsigned)nIndex) {
+	    RETURN ( __InstPtr(slf)->i_instvars[indx] );
 	}
     }
 %}
@@ -318,20 +332,25 @@
 %{  /* NOCONTEXT */
 
     REGISTER int indx;
+    REGISTER OBJ slf;
     REGISTER unsigned int nIndex;
-    OBJ cls;
+    REGISTER OBJ cls;
 
     if (__isSmallInteger(index)) {
 	indx = __intVal(index) - 1;
-	if (indx >= 0) {
-	    nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
-	    if ((cls = __qClass(self)) != Array)
-		indx += __intVal(__ClassInstPtr(cls)->c_ninstvars);
-	    if (indx < nIndex) {
-		__InstPtr(self)->i_instvars[indx] = anObject;
-		__STORE(self, anObject);
-		RETURN ( anObject );
-	    }
+	slf = self;
+
+	/* 
+	 * thanks to Patterson/Hennesey - this can be done with a single
+	 * compare ...
+	 */
+	nIndex = __BYTES2OBJS__(__qSize(slf) - OHDR_SIZE);
+	if ((cls = __qClass(slf)) != Array)
+	    indx += __intVal(__ClassInstPtr(cls)->c_ninstvars);
+	if ((unsigned)indx < (unsigned)nIndex) {
+	    __InstPtr(slf)->i_instvars[indx] = anObject;
+	    __STORE(slf, anObject);
+	    RETURN ( anObject );
 	}
     }
 %}
@@ -343,8 +362,9 @@
     "return the number of indexed elements in the receiver"
 
 %{  /* NOCONTEXT */
+    REGISTER OBJ slf = self;
 
-    RETURN ( __MKSMALLINT(__arraySize(self) - __intVal(__ClassInstPtr(__qClass(self))->c_ninstvars) ));
+    RETURN ( __MKSMALLINT(__arraySize(slf) - __intVal(__ClassInstPtr(__qClass(slf))->c_ninstvars) ));
 %}
 !
 
@@ -355,8 +375,9 @@
      This method is the same as basicSize."
 
 %{  /* NOCONTEXT */
+    REGISTER OBJ slf = self;
 
-    RETURN ( __MKSMALLINT(__arraySize(self) - __intVal(__ClassInstPtr(__qClass(self))->c_ninstvars) ));
+    RETURN ( __MKSMALLINT(__arraySize(slf) - __intVal(__ClassInstPtr(__qClass(slf))->c_ninstvars) ));
 %}
 ! !
 
@@ -1381,4 +1402,4 @@
 !Array class methodsFor:'documentation'!
 
 version
-^ '$Header: /cvs/stx/stx/libbasic/Array.st,v 1.59 1996-04-02 21:58:17 cg Exp $'! !
+^ '$Header: /cvs/stx/stx/libbasic/Array.st,v 1.60 1996-04-09 20:47:05 cg Exp $'! !
--- a/String.st	Sun Apr 07 04:13:20 1996 +0200
+++ b/String.st	Tue Apr 09 22:47:05 1996 +0200
@@ -279,17 +279,17 @@
 %{  /* NOCONTEXT */
 
     REGISTER int indx;
-    REGISTER OBJ cls;
+    REGISTER OBJ slf, cls;
 
     if (__isSmallInteger(index)) {
-	indx = __intVal(index);
-	if (indx > 0) {
-	    cls = __qClass(self);
-	    if (cls != String)
-		indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
-	    if (indx <= (__stringSize(self))) {
-		RETURN ( __MKCHARACTER(__stringVal(self)[indx-1] & 0xFF) );
-	    }
+	indx = __intVal(index) - 1;
+	slf = self;
+
+	cls = __qClass(slf);
+	if (cls != String)
+	    indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+	if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
+	    RETURN ( __MKCHARACTER(__stringVal(slf)[indx] & 0xFF) );
 	}
     }
 %}.
@@ -305,23 +305,19 @@
 %{  /* NOCONTEXT */
 
     REGISTER int value, indx;
+    REGISTER OBJ slf;
 
-    if (__isString(self)) {
+    slf = self;
+
+    if (__isString(slf)) {
 	if (__isCharacter(aCharacter)) {
 	    value = __intVal(_characterVal(aCharacter));
-#ifdef OLD
-	    if ((value > 0) 
-	     && (value <= 255)
-#else
-	    if (((value & ~0xFF) == 0)
-#endif
+	    if (((unsigned)value <= 0xFF)
 	     && __isSmallInteger(index)) {
-		indx = __intVal(index);
-		if (indx > 0) {
-		    if (indx <= (__stringSize(self))) {
-			__stringVal(self)[indx-1] = value;
-			RETURN ( aCharacter );
-		    }
+		indx = __intVal(index) - 1;
+		if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
+		    __stringVal(slf)[indx] = value;
+		    RETURN ( aCharacter );
 		}
 	    }
 	}
@@ -356,17 +352,17 @@
 %{  /* NOCONTEXT */
 
     REGISTER int indx;
-    REGISTER OBJ cls;
+    REGISTER OBJ slf, cls;
 
     if (__isSmallInteger(index)) {
-	indx = __intVal(index);
-	if (indx > 0) {
-	    cls = __qClass(self);
-	    if (cls != String)
-		indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
-	    if (indx <= (__stringSize(self))) {
-		RETURN ( __MKCHARACTER(__stringVal(self)[indx-1] & 0xFF) );
-	    }
+	indx = __intVal(index) - 1;
+
+	slf = self;
+	cls = __qClass(slf);
+	if (cls != String)
+	    indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+	if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
+	    RETURN ( __MKCHARACTER(__stringVal(slf)[indx] & 0xFF) );
 	}
     }
 %}.
@@ -375,34 +371,31 @@
 
 basicAt:index put:aCharacter
     "store the argument, aCharacter at position index, an Integer
-     - reimplemented here since we store characters
-     (but only for Strings, since subclasses may redefine basicAt:put:)."
+     - reimplemented here since we store characters"
 
 %{  /* NOCONTEXT */
 
     REGISTER int value, indx;
+    REGISTER OBJ slf;
+    REGISTER OBJ cls;
 
-    if (__isString(self)) {
-	if (__isCharacter(aCharacter)) {
-	    value = __intVal(_characterVal(aCharacter));
-	    if ((value > 0) 
-	     && (value <= 255)
-	     && __isSmallInteger(index)) {
-		indx = __intVal(index);
-		if (indx > 0) {
-		    if (indx <= (__stringSize(self))) {
-			__stringVal(self)[indx-1] = value;
-			RETURN ( aCharacter );
-		    }
-		}
+    slf = self;
+
+    if (__isCharacter(aCharacter)) {
+	value = __intVal(_characterVal(aCharacter));
+	if (((unsigned)value <= 0xFF)
+	 && __isSmallInteger(index)) {
+	    indx = __intVal(index) - 1;
+	    cls = __qClass(slf);
+	    if (cls != String)
+	        indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
+	    if ((unsigned)indx < (unsigned)(__stringSize(slf))) {
+		__stringVal(slf)[indx] = value;
+		RETURN ( aCharacter );
 	    }
 	}
     }
 %}.
-    (self isMemberOf:String) ifFalse:[
-	^ super basicAt:index put:aCharacter
-    ].
-
     (aCharacter isMemberOf:Character) ifFalse:[
 	"
 	 tried to store something which is not a character
@@ -426,14 +419,16 @@
      Redefined here to exclude the 0-byte at the end."
 
 %{  /* NOCONTEXT */
-    REGISTER OBJ cls;
+    REGISTER OBJ slf, cls;
 
-    cls = __qClass(self);
+    slf = self;
+
+    cls = __qClass(slf);
     if (cls == String) {
-	RETURN ( __MKSMALLINT(__stringSize(self)) );
+	RETURN ( __MKSMALLINT(__stringSize(slf)) );
     }
-    RETURN ( __MKSMALLINT(__stringSize(self)
-			 - __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars))));
+    RETURN ( __MKSMALLINT(__stringSize(slf)
+	                  - __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars))));
 %}
 !
 
@@ -444,13 +439,14 @@
      This method is the same as basicSize."
 
 %{  /* NOCONTEXT */
-    REGISTER OBJ cls;
+    REGISTER OBJ cls, slf;
 
-    cls = __qClass(self);
+    slf = self;
+    cls = __qClass(slf);
     if (cls == String) {
-	RETURN ( __MKSMALLINT(__stringSize(self)) );
+	RETURN ( __MKSMALLINT(__stringSize(slf)) );
     }
-    RETURN ( __MKSMALLINT(__stringSize(self)
+    RETURN ( __MKSMALLINT(__stringSize(slf)
 			 - __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars))));
 %}
 ! !
@@ -2193,5 +2189,5 @@
 !String class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/String.st,v 1.67 1996-04-06 13:22:51 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/String.st,v 1.68 1996-04-09 20:46:43 cg Exp $'
 ! !