]> gitweb.factorcode.org Git - factor.git/blob - core/math/math.factor
math: faster definition of 2/ (allows inlining of fixnum-shift-fast).
[factor.git] / core / math / math.factor
1 ! Copyright (C) 2003, 2009 Slava Pestov, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel ;
4 IN: math
5
6 GENERIC: >fixnum ( x -- n ) foldable
7 GENERIC: >bignum ( x -- n ) foldable
8 GENERIC: >integer ( x -- n ) foldable
9 GENERIC: >float ( x -- y ) foldable
10 GENERIC: integer>fixnum ( x -- y ) foldable
11 GENERIC: integer>fixnum-strict ( x -- y ) foldable
12
13 GENERIC: numerator ( a/b -- a )
14 GENERIC: denominator ( a/b -- b )
15
16 GENERIC: real-part ( z -- x )
17 GENERIC: imaginary-part ( z -- y )
18
19 MATH: number= ( x y -- ? ) foldable
20
21 M: object number= 2drop f ;
22
23 MATH: <  ( x y -- ? ) foldable
24 MATH: <= ( x y -- ? ) foldable
25 MATH: >  ( x y -- ? ) foldable
26 MATH: >= ( x y -- ? ) foldable
27
28 MATH: unordered? ( x y -- ? ) foldable
29 MATH: u<  ( x y -- ? ) foldable
30 MATH: u<= ( x y -- ? ) foldable
31 MATH: u>  ( x y -- ? ) foldable
32 MATH: u>= ( x y -- ? ) foldable
33
34 M: object unordered? 2drop f ;
35
36 MATH: +   ( x y -- z ) foldable
37 MATH: -   ( x y -- z ) foldable
38 MATH: *   ( x y -- z ) foldable
39 MATH: /   ( x y -- z ) foldable
40 MATH: /f  ( x y -- z ) foldable
41 MATH: /i  ( x y -- z ) foldable
42 MATH: mod ( x y -- z ) foldable
43
44 MATH: /mod ( x y -- z w ) foldable
45
46 MATH: bitand ( x y -- z ) foldable
47 MATH: bitor  ( x y -- z ) foldable
48 MATH: bitxor ( x y -- z ) foldable
49 GENERIC# shift 1 ( x n -- y ) foldable
50 GENERIC: bitnot ( x -- y ) foldable
51 GENERIC# bit? 1 ( x n -- ? ) foldable
52
53 GENERIC: abs ( x -- y ) foldable
54
55 <PRIVATE
56
57 GENERIC: (log2) ( x -- n ) foldable
58
59 PRIVATE>
60
61 ERROR: out-of-fixnum-range n ;
62
63 ERROR: log2-expects-positive x ;
64
65 : log2 ( x -- n )
66     dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline
67
68 : zero? ( x -- ? ) 0 number= ; inline
69
70 ! the following lines are necessary because the "-1 shift"
71 ! definition doesn't (yet) compile as nicely...
72 GENERIC: 2/ ( x -- y ) foldable
73 M: bignum 2/ -1 bignum-shift ; inline
74 M: fixnum 2/ -1 fixnum-shift ; inline
75
76 : sq ( x -- y ) dup * ; inline
77 : neg ( x -- -x ) -1 * ; inline
78 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
79 : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
80 : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
81 : 2^ ( n -- 2^n ) 1 swap shift ; inline
82 : even? ( n -- ? ) 1 bitand zero? ; inline
83 : odd? ( n -- ? ) 1 bitand 1 number= ; inline
84
85 GENERIC: neg? ( x -- -x )
86
87 : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
88     [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
89
90 : when-zero ( ..a n quot: ( ..a -- ..b ) -- ..b ) [ ] if-zero ; inline
91
92 : unless-zero ( ..a n quot: ( ..a -- ..b ) -- ..b ) [ ] swap if-zero ; inline
93
94 UNION: integer fixnum bignum ;
95
96 TUPLE: ratio { numerator integer read-only } { denominator integer read-only } ;
97
98 UNION: rational integer ratio ;
99
100 M: rational neg? 0 < ; inline
101
102 UNION: real rational float ;
103
104 TUPLE: complex { real real read-only } { imaginary real read-only } ;
105
106 UNION: number real complex ;
107
108 GENERIC: recip ( x -- y )
109
110 M: number recip 1 swap / ; inline
111
112 : fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
113
114 GENERIC: fp-special? ( x -- ? )
115 GENERIC: fp-nan? ( x -- ? )
116 GENERIC: fp-qnan? ( x -- ? )
117 GENERIC: fp-snan? ( x -- ? )
118 GENERIC: fp-infinity? ( x -- ? )
119 GENERIC: fp-nan-payload ( x -- bits )
120 GENERIC: fp-sign ( x -- ? )
121
122 M: object fp-special? drop f ; inline
123 M: object fp-nan? drop f ; inline
124 M: object fp-qnan? drop f ; inline
125 M: object fp-snan? drop f ; inline
126 M: object fp-infinity? drop f ; inline
127
128 : <fp-nan> ( payload -- nan )
129     0x7ff0000000000000 bitor bits>double ; inline
130
131 GENERIC: next-float ( m -- n )
132 GENERIC: prev-float ( m -- n )
133
134 : next-power-of-2 ( m -- n )
135     dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
136
137 : power-of-2? ( n -- ? )
138     dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
139
140 : align ( m w -- n )
141     1 - [ + ] keep bitnot bitand ; inline
142
143 <PRIVATE
144
145 : iterate-prep ( n quot -- i n quot ) [ 0 ] 2dip ; inline
146
147 : if-iterate? ( i n true false -- ) [ 2over < ] 2dip if ; inline
148
149 : iterate-step ( i n quot -- i n quot )
150     #! Apply quot to i, keep i and quot, hide n.
151     [ nip call ] 3keep ; inline
152
153 : iterate-next ( i n quot -- i' n quot ) [ 1 + ] 2dip ; inline
154
155 PRIVATE>
156
157 : (each-integer) ( ... i n quot: ( ... i -- ... ) -- ... )
158     [ iterate-step iterate-next (each-integer) ]
159     [ 3drop ] if-iterate? ; inline recursive
160
161 : (find-integer) ( ... i n quot: ( ... i -- ... ? ) -- ... i )
162     [
163         iterate-step
164         [ [ ] ] 2dip
165         [ iterate-next (find-integer) ] 2curry bi-curry if
166     ] [ 3drop f ] if-iterate? ; inline recursive
167
168 : (all-integers?) ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
169     [
170         iterate-step
171         [ iterate-next (all-integers?) ] 3curry
172         [ f ] if
173     ] [ 3drop t ] if-iterate? ; inline recursive
174
175 : each-integer ( ... n quot: ( ... i -- ... ) -- ... )
176     iterate-prep (each-integer) ; inline
177
178 : times ( ... n quot: ( ... -- ... ) -- ... )
179     [ drop ] prepose each-integer ; inline
180
181 : find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i )
182     iterate-prep (find-integer) ; inline
183
184 : all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
185     iterate-prep (all-integers?) ; inline
186
187 : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i )
188     over 0 < [
189         2drop f
190     ] [
191         [ call ] 2keep rot [
192             drop
193         ] [
194             [ 1 - ] dip find-last-integer
195         ] if
196     ] if ; inline recursive