SmallInteger.st
branchjv
changeset 18079 7b5afc0ad3d5
parent 18070 d262e3aecaca
parent 15568 e6ce93f948be
child 18080 9ae1db7ef04a
equal deleted inserted replaced
18078:7ef3221b036d 18079:7b5afc0ad3d5
   806      -9 quo:4
   806      -9 quo:4
   807     "
   807     "
   808 ! !
   808 ! !
   809 
   809 
   810 
   810 
       
   811 
   811 !SmallInteger methodsFor:'bit operators'!
   812 !SmallInteger methodsFor:'bit operators'!
   812 
   813 
   813 bitAnd:anInteger
   814 bitAnd:anInteger
   814     "return the bitwise-and of the receiver and the argument, anInteger"
   815     "return the bitwise-and of the receiver and the argument, anInteger"
   815 
   816 
   829 !
   830 !
   830 
   831 
   831 bitAt:anIntegerIndex
   832 bitAt:anIntegerIndex
   832     "return the value of the index's bit (index starts at 1) as 0 or 1.
   833     "return the value of the index's bit (index starts at 1) as 0 or 1.
   833      Notice: the result of bitAt: on negative receivers is not
   834      Notice: the result of bitAt: on negative receivers is not
   834 	     defined in the language standard (since the implementation
   835              defined in the language standard (since the implementation
   835 	     is free to choose any internal representation for integers)"
   836              is free to choose any internal representation for integers)"
   836 
   837 
   837 %{  /* NOCONTEXT */
   838 %{  /* NOCONTEXT */
   838 
   839 
   839     if (__isSmallInteger(anIntegerIndex)) {
   840     if (__isSmallInteger(anIntegerIndex)) {
   840 	INT idx = __smallIntegerVal(anIntegerIndex);
   841         INT idx = __smallIntegerVal(anIntegerIndex);
   841 	if (idx > 0) {
   842         if (idx > 0) {
   842 	    if (idx > N_INT_BITS) {
   843             if (idx > N_INT_BITS) {
   843 		RETURN(__mkSmallInteger(0));
   844                 RETURN(__mkSmallInteger(0));
   844 	    }
   845             }
   845 	    RETURN((__smallIntegerVal(self) & (1 << (idx-1))) ? __mkSmallInteger(1) : __mkSmallInteger(0));
   846             RETURN((__smallIntegerVal(self) & (1 << (idx-1))) ? __mkSmallInteger(1) : __mkSmallInteger(0));
   846 	}
   847         }
   847     }
   848     }
   848 %}.
   849 %}.
   849 
   850 
   850     ^ SubscriptOutOfBoundsSignal
   851     ^ SubscriptOutOfBoundsError
   851 	    raiseRequestWith:anIntegerIndex
   852             raiseRequestWith:anIntegerIndex
   852 	    errorString:'index out of bounds'
   853             errorString:'index out of bounds'
   853 
   854 
   854     "
   855     "
   855      16r00000001 bitAt:0
   856      16r00000001 bitAt:0
   856      16r00000001 bitAt:1
   857      16r00000001 bitAt:1
   857      16r00000001 bitAt:2
   858      16r00000001 bitAt:2
   867 
   868 
   868 " Smalltalk implementation:
   869 " Smalltalk implementation:
   869     |mask|
   870     |mask|
   870 
   871 
   871     anIntegerIndex <= 0 ifTrue:[
   872     anIntegerIndex <= 0 ifTrue:[
   872 	^ SubscriptOutOfBoundsSignal
   873         ^ SubscriptOutOfBoundsSignal
   873 		raiseRequestWith:anIntegerIndex
   874                 raiseRequestWith:anIntegerIndex
   874 		errorString:'index out of bounds'
   875                 errorString:'index out of bounds'
   875     ].
   876     ].
   876     (anIntegerIndex > SmallInteger maxBits) ifTrue:[^ 0].
   877     (anIntegerIndex > SmallInteger maxBits) ifTrue:[^ 0].
   877     mask := 1 bitShift:(anIntegerIndex - 1).
   878     mask := 1 bitShift:(anIntegerIndex - 1).
   878     ((self bitAnd:mask) == 0) ifTrue:[^ 0].
   879     ((self bitAnd:mask) == 0) ifTrue:[^ 0].
   879     ^ 1
   880     ^ 1
  1544      (16r0 setBit:100) hexPrintString
  1545      (16r0 setBit:100) hexPrintString
  1545     "
  1546     "
  1546 ! !
  1547 ! !
  1547 
  1548 
  1548 !SmallInteger methodsFor:'bit operators-32bit'!
  1549 !SmallInteger methodsFor:'bit operators-32bit'!
  1549 
       
  1550 asSigned32
       
  1551     "return a 32-bit integer with my bit-pattern. For protocol completeness."
       
  1552 
       
  1553     ^ self
       
  1554 !
       
  1555 
       
  1556 asUnsigned32
       
  1557     "return a 32-bit integer with my bit-pattern, but positive.
       
  1558      May be required for bit operations on the sign-bit and/or to
       
  1559      convert C/Java numbers."
       
  1560 
       
  1561     self < 0 ifTrue:[
       
  1562 	^ 16r100000000 + self
       
  1563     ].
       
  1564     ^ self
       
  1565 
       
  1566     "
       
  1567      (-1 asUnsigned32) hexPrintString
       
  1568      1 asUnsigned32
       
  1569      (SmallInteger minVal asUnsigned32) hexPrintString
       
  1570      (SmallInteger maxVal asUnsigned32) hexPrintString
       
  1571     "
       
  1572 !
       
  1573 
  1550 
  1574 bitInvert32
  1551 bitInvert32
  1575     "return the value of the receiver with all bits inverted in 32bit signed int space
  1552     "return the value of the receiver with all bits inverted in 32bit signed int space
  1576      (changes the sign)"
  1553      (changes the sign)"
  1577 
  1554 
  1937      could have simply created a 4-byte largeinteger and normalize it.
  1914      could have simply created a 4-byte largeinteger and normalize it.
  1938      The code below does the normalize right away, avoiding the
  1915      The code below does the normalize right away, avoiding the
  1939      overhead of producing any intermediate byte-arrays (and the scanning)
  1916      overhead of producing any intermediate byte-arrays (and the scanning)
  1940     "
  1917     "
  1941     self == 0 ifTrue: [
  1918     self == 0 ifTrue: [
  1942 	^ ByteArray with:0.
  1919         ^ ByteArray with:0.
  1943     ].
  1920     ].
  1944 
  1921 
  1945     self < 0 ifTrue: [
  1922     self < 0 ifTrue: [
  1946 	absValue := self negated
  1923         absValue := self negated
  1947     ] ifFalse: [
  1924     ] ifFalse: [
  1948 	absValue := self.
  1925         absValue := self.
  1949     ].
  1926     ].
  1950 
  1927 
  1951     b1 := absValue bitAnd:16rFF.
  1928     b1 := absValue bitAnd:16rFF.
  1952     absValue := absValue bitShift:-8.
  1929     absValue := absValue bitShift:-8.
  1953     absValue == 0 ifTrue:[
  1930     absValue == 0 ifTrue:[
  1954 	digitByteArray := ByteArray with:b1
  1931         digitByteArray := ByteArray with:b1
  1955     ] ifFalse:[
  1932     ] ifFalse:[
  1956 	b2 := absValue bitAnd:16rFF.
  1933         b2 := absValue bitAnd:16rFF.
  1957 	absValue := absValue bitShift:-8.
  1934         absValue := absValue bitShift:-8.
  1958 	absValue == 0 ifTrue:[
  1935         absValue == 0 ifTrue:[
  1959 	    digitByteArray := ByteArray with:b1 with:b2
  1936             digitByteArray := ByteArray with:b1 with:b2
  1960 	] ifFalse:[
  1937         ] ifFalse:[
  1961 	    b3 := absValue bitAnd:16rFF.
  1938             b3 := absValue bitAnd:16rFF.
  1962 	    absValue := absValue bitShift:-8.
  1939             absValue := absValue bitShift:-8.
  1963 	    absValue == 0 ifTrue:[
  1940             absValue == 0 ifTrue:[
  1964 		digitByteArray := ByteArray with:b1 with:b2 with:b3
  1941                 digitByteArray := ByteArray with:b1 with:b2 with:b3
  1965 	    ] ifFalse:[
  1942             ] ifFalse:[
  1966 		b4 := absValue bitAnd:16rFF.
  1943                 b4 := absValue bitAnd:16rFF.
  1967 		absValue := absValue bitShift:-8.
  1944                 absValue := absValue bitShift:-8.
  1968 		absValue == 0 ifTrue:[
  1945                 absValue == 0 ifTrue:[
  1969 		    digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4
  1946                     digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4
  1970 		] ifFalse:[
  1947                 ] ifFalse:[
  1971 		    b5 := absValue bitAnd:16rFF.
  1948                     b5 := absValue bitAnd:16rFF.
  1972 		    absValue := absValue bitShift:-8.
  1949                     absValue := absValue bitShift:-8.
  1973 		    absValue == 0 ifTrue:[
  1950                     absValue == 0 ifTrue:[
  1974 			digitByteArray := ByteArray new:5.
  1951                         digitByteArray := ByteArray new:5.
  1975 			digitByteArray at:1 put:b1.
  1952                         digitByteArray at:1 put:b1.
  1976 			digitByteArray at:2 put:b2.
  1953                         digitByteArray at:2 put:b2.
  1977 			digitByteArray at:3 put:b3.
  1954                         digitByteArray at:3 put:b3.
  1978 			digitByteArray at:4 put:b4.
  1955                         digitByteArray at:4 put:b4.
  1979 			digitByteArray at:5 put:b5.
  1956                         digitByteArray at:5 put:b5.
  1980 		    ] ifFalse:[
  1957                     ] ifFalse:[
  1981 			b6 := absValue bitAnd:16rFF.
  1958                         b6 := absValue bitAnd:16rFF.
  1982 			absValue := absValue bitShift:-8.
  1959                         absValue := absValue bitShift:-8.
  1983 			absValue == 0 ifTrue:[
  1960                         absValue == 0 ifTrue:[
  1984 			    digitByteArray := ByteArray new:6.
  1961                             digitByteArray := ByteArray new:6.
  1985 			    digitByteArray at:1 put:b1.
  1962                             digitByteArray at:1 put:b1.
  1986 			    digitByteArray at:2 put:b2.
  1963                             digitByteArray at:2 put:b2.
  1987 			    digitByteArray at:3 put:b3.
  1964                             digitByteArray at:3 put:b3.
  1988 			    digitByteArray at:4 put:b4.
  1965                             digitByteArray at:4 put:b4.
  1989 			    digitByteArray at:5 put:b5.
  1966                             digitByteArray at:5 put:b5.
  1990 			    digitByteArray at:6 put:b6.
  1967                             digitByteArray at:6 put:b6.
  1991 			] ifFalse:[
  1968                         ] ifFalse:[
  1992 			    b7 := absValue bitAnd:16rFF.
  1969                             b7 := absValue bitAnd:16rFF.
  1993 			    absValue := absValue bitShift:-8.
  1970                             absValue := absValue bitShift:-8.
  1994 			    absValue == 0 ifTrue:[
  1971                             absValue == 0 ifTrue:[
  1995 				digitByteArray := ByteArray new:7.
  1972                                 digitByteArray := ByteArray new:7.
  1996 				digitByteArray at:1 put:b1.
  1973                                 digitByteArray at:1 put:b1.
  1997 				digitByteArray at:2 put:b2.
  1974                                 digitByteArray at:2 put:b2.
  1998 				digitByteArray at:3 put:b3.
  1975                                 digitByteArray at:3 put:b3.
  1999 				digitByteArray at:4 put:b4.
  1976                                 digitByteArray at:4 put:b4.
  2000 				digitByteArray at:5 put:b5.
  1977                                 digitByteArray at:5 put:b5.
  2001 				digitByteArray at:6 put:b6.
  1978                                 digitByteArray at:6 put:b6.
  2002 				digitByteArray at:7 put:b7.
  1979                                 digitByteArray at:7 put:b7.
  2003 			    ] ifFalse:[
  1980                             ] ifFalse:[
  2004 				digitByteArray := ByteArray new:8.
  1981                                 digitByteArray := ByteArray new:8.
  2005 				digitByteArray at:1 put:b1.
  1982                                 digitByteArray at:1 put:b1.
  2006 				digitByteArray at:2 put:b2.
  1983                                 digitByteArray at:2 put:b2.
  2007 				digitByteArray at:3 put:b3.
  1984                                 digitByteArray at:3 put:b3.
  2008 				digitByteArray at:4 put:b4.
  1985                                 digitByteArray at:4 put:b4.
  2009 				digitByteArray at:5 put:b5.
  1986                                 digitByteArray at:5 put:b5.
  2010 				digitByteArray at:6 put:b6.
  1987                                 digitByteArray at:6 put:b6.
  2011 				digitByteArray at:7 put:b7.
  1988                                 digitByteArray at:7 put:b7.
  2012 				digitByteArray at:8 put:absValue.
  1989                                 digitByteArray at:8 put:absValue.
  2013 			    ]
  1990                             ]
  2014 			]
  1991                         ]
  2015 		    ]
  1992                     ]
  2016 		]
  1993                 ]
  2017 	    ]
  1994             ]
  2018 	]
  1995         ]
  2019     ].
  1996     ].
  2020 
  1997 
  2021     ^ digitByteArray
  1998     ^ digitByteArray
  2022 
  1999 
  2023     "
  2000     "
  2024       16r12 digitBytes
  2001       16r12 digitBytes hexPrintString
  2025       16r1234 digitBytes
  2002       16r1234 digitBytes hexPrintString
  2026       16r12345678 digitBytes
  2003       16r12345678 digitBytes hexPrintString
  2027     "
  2004     "
  2028 !
  2005 !
  2029 
  2006 
  2030 digitBytesMSB:msbFlag
  2007 digitBytesMSB
  2031     "return a byteArray filled with the receivers bits
  2008     "return a byteArray filled with the receivers bits
  2032      (8 bits of the absolute value per element),
  2009      (8 bits of the absolute value per element),
  2033      if msbflag = true, most significant byte is first,
  2010      most significant byte is first"
  2034      otherwise least significant byte is first"
  2011 
  2035 
  2012     |absValue
  2036     msbFlag ifTrue:[
  2013      b1 "{ Class: SmallInteger }"
  2037 	^ self digitBytes reversed.  "digitBytes has been just created - reverse inplace"
  2014      b2 "{ Class: SmallInteger }"
       
  2015      b3 "{ Class: SmallInteger }"
       
  2016      b4 "{ Class: SmallInteger }"
       
  2017      b5 "{ Class: SmallInteger }"
       
  2018      b6 "{ Class: SmallInteger }"
       
  2019      b7 "{ Class: SmallInteger }" digitByteArray|
       
  2020 
       
  2021     "
       
  2022      could have simply created a 4-byte largeinteger and normalize it.
       
  2023      The code below does the normalize right away, avoiding the
       
  2024      overhead of producing any intermediate byte-arrays (and the scanning)
       
  2025     "
       
  2026     self == 0 ifTrue: [
       
  2027         ^ ByteArray with:0.
  2038     ].
  2028     ].
  2039     ^ self digitBytes
  2029 
  2040 
  2030     self < 0 ifTrue: [
  2041     "
  2031         absValue := self negated
  2042       16r12 digitBytesMSB:true
  2032     ] ifFalse: [
  2043       16r1234 digitBytesMSB:true
  2033         absValue := self.
  2044       16r1234 digitBytesMSB:false
  2034     ].
  2045       16r12345678 digitBytesMSB:true
  2035 
  2046       16r12345678 digitBytesMSB:false
  2036     b1 := absValue bitAnd:16rFF.
       
  2037     absValue := absValue bitShift:-8.
       
  2038     absValue == 0 ifTrue:[
       
  2039         digitByteArray := ByteArray with:b1
       
  2040     ] ifFalse:[
       
  2041         b2 := absValue bitAnd:16rFF.
       
  2042         absValue := absValue bitShift:-8.
       
  2043         absValue == 0 ifTrue:[
       
  2044             digitByteArray := ByteArray with:b2 with:b1
       
  2045         ] ifFalse:[
       
  2046             b3 := absValue bitAnd:16rFF.
       
  2047             absValue := absValue bitShift:-8.
       
  2048             absValue == 0 ifTrue:[
       
  2049                 digitByteArray := ByteArray with:b3 with:b2 with:b1
       
  2050             ] ifFalse:[
       
  2051                 b4 := absValue bitAnd:16rFF.
       
  2052                 absValue := absValue bitShift:-8.
       
  2053                 absValue == 0 ifTrue:[
       
  2054                     digitByteArray := ByteArray with:b4 with:b3 with:b2 with:b1
       
  2055                 ] ifFalse:[
       
  2056                     b5 := absValue bitAnd:16rFF.
       
  2057                     absValue := absValue bitShift:-8.
       
  2058                     absValue == 0 ifTrue:[
       
  2059                         digitByteArray := ByteArray new:5.
       
  2060                         digitByteArray at:1 put:b5.
       
  2061                         digitByteArray at:2 put:b4.
       
  2062                         digitByteArray at:3 put:b3.
       
  2063                         digitByteArray at:4 put:b2.
       
  2064                         digitByteArray at:5 put:b1.
       
  2065                     ] ifFalse:[
       
  2066                         b6 := absValue bitAnd:16rFF.
       
  2067                         absValue := absValue bitShift:-8.
       
  2068                         absValue == 0 ifTrue:[
       
  2069                             digitByteArray := ByteArray new:6.
       
  2070                             digitByteArray at:1 put:b6.
       
  2071                             digitByteArray at:2 put:b5.
       
  2072                             digitByteArray at:3 put:b4.
       
  2073                             digitByteArray at:4 put:b3.
       
  2074                             digitByteArray at:5 put:b2.
       
  2075                             digitByteArray at:6 put:b1.
       
  2076                         ] ifFalse:[
       
  2077                             b7 := absValue bitAnd:16rFF.
       
  2078                             absValue := absValue bitShift:-8.
       
  2079                             absValue == 0 ifTrue:[
       
  2080                                 digitByteArray := ByteArray new:7.
       
  2081                                 digitByteArray at:1 put:b7.
       
  2082                                 digitByteArray at:2 put:b6.
       
  2083                                 digitByteArray at:3 put:b5.
       
  2084                                 digitByteArray at:4 put:b4.
       
  2085                                 digitByteArray at:5 put:b3.
       
  2086                                 digitByteArray at:6 put:b2.
       
  2087                                 digitByteArray at:7 put:b1.
       
  2088                             ] ifFalse:[
       
  2089                                 digitByteArray := ByteArray new:8.
       
  2090                                 digitByteArray at:1 put:absValue.
       
  2091                                 digitByteArray at:2 put:b7.
       
  2092                                 digitByteArray at:3 put:b6.
       
  2093                                 digitByteArray at:4 put:b5.
       
  2094                                 digitByteArray at:5 put:b4.
       
  2095                                 digitByteArray at:6 put:b3.
       
  2096                                 digitByteArray at:7 put:b2.
       
  2097                                 digitByteArray at:8 put:b1.
       
  2098                             ]
       
  2099                         ]
       
  2100                     ]
       
  2101                 ]
       
  2102             ]
       
  2103         ]
       
  2104     ].
       
  2105 
       
  2106     ^ digitByteArray
       
  2107 
       
  2108     "
       
  2109       16r12 digitBytesMSB hexPrintString
       
  2110       16r1234 digitBytesMSB hexPrintString
       
  2111       16r12345678 digitBytesMSB hexPrintString
  2047     "
  2112     "
  2048 !
  2113 !
  2049 
  2114 
  2050 digitLength
  2115 digitLength
  2051     "return the number bytes required to represent this Integer.
  2116     "return the number bytes required to represent this Integer.
  4230 ! !
  4295 ! !
  4231 
  4296 
  4232 !SmallInteger class methodsFor:'documentation'!
  4297 !SmallInteger class methodsFor:'documentation'!
  4233 
  4298 
  4234 version
  4299 version
  4235     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.208 2013-06-25 20:37:31 cg Exp $'
  4300     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.211 2013-07-28 19:35:06 stefan Exp $'
  4236 !
  4301 !
  4237 
  4302 
  4238 version_CVS
  4303 version_CVS
  4239     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.208 2013-06-25 20:37:31 cg Exp $'
  4304     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.211 2013-07-28 19:35:06 stefan Exp $'
  4240 ! !
  4305 ! !
  4241 
  4306 
  4242 
  4307 
  4243 SmallInteger initialize!
  4308 SmallInteger initialize!