SmallInteger.st
changeset 10487 f6287f4b83c9
parent 10342 58ce3aabaa4b
child 10947 e01ba2c3533f
equal deleted inserted replaced
10486:ecab71364dab 10487:f6287f4b83c9
     7  inclusion of the above copyright notice.   This software may not
     7  inclusion of the above copyright notice.   This software may not
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
       
    13 "{ Package: 'stx:libbasic' }"
    12 "{ Package: 'stx:libbasic' }"
    14 
    13 
    15 Integer subclass:#SmallInteger
    14 Integer subclass:#SmallInteger
    16 	instanceVariableNames:''
    15 	instanceVariableNames:''
    17 	classVariableNames:''
    16 	classVariableNames:''
   789      9 quo:4
   788      9 quo:4
   790      -9 quo:4
   789      -9 quo:4
   791     "
   790     "
   792 ! !
   791 ! !
   793 
   792 
       
   793 
   794 !SmallInteger methodsFor:'bit operators'!
   794 !SmallInteger methodsFor:'bit operators'!
   795 
   795 
   796 bitAnd:anInteger
   796 bitAnd:anInteger
   797     "return the bitwise-and of the receiver and the argument, anInteger"
   797     "return the bitwise-and of the receiver and the argument, anInteger"
   798 
   798 
  1587 digitBytes
  1587 digitBytes
  1588     "return a byteArray filled with the receivers bits
  1588     "return a byteArray filled with the receivers bits
  1589      (8 bits of the absolute value per element),
  1589      (8 bits of the absolute value per element),
  1590      least significant byte is first"
  1590      least significant byte is first"
  1591 
  1591 
  1592     ^ self asLargeInteger digitBytes.
  1592     |absValue
       
  1593      b1 "{ Class: SmallInteger }"
       
  1594      b2 "{ Class: SmallInteger }"
       
  1595      b3 "{ Class: SmallInteger }"
       
  1596      b4 "{ Class: SmallInteger }"
       
  1597      b5 "{ Class: SmallInteger }"
       
  1598      b6 "{ Class: SmallInteger }"
       
  1599      b7 "{ Class: SmallInteger }" digitByteArray|
       
  1600 
       
  1601     "
       
  1602      could have simply created a 4-byte largeinteger and normalize it.
       
  1603      The code below does the normalize right away, avoiding the
       
  1604      overhead of producing any intermediate byte-arrays (and the scanning)
       
  1605     "
       
  1606     self == 0 ifTrue: [
       
  1607         ^ ByteArray with:0.
       
  1608     ].
       
  1609 
       
  1610     self < 0 ifTrue: [
       
  1611         absValue := self negated
       
  1612     ] ifFalse: [
       
  1613         absValue := self.
       
  1614     ].
       
  1615 
       
  1616     b1 := absValue bitAnd:16rFF.
       
  1617     absValue := absValue bitShift:-8.
       
  1618     absValue == 0 ifTrue:[
       
  1619         digitByteArray := ByteArray with:b1
       
  1620     ] ifFalse:[
       
  1621         b2 := absValue bitAnd:16rFF.
       
  1622         absValue := absValue bitShift:-8.
       
  1623         absValue == 0 ifTrue:[
       
  1624             digitByteArray := ByteArray with:b1 with:b2
       
  1625         ] ifFalse:[
       
  1626             b3 := absValue bitAnd:16rFF.
       
  1627             absValue := absValue bitShift:-8.
       
  1628             absValue == 0 ifTrue:[
       
  1629                 digitByteArray := ByteArray with:b1 with:b2 with:b3
       
  1630             ] ifFalse:[
       
  1631                 b4 := absValue bitAnd:16rFF.
       
  1632                 absValue := absValue bitShift:-8.
       
  1633                 absValue == 0 ifTrue:[
       
  1634                     digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4
       
  1635                 ] ifFalse:[
       
  1636                     b5 := absValue bitAnd:16rFF.
       
  1637                     absValue := absValue bitShift:-8.
       
  1638                     absValue == 0 ifTrue:[
       
  1639                         digitByteArray := ByteArray new:5.
       
  1640                         digitByteArray at:1 put:b1.
       
  1641                         digitByteArray at:2 put:b2.
       
  1642                         digitByteArray at:3 put:b3.
       
  1643                         digitByteArray at:4 put:b4.
       
  1644                         digitByteArray at:5 put:b5.
       
  1645                     ] ifFalse:[
       
  1646                         b6 := absValue bitAnd:16rFF.
       
  1647                         absValue := absValue bitShift:-8.
       
  1648                         absValue == 0 ifTrue:[
       
  1649                             digitByteArray := ByteArray new:6.
       
  1650                             digitByteArray at:1 put:b1.
       
  1651                             digitByteArray at:2 put:b2.
       
  1652                             digitByteArray at:3 put:b3.
       
  1653                             digitByteArray at:4 put:b4.
       
  1654                             digitByteArray at:5 put:b5.
       
  1655                             digitByteArray at:6 put:b6.
       
  1656                         ] ifFalse:[
       
  1657                             b7 := absValue bitAnd:16rFF.
       
  1658                             absValue := absValue bitShift:-8.
       
  1659                             absValue == 0 ifTrue:[
       
  1660                                 digitByteArray := ByteArray new:7.
       
  1661                                 digitByteArray at:1 put:b1.
       
  1662                                 digitByteArray at:2 put:b2.
       
  1663                                 digitByteArray at:3 put:b3.
       
  1664                                 digitByteArray at:4 put:b4.
       
  1665                                 digitByteArray at:5 put:b5.
       
  1666                                 digitByteArray at:6 put:b6.
       
  1667                                 digitByteArray at:7 put:b7.
       
  1668                             ] ifFalse:[
       
  1669                                 digitByteArray := ByteArray new:8.
       
  1670                                 digitByteArray at:1 put:b1.
       
  1671                                 digitByteArray at:2 put:b2.
       
  1672                                 digitByteArray at:3 put:b3.
       
  1673                                 digitByteArray at:4 put:b4.
       
  1674                                 digitByteArray at:5 put:b5.
       
  1675                                 digitByteArray at:6 put:b6.
       
  1676                                 digitByteArray at:7 put:b7.
       
  1677                                 digitByteArray at:8 put:absValue.
       
  1678                             ]
       
  1679                         ]
       
  1680                     ]
       
  1681                 ]
       
  1682             ]
       
  1683         ]
       
  1684     ].
       
  1685 
       
  1686     ^ digitByteArray
  1593 
  1687 
  1594     "
  1688     "
  1595       16r12 digitBytes
  1689       16r12 digitBytes
  1596       16r1234 digitBytes
  1690       16r1234 digitBytes
  1597       16r12345678 digitBytes
  1691       16r12345678 digitBytes
  1602     "return a byteArray filled with the receivers bits
  1696     "return a byteArray filled with the receivers bits
  1603      (8 bits of the absolute value per element),
  1697      (8 bits of the absolute value per element),
  1604      if msbflag = true, most significant byte is first,
  1698      if msbflag = true, most significant byte is first,
  1605      otherwise least significant byte is first"
  1699      otherwise least significant byte is first"
  1606 
  1700 
  1607     ^ self asLargeInteger digitBytesMSB:msbFlag.
  1701     msbFlag ifTrue:[
       
  1702         ^ self digitBytes reversed.  "digitBytes has been just created - reverse inplace"
       
  1703     ].
       
  1704     ^ self digitBytes
  1608 
  1705 
  1609     "
  1706     "
  1610       16r12 digitBytesMSB:true
  1707       16r12 digitBytesMSB:true
  1611       16r1234 digitBytesMSB:true
  1708       16r1234 digitBytesMSB:true
  1612       16r1234 digitBytesMSB:false
  1709       16r1234 digitBytesMSB:false
  1613       16r12345678 digitBytesMSB:true
  1710       16r12345678 digitBytesMSB:true
  1614       16r12345678 digitBytesMSB:false
  1711       16r12345678 digitBytesMSB:false
  1615     "
  1712     "
  1616 
       
  1617 !
  1713 !
  1618 
  1714 
  1619 digitLength
  1715 digitLength
  1620     "return the number bytes required to represent this Integer.
  1716     "return the number bytes required to represent this Integer.
  1621      For negative receivers, the digitLength of its absolute value
  1717      For negative receivers, the digitLength of its absolute value
  3574 ! !
  3670 ! !
  3575 
  3671 
  3576 !SmallInteger class methodsFor:'documentation'!
  3672 !SmallInteger class methodsFor:'documentation'!
  3577 
  3673 
  3578 version
  3674 version
  3579     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.168 2007-01-19 00:03:12 stefan Exp $'
  3675     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.169 2007-04-02 15:56:50 stefan Exp $'
  3580 ! !
  3676 ! !