Code fixes for test-matrix.
[sb-simd.git] / test-matrix.lisp
bloba2fcf4f11a18206adf7d6ff082216526c0748d32
1 #|
2 Copyright (c) 2005 Risto Laakso
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 (in-package :cl-user)
29 ; On my machine, naive is 30% faster without this; sse is 10% slower.
30 ;(declaim (optimize (speed 3) (space 0) (safety 0) (debug 0)))
33 (defun test-matrix (&optional (test-count 10000000))
34 (let ((mat1 (make-array 9 :element-type 'single-float :initial-element 0f0))
35 (mat2 (make-array 9 :element-type 'single-float :initial-element 0f0))
36 (naive (make-array 9 :element-type 'single-float))
37 (sse (make-array 9 :element-type 'single-float))
39 (declare (type (simple-array single-float (9)) naive sse)
40 (type fixnum test-count))
42 (loop for i of-type fixnum from 0 below 9 do (setf (aref mat1 i) (float (random 1f6))
43 (aref mat2 i) (float (random 1f6))))
45 (format t "Data: ~S~%~S~%" mat1 mat2)
47 (setf naive (naive-mul33 mat1 mat2)
48 sse (sse-mul33 mat1 mat2))
50 (format t "naive mul: ~S~%" naive)
51 (format t "sse mul: ~S~%" sse)
52 (format t "EQUALP? ~A~%" (loop for equal = t
53 for n of-type single-float across naive
54 for s of-type single-float across sse
55 when (/= n s) do (setq equal nil)
56 finally (return equal)))
57 (format t "naive, ~D ops ~%" test-count)
58 (time-sample-form #'(lambda ()
59 (dotimes (i test-count)
60 (setf naive (naive-mul33 mat1 mat2)))))
62 (format t "sse, ~D ops ~%" test-count)
63 (time-sample-form #'(lambda ()
64 (dotimes (i test-count)
65 (setf sse (sse-mul33 mat1 mat2)))))
69 (defun sse-mul33 (mat1 mat2)
70 (let ((res (make-array 9 :element-type 'single-float :initial-element 0f0)))
71 (declare (type (simple-array single-float (9)) mat1 mat2 res))
72 (sb-sys:%primitive sb-vm::%sse-matrix-mul-3x3/single-float res mat1 mat2)
73 res))
75 (defun naive-mul33 (mat1 mat2)
76 (let ((res (make-array 9 :element-type 'single-float :initial-element 0f0)))
77 (declare (type (simple-array single-float (9)) mat1 mat2 res))
78 (loop for row of-type fixnum from 0 to 2 do
79 (loop for col of-type fixnum from 0 to 2 do
80 (loop for elt of-type fixnum from 0 to 2 do
81 (incf (aref res (+ (* row 3) col))
82 (* (aref mat1 (+ (* row 3) elt)) (aref mat2 (+ (* elt 3) col)))))))
83 res))