]> gitweb.factorcode.org Git - factor.git/commitdiff
math: speed up integer>fixnum operations.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sat, 15 Sep 2012 15:52:06 +0000 (08:52 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 15 Sep 2012 15:52:06 +0000 (08:52 -0700)
core/math/integers/integers.factor
core/math/math.factor

index 27253b6da7200a5546b33b4c7fb37342157023e4..81dedc66dd969e70a207e8de9aedbd0021f26bd5 100644 (file)
@@ -15,8 +15,6 @@ M: fixnum >fixnum ; inline
 M: fixnum >bignum fixnum>bignum ; inline
 M: fixnum >integer ; inline
 M: fixnum >float fixnum>float ; inline
-M: fixnum integer>fixnum ; inline
-M: fixnum integer>fixnum-strict ; inline
 
 M: fixnum hashcode* nip ; inline
 M: fixnum equal? over bignum? [ >bignum bignum= ] [ 2drop f ] if ; inline
@@ -63,11 +61,6 @@ M: fixnum (log2) fixnum-log2 ; inline
 
 M: bignum >fixnum bignum>fixnum ; inline
 M: bignum >bignum ; inline
-M: bignum integer>fixnum bignum>fixnum ; inline
-
-M: bignum integer>fixnum-strict
-    dup bignum>fixnum
-    2dup number= [ nip ] [ drop out-of-fixnum-range ] if ; inline
 
 M: bignum hashcode* nip bignum>fixnum ;
 
index 20593bd48a89e1db518b1e3acf91e687f84adc9c..dcedc67963491cd31c3098c8dc3f1bbb92ce4204 100644 (file)
@@ -7,8 +7,6 @@ GENERIC: >fixnum ( x -- n ) foldable
 GENERIC: >bignum ( x -- n ) foldable
 GENERIC: >integer ( x -- n ) foldable
 GENERIC: >float ( x -- y ) foldable
-GENERIC: integer>fixnum ( x -- y ) foldable
-GENERIC: integer>fixnum-strict ( x -- y ) foldable
 
 GENERIC: numerator ( a/b -- a )
 GENERIC: denominator ( a/b -- b )
@@ -58,6 +56,8 @@ GENERIC: (log2) ( x -- n ) foldable
 
 PRIVATE>
 
+ERROR: not-an-integer n ;
+
 ERROR: out-of-fixnum-range n ;
 
 ERROR: log2-expects-positive x ;
@@ -87,6 +87,19 @@ GENERIC: neg? ( x -- -x )
 
 UNION: integer fixnum bignum ;
 
+: integer>fixnum ( m -- n )
+    dup fixnum? [
+        dup bignum? [ bignum>fixnum ] [ not-an-integer ] if
+    ] unless ; inline foldable
+
+: integer>fixnum-strict ( m -- n )
+    dup fixnum? [
+        dup bignum? [
+            dup bignum>fixnum 2dup number=
+            [ nip ] [ drop out-of-fixnum-range ] if
+        ] [ not-an-integer ] if
+    ] unless ; inline foldable
+
 TUPLE: ratio { numerator integer read-only } { denominator integer read-only } ;
 
 UNION: rational integer ratio ;