From 6ea9c0fc029ea8a798d750a279b68918ee43dee5 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sat, 15 Sep 2012 08:52:06 -0700 Subject: [PATCH] math: speed up integer>fixnum operations. --- core/math/integers/integers.factor | 7 ------- core/math/math.factor | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/core/math/integers/integers.factor b/core/math/integers/integers.factor index 27253b6da7..81dedc66dd 100644 --- a/core/math/integers/integers.factor +++ b/core/math/integers/integers.factor @@ -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 ; diff --git a/core/math/math.factor b/core/math/math.factor index 20593bd48a..dcedc67963 100644 --- a/core/math/math.factor +++ b/core/math/math.factor @@ -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 ;