1 ! Copyright (C) 2003, 2009 Slava Pestov, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
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 )
88 GENERIC: real-part ( z -- x )
89 GENERIC: imaginary-part ( z -- y )
91 MATH: number= ( x y -- ? ) foldable
93 M: object number= 2drop f ;
95 MATH: < ( x y -- ? ) foldable
96 MATH: <= ( x y -- ? ) foldable
97 MATH: > ( x y -- ? ) foldable
98 MATH: >= ( x y -- ? ) foldable
100 MATH: unordered? ( x y -- ? ) foldable
101 MATH: u< ( x y -- ? ) foldable
102 MATH: u<= ( x y -- ? ) foldable
103 MATH: u> ( x y -- ? ) foldable
104 MATH: u>= ( x y -- ? ) foldable
106 M: object unordered? 2drop f ;
108 MATH: + ( x y -- z ) foldable
109 MATH: - ( x y -- z ) foldable
110 MATH: * ( x y -- z ) foldable
111 MATH: / ( x y -- z ) foldable
112 MATH: /f ( x y -- z ) foldable
113 MATH: /i ( x y -- z ) foldable
114 MATH: mod ( x y -- z ) foldable
116 MATH: /mod ( x y -- z w ) foldable
118 MATH: bitand ( x y -- z ) foldable
119 MATH: bitor ( x y -- z ) foldable
120 MATH: bitxor ( x y -- z ) foldable
121 GENERIC# shift 1 ( x n -- y ) foldable
122 GENERIC: bitnot ( x -- y ) foldable
123 GENERIC# bit? 1 ( x n -- ? ) foldable
125 GENERIC: abs ( x -- y ) foldable
129 GENERIC: (log2) ( x -- n ) foldable
133 ERROR: log2-expects-positive x ;
136 dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline
138 : zero? ( x -- ? ) 0 number= ; inline
139 : 2/ ( x -- y ) -1 shift ; inline
140 : sq ( x -- y ) dup * ; inline
141 : neg ( x -- -x ) -1 * ; inline
142 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
143 : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
144 : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
145 : 2^ ( n -- 2^n ) 1 swap shift ; inline
146 : even? ( n -- ? ) 1 bitand zero? ; inline
147 : odd? ( n -- ? ) 1 bitand 1 number= ; inline
149 GENERIC: neg? ( x -- -x )
151 : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
152 [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
154 : when-zero ( ..a n quot: ( ..a -- ..b ) -- ..b ) [ ] if-zero ; inline
156 : unless-zero ( ..a n quot: ( ..a n -- ..b ) -- ..b ) [ ] swap if-zero ; inline
158 UNION: integer fixnum bignum ;
160 TUPLE: ratio { numerator integer read-only } { denominator integer read-only } ;
162 UNION: rational integer ratio ;
164 M: rational neg? 0 < ; inline
166 UNION: real rational float ;
168 TUPLE: complex { real real read-only } { imaginary real read-only } ;
170 UNION: number real complex ;
172 GENERIC: recip ( x -- y )
174 M: number recip 1 swap / ; inline
176 : fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
178 GENERIC: fp-special? ( x -- ? )
179 GENERIC: fp-nan? ( x -- ? )
180 GENERIC: fp-qnan? ( x -- ? )
181 GENERIC: fp-snan? ( x -- ? )
182 GENERIC: fp-infinity? ( x -- ? )
183 GENERIC: fp-nan-payload ( x -- bits )
184 GENERIC: fp-sign ( x -- ? )
186 M: object fp-special? drop f ; inline
187 M: object fp-nan? drop f ; inline
188 M: object fp-qnan? drop f ; inline
189 M: object fp-snan? drop f ; inline
190 M: object fp-infinity? drop f ; inline
192 : <fp-nan> ( payload -- nan )
193 0x7ff0000000000000 bitor bits>double ; inline
195 GENERIC: next-float ( m -- n )
196 GENERIC: prev-float ( m -- n )
198 : next-power-of-2 ( m -- n )
199 dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
201 : power-of-2? ( n -- ? )
202 dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
205 1 - [ + ] keep bitnot bitand ; inline
209 : iterate-prep ( n quot -- i n quot ) [ 0 ] 2dip ; inline
211 : if-iterate? ( i n true false -- ) [ 2over < ] 2dip if ; inline
213 : iterate-step ( i n quot -- i n quot )
214 #! Apply quot to i, keep i and quot, hide n.
215 [ nip call ] 3keep ; inline
217 : iterate-rot ( ? i n quot -- i n quot ? )
218 [ rot ] dip swap ; inline
220 : iterate-next ( i n quot -- i' n quot ) [ 1 + ] 2dip ; inline
224 : (each-integer) ( ... i n quot: ( ... i -- ... ) -- ... )
225 [ iterate-step iterate-next (each-integer) ]
226 [ 3drop ] if-iterate? ; inline recursive
228 : (find-integer) ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
230 iterate-step iterate-rot
231 [ 2drop ] [ iterate-next (find-integer) ] if
232 ] [ 3drop f ] if-iterate? ; inline recursive
234 : (all-integers?) ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
236 iterate-step iterate-rot
237 [ iterate-next (all-integers?) ] [ 3drop f ] if
238 ] [ 3drop t ] if-iterate? ; inline recursive
240 : each-integer ( ... n quot: ( ... i -- ... ) -- ... )
241 iterate-prep (each-integer) ; inline
243 : times ( ... n quot: ( ... -- ... ) -- ... )
244 [ drop ] prepose each-integer ; inline
246 : find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i )
247 iterate-prep (find-integer) ; inline
249 : all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
250 iterate-prep (all-integers?) ; inline
252 : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i )
259 [ 1 - ] dip find-last-integer
261 ] if ; inline recursive