1 ! Copyright (C) 2003, 2009 Slava Pestov, Joe Groff.
2 ! See https://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: non-negative-number-expected n ;
136 : assert-non-negative ( n -- n )
137 dup 0 < [ non-negative-number-expected ] when ; inline
139 ERROR: positive-number-expected n ;
141 : assert-positive ( n -- n )
142 dup 0 > [ positive-number-expected ] unless ; inline
144 ERROR: negative-number-expected n ;
146 : assert-negative ( n -- n )
147 dup 0 < [ negative-number-expected ] unless ; inline
149 : recursive-hashcode ( n obj quot -- code )
150 pick 0 <= [ 3drop 0 ] [ [ 1 - ] 2dip call ] if ; inline
152 : log2 ( x -- n ) assert-positive (log2) ; inline
153 : zero? ( x -- ? ) 0 number= ; inline
154 : 2/ ( x -- y ) -1 shift ; inline
155 : sq ( x -- y ) dup * ; inline
156 : neg ( x -- -x ) -1 * ; inline
157 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
158 : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
159 : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
160 : 2^ ( n -- 2^n ) 1 swap shift ; inline
161 : even? ( n -- ? ) 1 bitand zero? ; inline
162 : odd? ( n -- ? ) 1 bitand 1 number= ; inline
164 : bit-length ( x -- n )
165 assert-non-negative dup 1 > [ log2 1 + ] when ;
167 GENERIC: neg? ( x -- ? )
169 : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
170 [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
172 : when-zero ( ... n quot: ( ... -- ... x ) -- ... x ) [ ] if-zero ; inline
174 : unless-zero ( ... n quot: ( ... n -- ... ) -- ... ) [ ] swap if-zero ; inline
176 : until-zero ( ... n quot: ( ... x -- ... y ) -- ... ) [ dup zero? ] swap until drop ; inline
178 UNION: integer fixnum bignum ;
181 { numerator integer read-only }
182 { denominator integer read-only } ;
184 UNION: rational integer ratio ;
186 M: rational neg? 0 < ; inline
188 UNION: real rational float ;
191 { real real read-only }
192 { imaginary real read-only } ;
194 UNION: number real complex ;
196 GENERIC: recip ( x -- y )
198 M: number recip 1 swap / ; inline
201 ! Note: an imaginary 0.0 should still create a complex
202 dup 0 = [ drop ] [ complex boa ] if ; inline
204 GENERIC: >rect ( z -- x y )
206 M: real >rect 0 ; inline
208 M: complex >rect [ real-part ] [ imaginary-part ] bi ; inline
212 : (gcd) ( b a x y -- a d )
216 [ /mod [ over * swapd - ] dip ] keep (gcd)
217 ] if-zero ; inline recursive
222 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; inline
224 MATH: simple-gcd ( x y -- d ) foldable
228 : fixnum-gcd ( x y -- d ) { fixnum fixnum } declare gcd nip ;
232 M: fixnum simple-gcd fixnum-gcd ; inline
234 M: bignum simple-gcd bignum-gcd ; inline
236 M: real simple-gcd gcd nip ; inline
239 [ * dup zero? ] 2keep '[ _ _ simple-gcd / ] unless ; foldable
241 : fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
243 GENERIC: fp-special? ( x -- ? )
244 GENERIC: fp-nan? ( x -- ? )
245 GENERIC: fp-qnan? ( x -- ? )
246 GENERIC: fp-snan? ( x -- ? )
247 GENERIC: fp-infinity? ( x -- ? )
248 GENERIC: fp-nan-payload ( x -- bits )
249 GENERIC: fp-sign ( x -- ? )
251 M: object fp-special? drop f ; inline
252 M: object fp-nan? drop f ; inline
253 M: object fp-qnan? drop f ; inline
254 M: object fp-snan? drop f ; inline
255 M: object fp-infinity? drop f ; inline
257 : <fp-nan> ( payload -- nan )
258 0x7ff0000000000000 bitor bits>double ; inline
260 GENERIC: next-float ( m -- n )
261 GENERIC: prev-float ( m -- n )
263 : next-power-of-2 ( m -- n )
264 dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
266 : power-of-2? ( n -- ? )
267 dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
270 1 - [ + ] keep bitnot bitand ; inline
272 : each-integer-from ( ... from to quot: ( ... i -- ... ) -- ... )
275 [ 1 + ] 2dip each-integer-from
278 ] if ; inline recursive
280 : each-integer ( ... n quot: ( ... i -- ... ) -- ... )
281 [ 0 ] 2dip each-integer-from ; inline
283 : times ( ... n quot: ( ... -- ... ) -- ... )
284 [ drop ] prepose each-integer ; inline
286 : find-integer-from ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
288 [ nip call ] 3keep roll
290 [ [ 1 + ] 2dip find-integer-from ] if
293 ] if ; inline recursive
295 : find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
296 [ 0 ] 2dip find-integer-from ; inline
298 : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
305 [ 1 - ] dip find-last-integer
307 ] if ; inline recursive
309 : all-integers-from? ( ... from to quot: ( ... i -- ... ? ) -- ... ? )
311 [ nip call ] 3keep roll
312 [ [ 1 + ] 2dip all-integers-from? ]
316 ] if ; inline recursive
318 : all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
319 [ 0 ] 2dip all-integers-from? ; inline