]> gitweb.factorcode.org Git - factor.git/blob - library/platform/jvm/arithmetic.factor
34a7ee3a4bf955f06757ce37c850423181174646
[factor.git] / library / platform / jvm / arithmetic.factor
1 ! :folding=indent:collapseFolds=0:
2
3 ! $Id$
4 !
5 ! Copyright (C) 2003, 2004 Slava Pestov.
6
7 ! Redistribution and use in source and binary forms, with or without
8 ! modification, are permitted provided that the following conditions are met:
9
10 ! 1. Redistributions of source code must retain the above copyright notice,
11 !    this list of conditions and the following disclaimer.
12
13 ! 2. Redistributions in binary form must reproduce the above copyright notice,
14 !    this list of conditions and the following disclaimer in the documentation
15 !    and/or other materials provided with the distribution.
16
17 ! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 ! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 ! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 ! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 ! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 ! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 ! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 IN: arithmetic
29 USE: combinators
30 USE: kernel
31 USE: logic
32 USE: stack
33
34 : + ( a b -- a+b )
35     [ "java.lang.Number" "java.lang.Number" ]
36     "factor.math.FactorMath" "add"
37     jinvoke-static ; inline
38
39 : - ( a b -- a-b )
40     [ "java.lang.Number" "java.lang.Number" ]
41     "factor.math.FactorMath" "subtract"
42     jinvoke-static ; inline
43
44 : * ( a b -- a*b )
45     [ "java.lang.Number" "java.lang.Number" ]
46     "factor.math.FactorMath" "multiply"
47     jinvoke-static ; inline
48
49 : / ( a b -- a/b )
50     [ "java.lang.Number" "java.lang.Number" ]
51     "factor.math.FactorMath" "divide"
52     jinvoke-static ; inline
53
54 : /i ( a b -- a/b )
55     #! Truncating division.
56     [ "java.lang.Number" "java.lang.Number" ]
57     "factor.math.FactorMath" "_divide"
58     jinvoke-static ; inline
59
60 : mod ( a b -- a%b )
61     [ "java.lang.Number" "java.lang.Number" ]
62     "factor.math.FactorMath" "mod"
63     jinvoke-static ; inline
64
65 : /mod ( a b -- a/b a%b )
66     2dup /i -rot mod ;
67
68 : > ( a b -- boolean )
69     [ "java.lang.Number" "java.lang.Number" ]
70     "factor.math.FactorMath" "greater"
71     jinvoke-static ; inline
72
73 : >= ( a b -- boolean )
74     [ "java.lang.Number" "java.lang.Number" ]
75     "factor.math.FactorMath" "greaterEqual"
76     jinvoke-static ; inline
77
78 : < ( a b -- boolean )
79     [ "java.lang.Number" "java.lang.Number" ]
80     "factor.math.FactorMath" "less"
81     jinvoke-static ; inline
82
83 : <= ( a b -- boolean )
84     [ "java.lang.Number" "java.lang.Number" ]
85     "factor.math.FactorMath" "lessEqual"
86     jinvoke-static ; inline
87
88 : >=< ( x y obj1 obj2 obj3 -- obj )
89     ! If x > y, pushes obj1, if x = y, pushes obj2, else obj3.
90     [
91         "float" "float"
92         "java.lang.Object" "java.lang.Object"  "java.lang.Object"
93     ]
94     "factor.FactorLib" "branch3" jinvoke-static ;
95
96 : bitand ( x y -- x&y )
97     #! Bitwise and.
98     [ "java.lang.Number" "java.lang.Number" ]
99     "factor.math.FactorMath" "and"
100     jinvoke-static ; inline
101
102 : bitor ( x y -- x|y )
103     #! Bitwise or.
104     [ "java.lang.Number" "java.lang.Number" ]
105     "factor.math.FactorMath" "or"
106     jinvoke-static ; inline
107
108 : bitxor ( x y -- x^y )
109     #! Bitwise exclusive or.
110     [ "java.lang.Number" "java.lang.Number" ]
111     "factor.math.FactorMath" "xor"
112     jinvoke-static ; inline
113
114 : bitnot ( x -- ~x )
115     #! Bitwise complement.
116     [ "java.lang.Number" ]
117     "factor.math.FactorMath" "not"
118     jinvoke-static ; inline
119
120 : shift< ( x by -- )
121     #! Shift 'by' bits to the left.
122     [ "java.lang.Number" "int" ]
123     "factor.math.FactorMath" "shiftLeft"
124     jinvoke-static ; inline
125
126 : shift> ( x by -- )
127     #! Shift 'by' bits to the right.
128     [ "java.lang.Number" "int" ]
129     "factor.math.FactorMath" "shiftRight"
130     jinvoke-static ; inline
131
132 : shift>> ( x by -- )
133     #! Shift 'by' bits to the right, without performing sign
134     #! extension.
135     [ "java.lang.Number" "int" ]
136     "factor.math.FactorMath" "shiftRightUnsigned"
137     jinvoke-static ; inline
138
139 : rem ( x y -- remainder )
140     [ "double" "double" ] "java.lang.Math" "IEEEremainder"
141     jinvoke-static ; inline
142
143 : gcd ( a b -- c )
144     [ "java.lang.Number" "java.lang.Number" ]
145     "factor.math.FactorMath" "gcd" jinvoke-static ;