StdInt.dcl 3.56 KB
Newer Older
1 2
system module StdInt 

Camil Staps's avatar
Camil Staps committed
3 4 5 6
/**
 * Class instances and basic functions for the Int type.
 */

7 8 9 10 11 12 13
// ****************************************************************************************
//	Concurrent Clean Standard Library Module Version 2.0
//	Copyright 1998 University of Nijmegen
// ****************************************************************************************

import	StdOverloaded

14
instance +				Int		:: !Int !Int -> Int					:== code { addI }
15

16
instance -  			Int		:: !Int !Int -> Int					:== code { subI }
17

18
instance zero			Int		:: Int								:== code { pushI 0 }
19

20
instance *  			Int		:: !Int !Int -> Int					:== code { mulI }
21

22 23
instance /				Int		:: !Int !Int -> Int					:== code { divI }
instance one			Int		:: Int								:== code { pushI 1 }
24 25 26 27

instance ^				Int
instance abs			Int
instance sign			Int
28 29 30 31 32 33 34 35 36 37 38 39
instance ~				Int		:: !Int -> Int						:== code { negI }

instance ==				Int		:: !Int !Int -> Bool				:== code { eqI }
instance <  			Int		:: !Int !Int -> Bool				:== code { ltI }
instance isEven 		Int		:: !Int -> Bool						:== code { pushI 1 ; and% ; pushI 0 ; eqI }
						// True if arg1 is an even number
instance isOdd			Int		:: !Int -> Bool						:== code { pushI 1 ; and% ; pushI 0 ; eqI ; notB }
						// True if arg1 is an odd  number

instance toInt			Char	:: !Char -> Int						:== code { CtoI }
instance toInt			Int		:: !Int -> Int						:== code { no_op }
instance toInt			Real	:: !Real -> Int						:== code { RtoI }
40 41
instance toInt			{#Char}

42 43 44 45
instance fromInt		Int		:: !Int -> Int						:== code { no_op }
instance fromInt		Char	:: !Int -> Char						:== code { ItoC }
instance fromInt		Real	:: !Int -> Real						:== code { ItoR }
instance fromInt		{#Char}	:: !Int -> {#Char}					:== code { .d 0 1 i ; jsr ItoAC ; .o 1 0 }
46 47 48

// Additional functions for integer arithmetic: 

49 50
instance rem Int		:: !Int !Int -> Int							:== code { remI }
				 	//	remainder after integer division
51 52 53 54 55
instance gcd Int	//	Greatest common divider
instance lcm Int	//	Least common multiple

//	Operators on Bits:

Camil Staps's avatar
Camil Staps committed
56 57 58 59 60 61 62
/**
 * Bitwise disjunction.
 *
 * @param The first integer
 * @param The second integer
 * @result An integer with exactly those bits set that at least one of the parameters has set
 */
63
(bitor)	infixl  6	:: !Int !Int 	->	Int							:== code { or% }
Camil Staps's avatar
Camil Staps committed
64 65 66 67 68 69 70 71

/**
 * Bitwise conjunction.
 *
 * @param The first integer
 * @param The second integer
 * @result An integer with exactly those bits set that both of the parameters have set
 */
72
(bitand) infixl 6	:: !Int !Int 	->	Int							:== code { and% }
Camil Staps's avatar
Camil Staps committed
73 74 75 76 77 78 79 80

/**
 * Bitwise exclusive disjunction.
 *
 * @param The first integer
 * @param The second integer
 * @result An integer with exactly those bits set that exactly one of the parameters has set
 */
81
(bitxor) infixl 6	:: !Int !Int 	->	Int							:== code { xor% }
Camil Staps's avatar
Camil Staps committed
82 83 84 85 86 87 88 89

/**
 * Shift an integer to the left.
 *
 * @param The integer to shift
 * @param The number of places to shift
 * @result The result of the bitshift
 */
90
(<<)	infix  7	:: !Int !Int 	->	Int							:== code { shiftl% }
Camil Staps's avatar
Camil Staps committed
91 92 93 94 95 96 97 98

/**
 * Shift an integer to the right.
 *
 * @param The integer to shift
 * @param The number of places to shift
 * @result The result of the bitshift
 */
99
(>>)	infix  7	:: !Int !Int 	->	Int							:== code { shiftr% }
Camil Staps's avatar
Camil Staps committed
100 101 102 103 104 105 106

/**
 * Bitwise logical negation.
 *
 * @param An integer
 * @param The one's complement of the parameter
 */
107
bitnot				:: !Int 		->	Int							:== code { not% }
Camil Staps's avatar
Camil Staps committed
108 109 110 111

/**
 * Compile-time macro to check for the integer length of the system.
 *
112
 * @type .a .a -> .a
Camil Staps's avatar
Camil Staps committed
113 114 115 116
 * @param The value if the platform is 64-bit
 * @param The value if the platform is 32-bit
 * @result Either of the parameters, depending on the platform
 */
Camil Staps's avatar
Camil Staps committed
117
IF_INT_64_OR_32 int64 int32 :== int64;