Update of /project/movitz/cvsroot/movitz/losp/muerte In directory common-lisp.net:/tmp/cvs-serv28989
Modified Files: ratios.lisp Log Message: Changed the implementation of ratios from a defstruct to a built-in structure.
Date: Sat Jul 31 16:35:09 2004 Author: ffjeld
Index: movitz/losp/muerte/ratios.lisp diff -u movitz/losp/muerte/ratios.lisp:1.4 movitz/losp/muerte/ratios.lisp:1.5 --- movitz/losp/muerte/ratios.lisp:1.4 Fri Jul 30 15:04:17 2004 +++ movitz/losp/muerte/ratios.lisp Sat Jul 31 16:35:09 2004 @@ -10,7 +10,7 @@ ;;;; Author: Frode Vatvedt Fjeld frodef@acm.org ;;;; Created at: Tue Jul 20 00:39:59 2004 ;;;; -;;;; $Id: ratios.lisp,v 1.4 2004/07/30 22:04:17 ffjeld Exp $ +;;;; $Id: ratios.lisp,v 1.5 2004/07/31 23:35:09 ffjeld Exp $ ;;;; ;;;;------------------------------------------------------------------
@@ -21,9 +21,37 @@
(in-package muerte)
-(defstruct (ratio (:constructor make-ratio (numerator denominator)) - (:superclass rational)) - numerator denominator) +;;;(defstruct (ratio (:constructor make-ratio (numerator denominator)) +;;; (:superclass rational)) +;;; numerator denominator) + +(defun make-ratio (numerator denominator) + (check-type numerator integer) + (check-type denominator (integer 1 *)) + (let ((ratio (malloc-pointer-words 4))) + (setf (memref ratio #.(bt:slot-offset 'movitz:movitz-basic-vector 'movitz::type) + 0 :unsigned-byte32) + #.(movitz:tag :ratio)) + (setf (memref ratio -6 2 :lisp) numerator + (memref ratio -6 3 :lisp) denominator) + ratio)) + +(defun ratio-p (x) + (typep x 'ratio)) + +(define-compiler-macro %ratio-numerator (x) + `(memref ,x ,(bt:slot-offset 'movitz::movitz-ratio 'movitz::numerator) 0 :lisp)) + +(defun ratio-numerator (x) + (check-type x ratio) + (%ratio-numerator x)) + +(define-compiler-macro %ratio-denominator (x) + `(memref ,x ,(bt:slot-offset 'movitz::movitz-ratio 'movitz::denominator) 0 :lisp)) + +(defun ratio-denominator (x) + (check-type x ratio) + (%ratio-denominator x))
(defun make-rational (numerator denominator) (check-type numerator integer) @@ -44,11 +72,11 @@ (defun numerator (x) (etypecase x (integer x) - (ratio (ratio-numerator x)))) + (ratio (%ratio-numerator x))))
(defun denominator (x) (etypecase x (integer 1) - (ratio (ratio-denominator x)))) + (ratio (%ratio-denominator x))))
(defconstant pi #xea7632a/4aa1a8b)