ByteArray.st
changeset 16621 ac0885c03e15
parent 16620 9db0a09c71c0
child 16741 81ada4378090
--- a/ByteArray.st	Mon Jun 23 22:31:40 2014 +0200
+++ b/ByteArray.st	Mon Jun 23 22:54:00 2014 +0200
@@ -1534,12 +1534,12 @@
      of some logical operation, as specified by the ruleSymbol.
      SourceBytes are fetched starting at sourceOffset.
      Valid rule symbols are:
-	#copy    - trivial;  same as replaceBytesFrom:to:with:startingAt:
-	#bitXor: - xoring;   byte[dI] = byte[dI] bitXor:(srcByte[sI])
-	#bitAnd: - anding;   byte[dI] = byte[dI] bitAnd:(srcByte[sI])
-	#bitOr:  - oring;    byte[dI] = byte[dI] bitOr:(srcByte[sI])
-	#+       - adding;   byte[dI] = (byte[dI] + (srcByte[sI])) mod: 256
-	#-       - subtract; byte[dI] = (byte[dI] - (srcByte[sI])) mod: 256
+        #copy    - trivial;  same as replaceBytesFrom:to:with:startingAt:
+        #bitXor: - xoring;   byte[dI] = byte[dI] bitXor:(srcByte[sI])
+        #bitAnd: - anding;   byte[dI] = byte[dI] bitAnd:(srcByte[sI])
+        #bitOr:  - oring;    byte[dI] = byte[dI] bitOr:(srcByte[sI])
+        #+       - adding;   byte[dI] = (byte[dI] + (srcByte[sI])) mod: 256
+        #-       - subtract; byte[dI] = (byte[dI] - (srcByte[sI])) mod: 256
      Warning: this is a destructive operation - elements in the receiver are overwritten.
     "
 
