| 1 |
;;; -*- mode: Lisp; indent-tabs-mode: nil; -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2010, Alexander Gavrilov ([email protected]) |
| 4 |
;;; |
| 5 |
;;; This file implements some common utility functions. |
| 6 |
;;; |
| 7 |
|
| 8 |
(in-package #:SSE) |
| 9 |
|
| 10 |
;;; CPU control |
| 11 |
|
| 12 |
(eval-when (:compile-toplevel :load-toplevel :execute) |
| 13 |
(declaim (ftype (function (&rest t) (unsigned-byte 32)) cpu-mxcsr-bits)) |
| 14 |
(defun cpu-mxcsr-bits (&rest tags) |
| 15 |
(loop with mask = 0 |
| 16 |
for tag in tags |
| 17 |
for bit = (if (listp tag) |
| 18 |
(apply #'cpu-mxcsr-bits tag) |
| 19 |
(ecase tag |
| 20 |
(:except-invalid #x1) |
| 21 |
(:except-denormal #x2) |
| 22 |
(:except-divide-zero #x4) |
| 23 |
(:except-overflow #x8) |
| 24 |
(:except-underflow #x10) |
| 25 |
(:except-precision #x20) |
| 26 |
(:except-all #x3F) |
| 27 |
(:denormals-are-zero #x40) |
| 28 |
(:mask-invalid #x80) |
| 29 |
(:mask-denormal #x100) |
| 30 |
(:mask-divide-zero #x200) |
| 31 |
(:mask-overflow #x400) |
| 32 |
(:mask-underflow #x800) |
| 33 |
(:mask-precision #x1000) |
| 34 |
(:mask-all #x1f80) |
| 35 |
(:round-nearest 0) |
| 36 |
(:round-negative #x2000) |
| 37 |
(:round-positive #x4000) |
| 38 |
(:round-zero #x6000) |
| 39 |
(:round-bits #x6000) |
| 40 |
(:flush-to-zero #x8000))) |
| 41 |
do (setf mask (logior mask bit)) |
| 42 |
finally (return mask))) |
| 43 |
(defun expand-cpu-mxcsr-bits (tags on-fail) |
| 44 |
(loop for tag in tags |
| 45 |
when (keywordp tag) collect tag into kwds |
| 46 |
else collect tag into rest |
| 47 |
finally |
| 48 |
(return |
| 49 |
(cond ((and kwds rest) |
| 50 |
`(logior ,(apply #'cpu-mxcsr-bits kwds) |
| 51 |
(cpu-mxcsr-bits ,@rest))) |
| 52 |
(kwds |
| 53 |
(apply #'cpu-mxcsr-bits kwds)) |
| 54 |
(t on-fail)))))) |
| 55 |
|
| 56 |
(define-compiler-macro cpu-mxcsr-bits (&whole whole &rest tags) |
| 57 |
(expand-cpu-mxcsr-bits tags whole)) |
| 58 |
|
| 59 |
(defmacro with-saved-mxcsr (&body code) |
| 60 |
(let ((v (gensym "CSR"))) |
| 61 |
`(let ((,v (cpu-mxcsr))) |
| 62 |
(declare (type (unsigned-byte 32) ,v) |
| 63 |
#+ecl (:read-only ,v)) |
| 64 |
(unwind-protect (progn ,@code) |
| 65 |
(%set-cpu-mxcsr ,v))))) |
| 66 |
|
| 67 |
#+nil |
| 68 |
(defun cpu-check-exceptions (&rest tags) |
| 69 |
(let ((mask (logand (cpu-mxcsr-bits (or tags :except-all)) |
| 70 |
(cpu-mxcsr-bits :except-all))) |
| 71 |
(csr (get-cpu-mxcsr))) |
| 72 |
(declare (optimize (safety 0) (speed 3) (debug 0)) |
| 73 |
(type fixnum csr mask)) |
| 74 |
(not (zerop (logand mask csr))))) |
| 75 |
|
| 76 |
#+nil |
| 77 |
(define-compiler-macro cpu-check-exceptions (&whole whole &rest tags) |
| 78 |
(let ((bits (expand-cpu-mxcsr-bits (or tags '(except-all)) nil))) |
| 79 |
(if (integerp bits) |
| 80 |
`(locally (declare (optimize (speed 3) (safety 0) (debug 0))) |
| 81 |
(not (zerop (logand (cpu-get-mxcsr) |
| 82 |
,(logand bits (cpu-mxcsr-bits :except-all)))))) |
| 83 |
whole))) |
| 84 |
|
| 85 |
#+nil |
| 86 |
(macrolet ((foo (&rest names) |
| 87 |
(let* ((kwds (mapcar (lambda (x) (intern (format nil "MASK-~A" x) :keyword)) names)) |
| 88 |
(pvars (mapcar (lambda (x) (intern (format nil "~A-P" x))) names))) |
| 89 |
`(defun cpu-mask-exceptions (&key |
| 90 |
,@(mapcar (lambda (n p) `(,n nil ,p)) names pvars) |
| 91 |
(other nil rest-p)) |
| 92 |
(let ((set-bits (logior ,@(mapcar (lambda (n k) `(if ,n (cpu-mxcsr-bits ,k) 0)) names kwds))) |
| 93 |
(arg-bits (logior ,@(mapcar (lambda (p k) `(if ,p (cpu-mxcsr-bits ,k) 0)) pvars kwds)))) |
| 94 |
(%set-cpu-mxcsr |
| 95 |
(the fixnum |
| 96 |
(if (not rest-p) |
| 97 |
(logior set-bits (logand (get-cpu-mxcsr) (lognot arg-bits))) |
| 98 |
(logior set-bits |
| 99 |
(if other (logand (cpu-mxcsr-bits :mask-all) (lognot arg-bits)) 0) |
| 100 |
(logiand (get-cpu-mxcsr) (lognot (cpu-mxcsr-bits :mask-all))))))) |
| 101 |
nil))))) |
| 102 |
(foo invalid denormal divide-zero overflow underflow precision)) |
| 103 |
|
| 104 |
(defun cpu-configure-rounding (&key round-to |
| 105 |
(denormals-are-zero nil daz-p) |
| 106 |
(flush-to-zero nil ftz-p)) |
| 107 |
(let ((set 0) |
| 108 |
(mask 0)) |
| 109 |
(when round-to |
| 110 |
(setf mask (cpu-mxcsr-bits :round-bits) |
| 111 |
set (ecase round-to |
| 112 |
(:zero (cpu-mxcsr-bits :round-zero)) |
| 113 |
(:negative (cpu-mxcsr-bits :round-negative)) |
| 114 |
(:positive (cpu-mxcsr-bits :round-positive)) |
| 115 |
(:nearest (cpu-mxcsr-bits :round-nearest))))) |
| 116 |
(when daz-p |
| 117 |
(setf mask (logior mask (cpu-mxcsr-bits :denormals-are-zero))) |
| 118 |
(when denormals-are-zero |
| 119 |
(setf set (logior set (cpu-mxcsr-bits :denormals-are-zero))))) |
| 120 |
(when ftz-p |
| 121 |
(setf mask (logior mask (cpu-mxcsr-bits :flush-to-zero))) |
| 122 |
(when flush-to-zero |
| 123 |
(setf set (logior set (cpu-mxcsr-bits :flush-to-zero))))) |
| 124 |
(setf (cpu-mxcsr) |
| 125 |
(the (unsigned-byte 32) |
| 126 |
(logior set (logand (cpu-mxcsr) (lognot mask))))) |
| 127 |
nil)) |
| 128 |
|