SmallInteger.st
changeset 18849 646aee8aa03a
parent 18846 1dfa7fa17f87
child 18858 2968df243134
child 18864 5f4e0d1f6df1
equal deleted inserted replaced
18848:0db793427e55 18849:646aee8aa03a
   437         6 * (-1/2)
   437         6 * (-1/2)
   438    "
   438    "
   439 !
   439 !
   440 
   440 
   441 + aNumber
   441 + aNumber
   442     "return the sum of the receivers value and the arguments value"
   442     "return the sum of the receiver's value and the argument's value"
   443 
   443 
   444 %{  /* NOCONTEXT */
   444 %{  /* NOCONTEXT */
   445 #ifdef __SCHTEAM__
   445 #ifdef __SCHTEAM__
   446     return context._RETURN( self.plus( aNumber ));
   446     return context._RETURN( self.plus( aNumber ));
   447 #else
   447 #else
   455      * (see the message send at the bottom)
   455      * (see the message send at the bottom)
   456      */
   456      */
   457 
   457 
   458     if (__isSmallInteger(aNumber)) {
   458     if (__isSmallInteger(aNumber)) {
   459 # ifdef _ADD_IO_IO
   459 # ifdef _ADD_IO_IO
   460 	RETURN ( _ADD_IO_IO(self, aNumber) );
   460         RETURN ( _ADD_IO_IO(self, aNumber) );
   461 # else
   461 # else
   462 	REGISTER INT sum;
   462         REGISTER INT sum;
   463 
   463 
   464 	sum =  __intVal(self) + __intVal(aNumber);
   464         sum =  __intVal(self) + __intVal(aNumber);
   465 	if (__ISVALIDINTEGER(sum)) {
   465         if (__ISVALIDINTEGER(sum)) {
   466 	    RETURN ( __mkSmallInteger(sum) );
   466             RETURN ( __mkSmallInteger(sum) );
   467 	}
   467         }
   468 	RETURN ( __MKLARGEINT(sum) );
   468         RETURN ( __MKLARGEINT(sum) );
   469 # endif
   469 # endif
   470     }
   470     }
   471     if (__isFloatLike(aNumber)) {
   471     if (__isFloatLike(aNumber)) {
   472 	OBJ newFloat;
   472         OBJ newFloat;
   473 	double val = (double)__intVal(self) + __floatVal(aNumber);
   473         double val = (double)__intVal(self) + __floatVal(aNumber);
   474 
   474 
   475 	__qMKFLOAT(newFloat, val);
   475         __qMKFLOAT(newFloat, val);
   476 	RETURN ( newFloat );
   476         RETURN ( newFloat );
   477     }
   477     }
   478     if (__isShortFloat(aNumber)) {
   478     if (__isShortFloat(aNumber)) {
   479 	OBJ newFloat;
   479         OBJ newFloat;
   480 	float val = (float)__intVal(self) + __shortFloatVal(aNumber);
   480         float val = (float)__intVal(self) + __shortFloatVal(aNumber);
   481 
   481 
   482 	__qMKSFLOAT(newFloat, val);
   482         __qMKSFLOAT(newFloat, val);
   483 	RETURN ( newFloat );
   483         RETURN ( newFloat );
   484     }
   484     }
   485 #endif /* not __SCHTEAM__ */
   485 #endif /* not __SCHTEAM__ */
   486 %}.
   486 %}.
   487     ^ aNumber sumFromInteger:self
   487     ^ aNumber sumFromInteger:self
   488 !
   488 !
   489 
   489 
   490 - aNumber
   490 - aNumber
   491     "return the difference of the receivers value and the arguments value"
   491     "return the difference of the receiver's value and the argument's value"
   492 
   492 
   493 %{  /* NOCONTEXT */
   493 %{  /* NOCONTEXT */
   494 #ifdef __SCHTEAM__
   494 #ifdef __SCHTEAM__
   495     return context._RETURN( self.minus( aNumber ));
   495     return context._RETURN( self.minus( aNumber ));
   496 #else
   496 #else
   504      * (see the message send at the bottom)
   504      * (see the message send at the bottom)
   505      */
   505      */
   506 
   506 
   507     if (__isSmallInteger(aNumber)) {
   507     if (__isSmallInteger(aNumber)) {
   508 # ifdef _SUB_IO_IO
   508 # ifdef _SUB_IO_IO
   509 	RETURN ( _SUB_IO_IO(self, aNumber) );
   509         RETURN ( _SUB_IO_IO(self, aNumber) );
   510 # else
   510 # else
   511 	REGISTER INT diff;
   511         REGISTER INT diff;
   512 
   512 
   513 	diff =  __intVal(self) - __intVal(aNumber);
   513         diff =  __intVal(self) - __intVal(aNumber);
   514 	if (__ISVALIDINTEGER(diff)) {
   514         if (__ISVALIDINTEGER(diff)) {
   515 	    RETURN ( __mkSmallInteger(diff) );
   515             RETURN ( __mkSmallInteger(diff) );
   516 	}
   516         }
   517 	RETURN ( __MKLARGEINT(diff) );
   517         RETURN ( __MKLARGEINT(diff) );
   518 # endif
   518 # endif
   519     }
   519     }
   520     if (__isFloatLike(aNumber)) {
   520     if (__isFloatLike(aNumber)) {
   521 	OBJ newFloat;
   521         OBJ newFloat;
   522 	double val = (double)__intVal(self) - __floatVal(aNumber);
   522         double val = (double)__intVal(self) - __floatVal(aNumber);
   523 
   523 
   524 	__qMKFLOAT(newFloat, val);
   524         __qMKFLOAT(newFloat, val);
   525 	RETURN ( newFloat );
   525         RETURN ( newFloat );
   526     }
   526     }
   527     if (__isShortFloat(aNumber)) {
   527     if (__isShortFloat(aNumber)) {
   528 	OBJ newFloat;
   528         OBJ newFloat;
   529 	float val = (float)__intVal(self) - __shortFloatVal(aNumber);
   529         float val = (float)__intVal(self) - __shortFloatVal(aNumber);
   530 
   530 
   531 	__qMKSFLOAT(newFloat, val);
   531         __qMKSFLOAT(newFloat, val);
   532 	RETURN ( newFloat );
   532         RETURN ( newFloat );
   533     }
   533     }
   534 #endif /* not __SCHTEAM__ */
   534 #endif /* not __SCHTEAM__ */
   535 %}.
   535 %}.
   536     ^ aNumber differenceFromInteger:self
   536     ^ aNumber differenceFromInteger:self
   537 !
   537 !
   538 
   538 
   539 / aNumber
   539 / aNumber
   540     "return the quotient of the receivers value and the arguments value"
   540     "return the quotient of the receiver's value and the argument's value"
   541 
   541 
   542 %{  /* NOCONTEXT */
   542 %{  /* NOCONTEXT */
   543 #ifdef __SCHTEAM__
   543 #ifdef __SCHTEAM__
   544     return context._RETURN( self.quotient( aNumber ));
   544     return context._RETURN( self.quotient( aNumber ));
   545 #else
   545 #else
   556 
   556 
   557     INT me, t, val;
   557     INT me, t, val;
   558     double dval;
   558     double dval;
   559 
   559 
   560     if (__isSmallInteger(aNumber)) {
   560     if (__isSmallInteger(aNumber)) {
   561 	val = __intVal(aNumber);
   561         val = __intVal(aNumber);
   562 	if (val != 0) {
   562         if (val != 0) {
   563 	    me = __intVal(self);
   563             me = __intVal(self);
   564 	    t = me / val;
   564             t = me / val;
   565 # ifdef GOOD_OPTIMIZER
   565 # ifdef GOOD_OPTIMIZER
   566 	    if (me % val == 0) {
   566             if (me % val == 0) {
   567 # else
   567 # else
   568 	    /* this is stupid - all I want is to look for a remainder ...
   568             /* this is stupid - all I want is to look for a remainder ...
   569 	       but most compilers are too stupid and generate an extra modulus
   569                but most compilers are too stupid and generate an extra modulus
   570 	       instruction for "if (me % val)".
   570                instruction for "if (me % val)".
   571 	       Even if most divide instructions already leave the remainder in
   571                Even if most divide instructions already leave the remainder in
   572 	       some register.
   572                some register.
   573 	       Therefore I use a multiplication which is faster than a modulo
   573                Therefore I use a multiplication which is faster than a modulo
   574 	       on most machines. Hint to GNU people :-)
   574                on most machines. Hint to GNU people :-)
   575 	    */
   575             */
   576 	    if ((t * val) == me) {
   576             if ((t * val) == me) {
   577 # endif
   577 # endif
   578 		RETURN ( __mkSmallInteger(t) );
   578                 RETURN ( __mkSmallInteger(t) );
   579 	    }
   579             }
   580 	}
   580         }
   581     } else {
   581     } else {
   582 	if (__isFloatLike(aNumber)) {
   582         if (__isFloatLike(aNumber)) {
   583 	    dval = __floatVal(aNumber);
   583             dval = __floatVal(aNumber);
   584 	    if (dval != 0.0) {
   584             if (dval != 0.0) {
   585 		OBJ newFloat;
   585                 OBJ newFloat;
   586 		double val = (double)__intVal(self) / dval;
   586                 double val = (double)__intVal(self) / dval;
   587 
   587 
   588 		__qMKFLOAT(newFloat, val);
   588                 __qMKFLOAT(newFloat, val);
   589 		RETURN ( newFloat );
   589                 RETURN ( newFloat );
   590 	    }
   590             }
   591 	}
   591         }
   592     }
   592     }
   593 #endif /* not __SCHTEAM__ */
   593 #endif /* not __SCHTEAM__ */
   594 %}.
   594 %}.
   595     aNumber isInteger ifTrue:[
   595     aNumber isInteger ifTrue:[
   596 	aNumber == 0 ifTrue:[
   596         aNumber == 0 ifTrue:[
   597 	    ^ ZeroDivide raiseRequestWith:thisContext.
   597             ^ ZeroDivide raiseRequestWith:thisContext.
   598 	].
   598         ].
   599 	^ Fraction numerator:self denominator:aNumber
   599         ^ Fraction numerator:self denominator:aNumber
   600     ].
   600     ].
   601     ^ aNumber quotientFromInteger:self
   601     ^ aNumber quotientFromInteger:self
   602 
   602 
   603     "
   603     "
   604      8 / 4
   604      8 / 4
   612      -9 quo:4
   612      -9 quo:4
   613     "
   613     "
   614 !
   614 !
   615 
   615 
   616 // aNumber
   616 // aNumber
   617     "return the integer part of the quotient of the receivers value
   617     "return the integer part of the quotient of the receiver's value
   618      and the arguments value.
   618      and the argument's value.
   619      The result is truncated toward negative infinity
   619      The result is truncated toward negative infinity
   620      and will be negative, if the operands signs differ.
   620      and will be negative, if the operands signs differ.
   621      The following is always true:
   621      The following is always true:
   622         (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
   622         (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
   623 
   623 
   893 "/    "only reached for minVal"
   893 "/    "only reached for minVal"
   894 "/    ^ (LargeInteger value:(SmallInteger maxVal)) + 1
   894 "/    ^ (LargeInteger value:(SmallInteger maxVal)) + 1
   895 !
   895 !
   896 
   896 
   897 quo:aNumber
   897 quo:aNumber
   898     "return the integer part of the quotient of the receivers value
   898     "return the integer part of the quotient of the receiver's value
   899      and the arguments value. The result is truncated towards zero
   899      and the argument's value. The result is truncated towards zero
   900      and negative, if the operands signs differ..
   900      and negative, if the operands signs differ..
   901      The following is always true:
   901      The following is always true:
   902         (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver
   902         (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver
   903      For positive results, this is the same as #//,
   903      For positive results, this is the same as #//,
   904      for negative results, the remainder is ignored.
   904      for negative results, the remainder is ignored.