@@ -1551,130 +1551,139 @@
      && __isSmallInteger(dstStart)
      && __isSmallInteger(dstEnd)
      && __isSmallInteger(sourceStart)) {
-	unsigned char *srcP = __ByteArrayInstPtr(sourceBytes)->ba_element;
-	unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element;
-	int srcLen = __byteArraySize(sourceBytes);
-	int dstLen = __byteArraySize(self);
-	int __srcStart = __intVal(sourceStart);
-	int __dstStart = __intVal(dstStart);
-	int count = __intVal(dstEnd) - __dstStart + 1;
-
-	if ((__dstStart >= 1)
-	 && (__srcStart >= 1)
-	 && ((__dstStart + count - 1) <= dstLen)
-	 && ((__srcStart + count - 1) <= srcLen)) {
-	    srcP += __srcStart - 1;
-	    dstP += __dstStart - 1;
+        unsigned char *srcP = __ByteArrayInstPtr(sourceBytes)->ba_element;
+        unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element;
+        int srcLen = __byteArraySize(sourceBytes);
+        int dstLen = __byteArraySize(self);
+        int __srcStart = __intVal(sourceStart);
+        int __dstStart = __intVal(dstStart);
+        int count = __intVal(dstEnd) - __dstStart + 1;
+
+        if ((__dstStart >= 1)
+         && (__srcStart >= 1)
+         && ((__dstStart + count - 1) <= dstLen)
+         && ((__srcStart + count - 1) <= srcLen)) {
+            srcP += __srcStart - 1;
+            dstP += __dstStart - 1;
 
 #define OP_LOOP_BYTES(OP) \
+    while (count >= 4) {                                             \
+        (dstP[0]) OP (srcP[0]);                                      \
+        (dstP[1]) OP (srcP[1]);                                      \
+        (dstP[2]) OP (srcP[2]);                                      \
+        (dstP[3]) OP (srcP[3]);                                      \
+        srcP += 4;                                                   \
+        dstP += 4;                                                   \
+        count -= 4;                                                  \
+    }                                                                \
     while (count > 0) {                                              \
-	*dstP OP (*srcP);                                            \
-	srcP++;                                                      \
-	dstP++;                                                      \
-	count--;                                                     \
+        *dstP OP (*srcP);                                            \
+        srcP++;                                                      \
+        dstP++;                                                      \
+        count--;                                                     \
     }
 
 #define OP_LOOP(OP) \
     while (count >= 16) {                                            \
-	((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]);    \
-	((unsigned int *)dstP)[1] OP (((unsigned int *)srcP)[1]);    \
-	((unsigned int *)dstP)[2] OP (((unsigned int *)srcP)[2]);    \
-	((unsigned int *)dstP)[3] OP (((unsigned int *)srcP)[3]);    \
-	srcP += 16;                                                  \
-	dstP += 16;                                                  \
-	count -= 16;                                                 \
+        ((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]);    \
+        ((unsigned int *)dstP)[1] OP (((unsigned int *)srcP)[1]);    \
+        ((unsigned int *)dstP)[2] OP (((unsigned int *)srcP)[2]);    \
+        ((unsigned int *)dstP)[3] OP (((unsigned int *)srcP)[3]);    \
+        srcP += 16;                                                  \
+        dstP += 16;                                                  \
+        count -= 16;                                                 \
     }                                                                \
     while (count >= 4) {                                             \
-	((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]);    \
-	srcP += 4;                                                   \
-	dstP += 4;                                                   \
-	count -= 4;                                                  \
+        ((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]);    \
+        srcP += 4;                                                   \
+        dstP += 4;                                                   \
+        count -= 4;                                                  \
     }                                                                \
     while (count > 0) {                                              \
-	*dstP OP (*srcP);                                            \
-	srcP++;                                                      \
-	dstP++;                                                      \
-	count--;                                                     \
+        *dstP OP (*srcP);                                            \
+        srcP++;                                                      \
+        dstP++;                                                      \
+        count--;                                                     \
     }
 
 
-	    if (ruleSymbol == @symbol(bitXor:)) {
-		OP_LOOP( ^= )
-		RETURN (self);
-	    }
-	    if (ruleSymbol == @symbol(bitXorNot:)) {
-		OP_LOOP( ^=~ )
-		RETURN (self);
-	    }
-	    if (ruleSymbol == @symbol(bitAnd:)) {
-		OP_LOOP( &= )
-		RETURN (self);
-	    }
-	    if (ruleSymbol == @symbol(bitAndNot:)) {
-		OP_LOOP( &=~ )
-		RETURN (self);
-	    }
-	    if (ruleSymbol == @symbol(bitOr:)) {
-		OP_LOOP( |= )
-		RETURN (self);
-	    }
-	    if (ruleSymbol == @symbol(bitOrNot:)) {
-		OP_LOOP( |=~ )
-		RETURN (self);
-	    }
-	    if (ruleSymbol == @symbol(copy)) {
-		OP_LOOP( = )
-		RETURN (self);
-	    }
-	    if (ruleSymbol == @symbol(copyNot)) {
-		OP_LOOP( =~ )
-		RETURN (self);
-	    }
-	    if (ruleSymbol == @symbol(+)) {
-		OP_LOOP_BYTES( += )
-		RETURN (self);
-	    }
-	    if (ruleSymbol == @symbol(-)) {
-		OP_LOOP_BYTES( -= )
-		RETURN (self);
-	    }
-	}
+            if (ruleSymbol == @symbol(bitXor:)) {
+                OP_LOOP( ^= )
+                RETURN (self);
+            }
+            if (ruleSymbol == @symbol(bitXorNot:)) {
+                OP_LOOP( ^=~ )
+                RETURN (self);
+            }
+            if (ruleSymbol == @symbol(bitAnd:)) {
+                OP_LOOP( &= )
+                RETURN (self);
+            }
+            if (ruleSymbol == @symbol(bitAndNot:)) {
+                OP_LOOP( &=~ )
+                RETURN (self);
+            }
+            if (ruleSymbol == @symbol(bitOr:)) {
+                OP_LOOP( |= )
+                RETURN (self);
+            }
+            if (ruleSymbol == @symbol(bitOrNot:)) {
+                OP_LOOP( |=~ )
+                RETURN (self);
+            }
+            if (ruleSymbol == @symbol(copy)) {
+                OP_LOOP( = )
+                RETURN (self);
+            }
+            if (ruleSymbol == @symbol(copyNot)) {
+                OP_LOOP( =~ )
+                RETURN (self);
+            }
+            if (ruleSymbol == @symbol(+)) {
+                OP_LOOP_BYTES( += )
+                RETURN (self);
+            }
+            if (ruleSymbol == @symbol(-)) {
+                OP_LOOP_BYTES( -= )
+                RETURN (self);
+            }
+        }
     }
 #undef OP_LOOP_BYTES
 #undef OP_LOOP
 
 %}.
     ruleSymbol == #copy ifTrue:[
