1 ! Copyright (C) 2003, 2009 Slava Pestov, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel kernel.private ;
10 PRIMITIVE: bits>double ( n -- x )
11 PRIMITIVE: bits>float ( n -- x )
12 PRIMITIVE: double>bits ( x -- n )
13 PRIMITIVE: float>bits ( x -- n )
16 PRIMITIVE: bignum* ( x y -- z )
17 PRIMITIVE: bignum+ ( x y -- z )
18 PRIMITIVE: bignum- ( x y -- z )
19 PRIMITIVE: bignum-bit? ( x n -- ? )
20 PRIMITIVE: bignum-bitand ( x y -- z )
21 PRIMITIVE: bignum-bitnot ( x -- y )
22 PRIMITIVE: bignum-bitor ( x y -- z )
23 PRIMITIVE: bignum-bitxor ( x y -- z )
24 PRIMITIVE: bignum-gcd ( x y -- z )
25 PRIMITIVE: bignum-log2 ( x -- n )
26 PRIMITIVE: bignum-mod ( x y -- z )
27 PRIMITIVE: bignum-shift ( x y -- z )
28 PRIMITIVE: bignum/i ( x y -- z )
29 PRIMITIVE: bignum/mod ( x y -- z w )
30 PRIMITIVE: bignum< ( x y -- ? )
31 PRIMITIVE: bignum<= ( x y -- ? )
32 PRIMITIVE: bignum= ( x y -- ? )
33 PRIMITIVE: bignum> ( x y -- ? )
34 PRIMITIVE: bignum>= ( x y -- ? )
35 PRIMITIVE: bignum>fixnum ( x -- y )
36 PRIMITIVE: bignum>fixnum-strict ( x -- y )
37 PRIMITIVE: both-fixnums? ( x y -- ? )
38 PRIMITIVE: fixnum* ( x y -- z )
39 PRIMITIVE: fixnum*fast ( x y -- z )
40 PRIMITIVE: fixnum+ ( x y -- z )
41 PRIMITIVE: fixnum+fast ( x y -- z )
42 PRIMITIVE: fixnum- ( x y -- z )
43 PRIMITIVE: fixnum-bitand ( x y -- z )
44 PRIMITIVE: fixnum-bitnot ( x -- y )
45 PRIMITIVE: fixnum-bitor ( x y -- z )
46 PRIMITIVE: fixnum-bitxor ( x y -- z )
47 PRIMITIVE: fixnum-fast ( x y -- z )
48 PRIMITIVE: fixnum-mod ( x y -- z )
49 PRIMITIVE: fixnum-shift ( x y -- z )
50 PRIMITIVE: fixnum-shift-fast ( x y -- z )
51 PRIMITIVE: fixnum/i ( x y -- z )
52 PRIMITIVE: fixnum/i-fast ( x y -- z )
53 PRIMITIVE: fixnum/mod ( x y -- z w )
54 PRIMITIVE: fixnum/mod-fast ( x y -- z w )
55 PRIMITIVE: fixnum< ( x y -- ? )
56 PRIMITIVE: fixnum<= ( x y -- z )
57 PRIMITIVE: fixnum> ( x y -- ? )
58 PRIMITIVE: fixnum>= ( x y -- ? )
59 PRIMITIVE: fixnum>bignum ( x -- y )
60 PRIMITIVE: fixnum>float ( x -- y )
61 PRIMITIVE: float* ( x y -- z )
62 PRIMITIVE: float+ ( x y -- z )
63 PRIMITIVE: float- ( x y -- z )
64 PRIMITIVE: float-u< ( x y -- ? )
65 PRIMITIVE: float-u<= ( x y -- ? )
66 PRIMITIVE: float-u> ( x y -- ? )
67 PRIMITIVE: float-u>= ( x y -- ? )
68 PRIMITIVE: float/f ( x y -- z )
69 PRIMITIVE: float< ( x y -- ? )
70 PRIMITIVE: float<= ( x y -- ? )
71 PRIMITIVE: float= ( x y -- ? )
72 PRIMITIVE: float> ( x y -- ? )
73 PRIMITIVE: float>= ( x y -- ? )
74 PRIMITIVE: float>bignum ( x -- y )
75 PRIMITIVE: float>fixnum ( x -- y )
78 GENERIC: >fixnum ( x -- n ) foldable
79 GENERIC: >bignum ( x -- n ) foldable
80 GENERIC: >integer ( x -- n ) foldable
81 GENERIC: >float ( x -- y ) foldable
82 GENERIC: integer>fixnum ( x -- y ) foldable
83 GENERIC: integer>fixnum-strict ( x -- y ) foldable
85 GENERIC: numerator ( a/b -- a )
86 GENERIC: denominator ( a/b -- b )
87 GENERIC: >fraction ( a/b -- a b )
89 GENERIC: real-part ( z -- x )
90 GENERIC: imaginary-part ( z -- y )
92 MATH: number= ( x y -- ? ) foldable
94 M: object number= 2drop f ;
96 MATH: < ( x y -- ? ) foldable
97 MATH: <= ( x y -- ? ) foldable
98 MATH: > ( x y -- ? ) foldable
99 MATH: >= ( x y -- ? ) foldable
101 MATH: unordered? ( x y -- ? ) foldable
102 MATH: u< ( x y -- ? ) foldable
103 MATH: u<= ( x y -- ? ) foldable
104 MATH: u> ( x y -- ? ) foldable
105 MATH: u>= ( x y -- ? ) foldable
107 M: object unordered? 2drop f ;
109 MATH: + ( x y -- z ) foldable
110 MATH: - ( x y -- z ) foldable
111 MATH: * ( x y -- z ) foldable
112 MATH: / ( x y -- z ) foldable
113 MATH: /f ( x y -- z ) foldable
114 MATH: /i ( x y -- z ) foldable
115 MATH: mod ( x y -- z ) foldable
117 MATH: /mod ( x y -- z w ) foldable
119 MATH: bitand ( x y -- z ) foldable
120 MATH: bitor ( x y -- z ) foldable
121 MATH: bitxor ( x y -- z ) foldable
122 GENERIC#: shift 1 ( x n -- y ) foldable
123 GENERIC: bitnot ( x -- y ) foldable
124 GENERIC#: bit? 1 ( x n -- ? ) foldable
126 GENERIC: abs ( x -- y ) foldable
130 GENERIC: (log2) ( x -- n ) foldable
134 ERROR: log2-expects-positive x ;
137 dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline
139 : zero? ( x -- ? ) 0 number= ; inline
140 : 2/ ( x -- y ) -1 shift ; inline
141 : sq ( x -- y ) dup * ; inline
142 : neg ( x -- -x ) -1 * ; inline
143 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
144 : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
145 : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
146 : 2^ ( n -- 2^n ) 1 swap shift ; inline
147 : even? ( n -- ? ) 1 bitand zero? ; inline
148 : odd? ( n -- ? ) 1 bitand 1 number= ; inline
150 GENERIC: neg? ( x -- ? )
152 : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
153 [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
155 : when-zero ( ... n quot: ( ... -- ... x ) -- ... x ) [ ] if-zero ; inline
157 : unless-zero ( ... n quot: ( ... n -- ... ) -- ... ) [ ] swap if-zero ; inline
159 : until-zero ( ... n quot: ( ... x -- ... y ) -- ... ) [ dup zero? ] swap until drop ; inline
161 UNION: integer fixnum bignum ;
164 { numerator integer read-only }
165 { denominator integer read-only } ;
167 UNION: rational integer ratio ;
169 M: rational neg? 0 < ; inline
171 UNION: real rational float ;
174 { real real read-only }
175 { imaginary real read-only } ;
177 UNION: number real complex ;
179 GENERIC: recip ( x -- y )
181 M: number recip 1 swap / ; inline
184 ! Note: an imaginary 0.0 should still create a complex
185 dup 0 = [ drop ] [ complex boa ] if ; inline
187 GENERIC: >rect ( z -- x y )
189 M: real >rect 0 ; inline
191 M: complex >rect [ real-part ] [ imaginary-part ] bi ; inline
195 : (gcd) ( b a x y -- a d )
199 [ /mod [ over * swapd - ] dip ] keep (gcd)
200 ] if-zero ; inline recursive
205 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; inline
207 MATH: simple-gcd ( x y -- d ) foldable
211 : fixnum-gcd ( x y -- d ) { fixnum fixnum } declare gcd nip ;
215 M: fixnum simple-gcd fixnum-gcd ; inline
217 M: bignum simple-gcd bignum-gcd ; inline
219 : fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
221 GENERIC: fp-special? ( x -- ? )
222 GENERIC: fp-nan? ( x -- ? )
223 GENERIC: fp-qnan? ( x -- ? )
224 GENERIC: fp-snan? ( x -- ? )
225 GENERIC: fp-infinity? ( x -- ? )
226 GENERIC: fp-nan-payload ( x -- bits )
227 GENERIC: fp-sign ( x -- ? )
229 M: object fp-special? drop f ; inline
230 M: object fp-nan? drop f ; inline
231 M: object fp-qnan? drop f ; inline
232 M: object fp-snan? drop f ; inline
233 M: object fp-infinity? drop f ; inline
235 : <fp-nan> ( payload -- nan )
236 0x7ff0000000000000 bitor bits>double ; inline
238 GENERIC: next-float ( m -- n )
239 GENERIC: prev-float ( m -- n )
241 : next-power-of-2 ( m -- n )
242 dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
244 : power-of-2? ( n -- ? )
245 dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
248 1 - [ + ] keep bitnot bitand ; inline
252 : ((each-integer)) ( ... i n quot: ( ... i -- ... ) -- ... )
255 [ 1 + ] 2dip ((each-integer))
258 ] if ; inline recursive
260 : ((find-integer)) ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
262 [ nip call ] 3keep roll
264 [ [ 1 + ] 2dip ((find-integer)) ] if
267 ] if ; inline recursive
269 : ((all-integers?)) ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
271 [ nip call ] 3keep roll
272 [ [ 1 + ] 2dip ((all-integers?)) ]
276 ] if ; inline recursive
280 : (each-integer) ( ... i n quot: ( ... i -- ... ) -- ... )
281 2over both-fixnums? [ ((each-integer)) ] [ ((each-integer)) ] if ; inline
283 : (find-integer) ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
284 2over both-fixnums? [ ((find-integer)) ] [ ((find-integer)) ] if ; inline
286 : (all-integers?) ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
287 2over both-fixnums? [ ((all-integers?)) ] [ ((all-integers?)) ] if ; inline
289 : each-integer ( ... n quot: ( ... i -- ... ) -- ... )
290 [ 0 ] 2dip (each-integer) ; inline
292 : times ( ... n quot: ( ... -- ... ) -- ... )
293 [ drop ] prepose each-integer ; inline
295 : find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
296 [ 0 ] 2dip (find-integer) ; inline
298 : all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
299 [ 0 ] 2dip (all-integers?) ; inline
303 : (find-last-integer) ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
310 [ 1 - ] dip (find-last-integer)
312 ] if ; inline recursive
317 : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
318 over fixnum? [ (find-last-integer) ] [ (find-last-integer) ] if ; inline