[squeak-dev] The Trunk: Kernel-nice.262.mcz

commits at source.squeak.org commits at source.squeak.org
Tue Oct 6 10:21:17 UTC 2009


Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.262.mcz

==================== Summary ====================

Name: Kernel-nice.262
Author: nice
Time: 6 October 2009, 12:21:19 pm
UUID: fbaaa422-359f-3b40-9182-658a0e781350
Ancestors: Kernel-ar.261

Add ANSI required feature #bitAt:
from http://bugs.squeak.org/view.php?id=6985

=============== Diff against Kernel-ar.261 ===============

Item was added:
+ ----- Method: LargePositiveInteger>>bitAt: (in category 'bit manipulation') -----
+ bitAt: anInteger
+ 	"Optimize super algorithm to avoid long bit operations.
+ 	Instead work on digits which are known to be SmallInteger and fast.
+ 	Note that this algorithm does not work for negative integers."
+ 	
+ 	| digitIndex bitIndex |
+ 	digitIndex := anInteger - 1 // 8 + 1.
+ 	digitIndex > self digitLength ifTrue: [^0].
+ 	bitIndex := anInteger - 1 \\ 8 + 1.
+ 	^(self digitAt: digitIndex) bitAt: bitIndex!

Item was added:
+ ----- Method: LargeNegativeInteger>>bitAt: (in category 'bit manipulation') -----
+ bitAt: anInteger
+ 	"super would not work because we have to pretend we are in two-complement.
+ 	this has to be tricky..."
+ 	
+ 	| digitIndex bitIndex i |
+ 	digitIndex := anInteger - 1 // 8 + 1.
+ 	digitIndex > self digitLength ifTrue: [^1].
+ 	bitIndex := anInteger - 1 \\ 8 + 1.
+ 
+ 	i := 1.
+ 	[i = digitIndex
+ 		ifTrue:
+ 			["evaluate two complement (bitInvert + 1) on the digit :
+ 			(if digitIndex > 1, we must still add 1 due to the carry).
+ 			but x bitInvert is -1-x, bitInvert+1 is just x negated..."
+ 			^(self digitAt: digitIndex) negated bitAt: bitIndex].
+ 	(self digitAt: i) = 0]
+ 		whileTrue: [
+ 			"two complement (bitInvert + 1) raises a carry:
+ 			0 bitInvert -> 2r11111111.  2r11111111 + 1 -> 0 with carry...
+ 			Thus we must inquire one digit forward"
+ 			i := i + 1].
+ 	
+ 	"We escaped the while loop, because there is no more carry.
+ 	Do a simple bitInvert without a carry"
+ 	^1 - ((self digitAt: digitIndex) bitAt: bitIndex)!

Item was added:
+ ----- Method: Integer>>bitAt: (in category 'bit manipulation') -----
+ bitAt: anInteger
+ 	"Answer 1 if the bit at position anInteger is set to 1, 0 otherwise.
+ 	self is considered an infinite sequence of bits, so anInteger can be any strictly positive integer.
+ 	Bit at position 1 is the least significant bit.
+ 	Negative numbers are in two-complements.
+ 	
+ 	This is a naive implementation that can be refined in subclass for speed"
+ 	
+ 	^(self bitShift: 1 - anInteger) bitAnd: 1!




More information about the Squeak-dev mailing list