-	self replaceFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart.
-	^ self
+        self replaceFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart.
+        ^ self
     ].
 
     srcIdx := sourceStart.
     dstStart to:dstEnd do:[:dstIdx |
-	self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:(sourceBytes at:srcIdx)).
-	srcIdx := srcIdx + 1.
+        self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:(sourceBytes at:srcIdx)).
+        srcIdx := srcIdx + 1.
     ].
 
     "
      #[1 2 3 4 5 6 7 8]
-	bitBlitBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor:
+        bitBlitBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor:
      #[1 2 3 4 5 6 7 8]
-	bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor:
+        bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor:
      #[1 2 3 4 5 6 7 8]
-	bitBlitBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#bitAnd:
+        bitBlitBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#bitAnd:
      #[1 2 3 4 5 6 7 8]
-	bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+
+        bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+
      #[255 0 0 0 0 0 0 0]
-	bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+
+        bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+
      #[1 2 3 4 5 6 7 8]
-	bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#+
+        bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#+
      #[1 2 3 4 5 6 7 8]
-	bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#+
+        bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#+
      #[1 2 3 4 5 6 7 8]
-	bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#copyNot
+        bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#copyNot
 
      #[1 2 3 4 5 6 7 8]
-	bitBlitBytesFrom:1 to:8 with:(1 to:8) startingAt:1 rule:#+
+        bitBlitBytesFrom:1 to:8 with:(1 to:8) startingAt:1 rule:#+
     "
 !
 
@@ -2203,36 +2212,35 @@
      Q: is this really needed ?"
 
 %{  /* NOCONTEXT */
-
     REGISTER unsigned char *dst;
     REGISTER unsigned long *ldst;
     REGISTER int cnt;
 
     if (__qClass(self) == @global(ByteArray)) {
-	cnt = __byteArraySize(self);
-	dst = __ByteArrayInstPtr(self)->ba_element;
-	if (! ((INT)dst & (sizeof(long)-1))) {
-	    ldst = (unsigned long *)dst;
-	    while (cnt >= (sizeof(long))*4) {
-		ldst[0] = ~(ldst[0]);
-		ldst[1] = ~(ldst[1]);
-		ldst[2] = ~(ldst[2]);
-		ldst[3] = ~(ldst[3]);
-		ldst += 4;
-		cnt -= (sizeof(long))*4;
-	    }
-	    while (cnt >= sizeof(long)) {
-		*ldst = ~(*ldst);
-		ldst++;
-		cnt -= sizeof(long);
-	    }
-	    dst = (unsigned char *)ldst;
-	}
-	while (cnt--) {
-	    *dst = ~(*dst);
-	    dst++;
-	}
-	RETURN ( self );
+        cnt = __byteArraySize(self);
+        dst = __ByteArrayInstPtr(self)->ba_element;
+        if (((INT)dst & (sizeof(long)-1)) == 0) { // aligned
+            ldst = (unsigned long *)dst;
+            while (cnt >= (sizeof(long))*4) {
+                ldst[0] = ~(ldst[0]);
+                ldst[1] = ~(ldst[1]);
+                ldst[2] = ~(ldst[2]);
+                ldst[3] = ~(ldst[3]);
+                ldst += 4;
+                cnt -= (sizeof(long))*4;
+            }
+            while (cnt >= sizeof(long)) {
+                *ldst = ~(*ldst);
+                ldst++;
+                cnt -= sizeof(long);
+            }
+            dst = (unsigned char *)ldst;
+        }
+        while (cnt--) {
+            *dst = ~(*dst);
+            dst++;
+        }
+        RETURN ( self );
     }
 %}.
     self bitBlitBytesFrom:1 to:self size withConstant:16rFF rule:#bitXor:
@@ -2240,7 +2248,13 @@
     "
      #[1 2 3 4 5 6 7 8 9 10] copy invert
      #[1 2 3 4 5 6 7 8 9 10] copy
-	bitBlitBytesFrom:1 to:10 withConstant:16rFF rule:#bitXor:
+        bitBlitBytesFrom:1 to:10 withConstant:16rFF rule:#bitXor:
+
+     |l|
+     l := ByteArray fromHexString:'0102030405060708090a0b0c0d0e0f1112131415161718191a1b1c1d1e1f'. 
+     Time millisecondsToRun:[
+        1000000 timesRepeat:[ l invert ].
+     ]  
     "
 !
 
@@ -3030,10 +3044,10 @@
 !ByteArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.212 2014-06-23 20:31:40 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.213 2014-06-23 20:54:00 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.212 2014-06-23 20:31:40 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.213 2014-06-23 20:54:00 cg Exp $'
 ! !