SmallInteger.st
branchjv
changeset 17763 019bb9c842c5
parent 17761 b0e5971141bc
child 17767 a4a32df3aa5e
equal deleted inserted replaced
17762:6eb4414e6a31 17763:019bb9c842c5
  1464     "
  1464     "
  1465 ! !
  1465 ! !
  1466 
  1466 
  1467 !SmallInteger methodsFor:'bit operators-32bit'!
  1467 !SmallInteger methodsFor:'bit operators-32bit'!
  1468 
  1468 
       
  1469 asSigned32
       
  1470     "return a 32-bit integer with my bit-pattern. For protocol completeness."
       
  1471 
       
  1472     ^ self
       
  1473 !
       
  1474 
  1469 asUnsigned32
  1475 asUnsigned32
  1470     "return a 32-bit integer with my bit-pattern, but positive.
  1476     "return a 32-bit integer with my bit-pattern, but positive.
  1471      May be required for bit operations on the sign-bit and/or to
  1477      May be required for bit operations on the sign-bit and/or to
  1472      convert C/Java numbers."
  1478      convert C/Java numbers."
  1473 
  1479 
  1479     "
  1485     "
  1480      (-1 asUnsigned32) hexPrintString
  1486      (-1 asUnsigned32) hexPrintString
  1481      1 asUnsigned32
  1487      1 asUnsigned32
  1482      (SmallInteger minVal asUnsigned32) hexPrintString
  1488      (SmallInteger minVal asUnsigned32) hexPrintString
  1483      (SmallInteger maxVal asUnsigned32) hexPrintString
  1489      (SmallInteger maxVal asUnsigned32) hexPrintString
       
  1490     "
       
  1491 !
       
  1492 
       
  1493 bitInvert32
       
  1494     "return the value of the receiver with all bits inverted in 32bit signed int space
       
  1495      (changes the sign)"
       
  1496 
       
  1497 %{  /* NOCONTEXT */
       
  1498     unsigned INT v;
       
  1499 
       
  1500     v = __intVal(self);
       
  1501     v = ~v;
       
  1502 #if __POINTER_SIZE__ == 8
       
  1503     v &= 0xFFFFFFFFL;
       
  1504 #endif
       
  1505     RETURN ( __MKUINT(v) );
       
  1506 %}.
       
  1507     ^ self primitiveFailed
       
  1508 
       
  1509     "
       
  1510      1 bitInvert32
       
  1511      16r40000000 bitInvert32
       
  1512      16r80000000 bitInvert32
       
  1513     "
       
  1514 !
       
  1515 
       
  1516 bitRotate32:shiftCount
       
  1517     "return the value of the receiver rotated by shiftCount bits,
       
  1518      but only within 32 bits, rotating left for positive, right for negative counts.
       
  1519      Rotates through the sign bit.
       
  1520      Useful for crypt algorithms, or to emulate C/Java semantics."
       
  1521 
       
  1522 %{  /* NOCONTEXT */
       
  1523 
       
  1524     unsigned INT bits;
       
  1525     int count;
       
  1526 
       
  1527     if (__isSmallInteger(shiftCount)) {
       
  1528         count = __intVal(shiftCount);
       
  1529         count = count % 32;
       
  1530 
       
  1531         bits = __intVal(self);
       
  1532         if (count > 0) {
       
  1533             bits = (bits << count) | (bits >> (32-count));
       
  1534         } else {
       
  1535             bits = (bits >> (-count)) | (bits << (32-(-count)));
       
  1536         }
       
  1537 #if __POINTER_SIZE__ == 8
       
  1538         bits &= 0xFFFFFFFFL;
       
  1539 #endif
       
  1540         RETURN (__MKUINT(bits));
       
  1541     }
       
  1542 %}.
       
  1543     ^ self primitiveFailed
       
  1544 
       
  1545     "
       
  1546      128 rotate32:1
       
  1547 
       
  1548      1 rotate32:1   
       
  1549      1 rotate32:2   
       
  1550      1 rotate32:31
       
  1551      1 rotate32:32
       
  1552 
       
  1553      1 rotate32:-1   
       
  1554      1 rotate32:-2   
       
  1555      1 rotate32:-3   
       
  1556      1 rotate32:-32   
  1484     "
  1557     "
  1485 !
  1558 !
  1486 
  1559 
  1487 bitShift32:shiftCount
  1560 bitShift32:shiftCount
  1488     "return the value of the receiver shifted by shiftCount bits,
  1561     "return the value of the receiver shifted by shiftCount bits,
  1519      128 bitShift:24
  1592      128 bitShift:24
  1520      128 bitShift32:24
  1593      128 bitShift32:24
  1521 
  1594 
  1522      1 bitShift:31
  1595      1 bitShift:31
  1523      1 bitShift32:31
  1596      1 bitShift32:31
       
  1597     "
       
  1598 !
       
  1599 
       
  1600 bitXor32:aNumber
       
  1601     "return the xor of the receiver and the argument.
       
  1602      The argument must be another SmallInteger or a 4-byte LargeInteger.
       
  1603      If the result overflows the 32 bit range, the value modulo 16rFFFFFFFF is returned.
       
  1604      This is of course not always correct, but allows for C/Java behavior to be emulated."
       
  1605 
       
  1606 %{  /* NOCONTEXT */
       
  1607     INT rslt;
       
  1608 
       
  1609     rslt =  __unsignedLongIntVal(self) ^ __unsignedLongIntVal(aNumber);
       
  1610 #if __POINTER_SIZE__ == 8
       
  1611     rslt &= 0xFFFFFFFFL;
       
  1612 #endif
       
  1613     RETURN ( __MKUINT(rslt));
       
  1614 %}.
       
  1615     self primitiveFailed
       
  1616 
       
  1617     "
       
  1618      16r7FFFFFFF bitXor: 16r80000000          4294967295
       
  1619      16r7FFFFFFF bitXor32: 16r80000000
  1524     "
  1620     "
  1525 !
  1621 !
  1526 
  1622 
  1527 unsignedBitShift32:shiftCount
  1623 unsignedBitShift32:shiftCount
  1528     "return the value of the receiver shifted by shiftCount bits,
  1624     "return the value of the receiver shifted by shiftCount bits,
  1981     "return the generality value - see ArithmeticValue>>retry:coercing:"
  2077     "return the generality value - see ArithmeticValue>>retry:coercing:"
  1982 
  2078 
  1983     ^ 20
  2079     ^ 20
  1984 !
  2080 !
  1985 
  2081 
       
  2082 signExtended24BitValue
       
  2083     "return a smallInteger from sign-extending the 24'th bit.
       
  2084      May be useful for communication interfaces"
       
  2085 
       
  2086 %{  /* NOCONTEXT */
       
  2087     INT i = __intVal(self);
       
  2088 
       
  2089     if (i & 0x800000) {
       
  2090         i = i | ~0xFFFFFFL;
       
  2091     } else {
       
  2092         i = i & 0x7FFFFF;
       
  2093     }
       
  2094 
       
  2095     RETURN (__mkSmallInteger(i));
       
  2096 %}.
       
  2097     ^ self primitiveFailed
       
  2098 
       
  2099     "
       
  2100      16rFFFFFF signExtended24BitValue
       
  2101      16r800000 signExtended24BitValue
       
  2102      16r7FFFFF signExtended24BitValue
       
  2103     "
       
  2104 !
       
  2105 
  1986 signExtendedByteValue
  2106 signExtendedByteValue
  1987     "return a smallInteger from sign-extending the 8'th bit.
  2107     "return a smallInteger from sign-extending the 8'th bit.
  1988      May be useful for communication interfaces"
  2108      May be useful for communication interfaces"
  1989 
  2109 
  1990 %{  /* NOCONTEXT */
  2110 %{  /* NOCONTEXT */
  1991     INT i = __intVal(self);
  2111     INT i = __intVal(self);
  1992 
  2112 
  1993     if (i & 0x80) {
  2113     if (i & 0x80) {
  1994 	i = i | ~0xFFL;
  2114         i = i | ~0xFFL;
  1995     } else {
  2115     } else {
  1996 	i = i & 0x7F;
  2116         i = i & 0x7F;
  1997     }
  2117     }
  1998 
  2118 
  1999     RETURN (__mkSmallInteger(i));
  2119     RETURN (__mkSmallInteger(i));
  2000 %}.
  2120 %}.
  2001     ^ self primitiveFailed
  2121     ^ self primitiveFailed
  2002 
  2122 
  2003     "
  2123     "
  2004      16rFF signExtendedByteValue
  2124      16rFF signExtendedByteValue 
  2005      16r7F signExtendedByteValue
  2125      16r80 signExtendedByteValue 
       
  2126      16r7F signExtendedByteValue 
  2006     "
  2127     "
  2007 !
  2128 !
  2008 
  2129 
  2009 signExtendedShortValue
  2130 signExtendedShortValue
  2010     "return a smallInteger from sign-extending the 16'th bit.
  2131     "return a smallInteger from sign-extending the 16'th bit.
  2012 
  2133 
  2013 %{  /* NOCONTEXT */
  2134 %{  /* NOCONTEXT */
  2014     INT i = __intVal(self);
  2135     INT i = __intVal(self);
  2015 
  2136 
  2016     if (i & 0x8000) {
  2137     if (i & 0x8000) {
  2017 	i = i | ~0xFFFFL;
  2138         i = i | ~0xFFFFL;
  2018     } else {
  2139     } else {
  2019 	i = i & 0x7FFF;
  2140         i = i & 0x7FFF;
  2020     }
  2141     }
  2021 
  2142 
  2022     RETURN (__mkSmallInteger(i));
  2143     RETURN (__mkSmallInteger(i));
  2023 %}.
  2144 %}.
  2024     ^ self primitiveFailed
  2145     ^ self primitiveFailed
  2025 
  2146 
  2026     "
  2147     "
  2027      16rFFFF signExtendedShortValue
  2148      16rFFFF signExtendedShortValue 
  2028      16r7FFF signExtendedShortValue
  2149      16r8000 signExtendedShortValue 
       
  2150      16r7FFF signExtendedShortValue 
  2029     "
  2151     "
  2030 ! !
  2152 ! !
  2031 
  2153 
  2032 !SmallInteger methodsFor:'comparing'!
  2154 !SmallInteger methodsFor:'comparing'!
  2033 
  2155 
  3126     "
  3248     "
  3127 ! !
  3249 ! !
  3128 
  3250 
  3129 !SmallInteger methodsFor:'modulo arithmetic'!
  3251 !SmallInteger methodsFor:'modulo arithmetic'!
  3130 
  3252 
       
  3253 plus32:aNumber
       
  3254     "return the sum of the receiver and the argument, as SmallInteger.
       
  3255      The argument must be another SmallInteger.
       
  3256      If the result overflows the 32 bit range, the value modulo 16rFFFFFFFF is returned.
       
  3257      This is of course not always correct, but allows for C/Java behavior to be emulated."
       
  3258 
       
  3259 %{  /* NOCONTEXT */
       
  3260     INT sum;
       
  3261 
       
  3262     sum =  __unsignedLongIntVal(self) + __unsignedLongIntVal(aNumber);
       
  3263 #if __POINTER_SIZE__ == 8
       
  3264     sum &= 0xFFFFFFFFL;
       
  3265 #endif
       
  3266     RETURN ( __MKUINT(sum));
       
  3267 %}.
       
  3268     self primitiveFailed
       
  3269 
       
  3270     "
       
  3271      16r7FFFFFFF + 1          2147483648
       
  3272      16r7FFFFFFF plus32: 1    
       
  3273     "
       
  3274 !
       
  3275 
  3131 plus:aNumber
  3276 plus:aNumber
  3132     "return the sum of the receiver and the argument, as SmallInteger.
  3277     "return the sum of the receiver and the argument, as SmallInteger.
  3133      The argument must be another SmallInteger.
  3278      The argument must be another SmallInteger.
  3134      If the result overflows the smallInteger range, the value modulo the
  3279      If the result overflows the smallInteger range, the value modulo the
  3135      smallInteger range is returned (i.e. the low bits of the sum).
  3280      smallInteger range is returned (i.e. the low bits of the sum).
  3770 ! !
  3915 ! !
  3771 
  3916 
  3772 !SmallInteger class methodsFor:'documentation'!
  3917 !SmallInteger class methodsFor:'documentation'!
  3773 
  3918 
  3774 version
  3919 version
  3775     ^ '$Id: SmallInteger.st 10517 2010-04-26 18:26:38Z vranyj1 $'
  3920     ^ '$Id: SmallInteger.st 10520 2010-05-04 11:50:05Z vranyj1 $'
  3776 !
  3921 !
  3777 
  3922 
  3778 version_CVS
  3923 version_CVS
  3779     ^ '§Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.183 2010/03/12 14:12:29 stefan Exp §'
  3924     ^ 'Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.185 2010/04/13 18:58:23 cg Exp §'
  3780 !
  3925 !
  3781 
  3926 
  3782 version_SVN
  3927 version_SVN
  3783     ^ '$Id: SmallInteger.st 10517 2010-04-26 18:26:38Z vranyj1 $'
  3928     ^ '$Id: SmallInteger.st 10520 2010-05-04 11:50:05Z vranyj1 $'
  3784 ! !
  3929 ! !
       
  3930