SmallInteger.st
changeset 6480 4ff7f2af25fc
parent 6417 10effd5ab612
child 6481 d5c94e93067c
equal deleted inserted replaced
6479:2a0abedfbbd3 6480:4ff7f2af25fc
  1289 
  1289 
  1290 !
  1290 !
  1291 
  1291 
  1292 lowBit
  1292 lowBit
  1293     "return the bitIndex of the lowest bit set. The returned bitIndex
  1293     "return the bitIndex of the lowest bit set. The returned bitIndex
  1294      starts at 1 for the least significant bit. Returns -1 if no bit is set."
  1294      starts at 1 for the least significant bit. 
       
  1295      Returns 0 if no bit is set."
  1295 
  1296 
  1296 %{  /* NOCONTEXT */
  1297 %{  /* NOCONTEXT */
  1297 
  1298 
  1298     INT mask, index, bits;
  1299     INT mask, index, bits;
  1299 
  1300 
  1300     bits = __intVal(self);
  1301     bits = __intVal(self);
  1301     if (bits == 0) {
  1302     if (bits == 0) {
  1302         RETURN ( __MKSMALLINT(-1) );
  1303         RETURN ( __MKSMALLINT(0) );
  1303     }
  1304     }
  1304 
  1305 
  1305 #ifdef __BSF
  1306 #ifdef __BSF
  1306     /*
  1307     /*
  1307      * so much for CISC CPUS:
  1308      * so much for CISC CPUS:
  1308      * the following code is only marginally faster on a PIII-400
  1309      * the following code is only marginally faster on a PIII-400
  1309      * (but saves a few code-bytes, though)
  1310      * (and not at all faster on an Athlon...)
       
  1311      * but saves a few code-bytes, though.
  1310      */
  1312      */
  1311     bits = __BSF(bits);
  1313     bits = __BSF(bits);
  1312     RETURN ( __MKSMALLINT(bits + 1) );
  1314     RETURN ( __MKSMALLINT(bits + 1) );
  1313 #else
  1315 #else
  1314 
  1316 
  1315     if ((bits & 0xFFFFFF) == 0) {
  1317     index = 1;
  1316         mask = 0x1000000;
       
  1317         index = 25;
       
  1318     } else {
       
  1319         if ((bits & 0xFFFF) == 0) {
       
  1320             mask = 0x10000;
       
  1321             index = 17;
       
  1322         } else {
       
  1323             if ((bits & 0xFF) == 0) {
       
  1324                 mask = 0x100;
       
  1325                 index = 9;
       
  1326             } else {
       
  1327                 mask = 1;
       
  1328                 index = 1;
       
  1329             }
       
  1330         }
       
  1331     }
       
  1332 
  1318 
  1333 # ifdef alpha64
  1319 # ifdef alpha64
  1334     while (index != 63) {
  1320     if ((bits<<32) == 0) {
  1335 # else
  1321         index += 32; bits >>= 32;
  1336     while (index != 31) {
  1322     }
  1337 # endif
  1323 # endif
  1338         if (bits & mask) {
  1324 
  1339             RETURN ( __MKSMALLINT(index) );
  1325     if ((bits & 0xFFFF)==0) {
  1340         }
  1326         index += 16; bits >>= 16;
  1341         mask = mask << 1;
  1327     }
  1342         index++;
  1328     if ((bits & 0xFF)==0) {
  1343     }
  1329         index += 8; bits >>= 8;
  1344     RETURN ( __MKSMALLINT(-1) );
  1330     }
  1345 #endif
  1331     if ((bits & 0xF)==0) {
       
  1332         index += 4; bits >>= 4;
       
  1333     }
       
  1334     if ((bits & 0x3)==0) {
       
  1335         index += 2; bits >>= 2;
       
  1336     }
       
  1337     if ((bits & 0x1)==0) {
       
  1338         index += 1;
       
  1339     }
       
  1340 #endif
       
  1341 
       
  1342     RETURN ( __MKSMALLINT(index) );
  1346 %}
  1343 %}
  1347 
  1344 
  1348     "
  1345     "
       
  1346      0 lowBit            
       
  1347      2r0001 lowBit       
       
  1348      2r0010 lowBit       
       
  1349      2r0100 lowBit       
       
  1350      2r1000 lowBit       
       
  1351 
       
  1352      2r000100 lowBit      
       
  1353      2r010010 lowBit      
       
  1354      2r100001 lowBit      
       
  1355      16r1000 lowBit       
       
  1356      16r1000000 lowBit    
       
  1357      16r1000000000000000 lowBit    
       
  1358 
  1349      Time millisecondsToRun:[
  1359      Time millisecondsToRun:[
  1350         1000000 timesRepeat:[
  1360         1000000 timesRepeat:[
  1351             2r1000 lowBit
  1361             2r1000 lowBit
  1352         ]
  1362         ]
  1353      ]  
  1363      ]    
  1354     "
  1364 
  1355     "
       
  1356      Time millisecondsToRun:[
  1365      Time millisecondsToRun:[
  1357         1000000 timesRepeat:[
  1366         1000000 timesRepeat:[
  1358             2r11110000000 lowBit
  1367             2r11110000000 lowBit
  1359         ]
  1368         ]
  1360      ] 
  1369      ] 
  1361     "
  1370 
  1362     "
       
  1363      Time millisecondsToRun:[
  1371      Time millisecondsToRun:[
  1364         1000000 timesRepeat:[
  1372         1000000 timesRepeat:[
  1365             2r1000000000000 lowBit
  1373             2r1000000000000 lowBit
  1366         ]
  1374         ]
  1367      ]  
  1375      ]  
  1368     "
  1376 
  1369     "
       
  1370      Time millisecondsToRun:[
  1377      Time millisecondsToRun:[
  1371         1000000 timesRepeat:[
  1378         1000000 timesRepeat:[
  1372             2r1000000000000000000000000000 lowBit     
  1379             2r1000000000000000000000000000 lowBit     
  1373         ]
  1380         ]
  1374      ]  
  1381      ]  
  1375     "
       
  1376     "
       
  1377      2r000100 lowBit
       
  1378      2r010010 lowBit
       
  1379      2r100001 lowBit
       
  1380      16r1000 lowBit 
       
  1381      16r1000000 lowBit 
       
  1382      0 lowBit 
       
  1383     "
  1382     "
  1384 !
  1383 !
  1385 
  1384 
  1386 setBit:anInteger
  1385 setBit:anInteger
  1387     "return a new number where the specified bit is on.
  1386     "return a new number where the specified bit is on.
  3403 ! !
  3402 ! !
  3404 
  3403 
  3405 !SmallInteger class methodsFor:'documentation'!
  3404 !SmallInteger class methodsFor:'documentation'!
  3406 
  3405 
  3407 version
  3406 version
  3408     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.145 2002-02-26 10:48:26 cg Exp $'
  3407     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.146 2002-03-28 10:50:28 cg Exp $'
  3409 ! !
  3408 ! !