Blog of Dimiter "malkia" Stanev mainly for Common Lisp.

Showing posts with label lisp. Show all posts
Showing posts with label lisp. Show all posts

Sunday, August 05, 2007

VMATH (Thinking out loud in Common Lisp)

;;; Some rough ideas about what the VMATH library might be
(in-package "VMATH")

;;; Scalar is the basic floating type.
(deftype scalar `single-float)

;;; This represents a packed (SIMD/Altivec) floating point vector of 4 components
(deftype vector4 `(simple-array scalar (4))

;;; Quaternions can be represented with the same structure of vector4
(deftype quat `vector4)

;;; XYZW Accessosrs
(defmacro x (v) (aref ,v 0))
(defmacro y (v) (aref ,v 1))
(defmacro z (v) (aref ,v 2))
(defmacro w (v) (aref ,v 3))

;;; Example
(defun v4add (va vb vr)
(declare (type vector4 va vb vr))
(setf (x vr) (+ (x va) (y va))
(y vr) (+ (y va) (y vb))
(z vr) (+ (z va) (z vb))
(w vr) (+ (w va) (z vb)))
vr)

;;; Which one sounds better:
;;;
;;; 1. V4+ ?
;;; 2. V4ADD ?
;;; 3. +V4 ?
;;; 4. ADDV4 ?
;;;
;;; My vote is for 2. More assembly like syntax.

;;; How to reduce the consing?
;;; Example:
;;; (sqrt some-value)
;;; That would cons, and more importantly is too generic
;;; (declare (type scalar some-value))
;;; (sqrt (the positive-scalar some-value))
;;; Would avoid consing on some of the Lisp Compilers