x86's floating-point rules around core's IEEE primitives (SSE, AVX, FMA).
(defconstant +mxcsr-de+) source(defconstant +mxcsr-de+ 1)(defconstant +mxcsr-daz+) source(defconstant +mxcsr-daz+ 6)(defconstant +mxcsr-ftz+) source(defconstant +mxcsr-ftz+ 15)(defconstant +mxcsr-reset+) source(defconstant +mxcsr-reset+ #x1F80)(defmacro x86-rc-code) source(defmacro x86-rc-code (rc)
"a rounding control field (MXCSR.RC, ROUNDSD's imm8[1:0]: 00 nearest even,
01 down, 10 up, 11 toward zero) as the kit's rounding mode (0, 4, 3, 2)"
`(case ,rc (0 0) (1 4) (2 3) (t 2)))(defmacro x86-rc) source(defmacro x86-rc (mxcsr)
"MXCSR.RC as the kit's rounding mode"
`(x86-rc-code (bit-field ,mxcsr 13 2)))(defmacro x86-daz) source(defmacro x86-daz (fmt x)
"the source X as read under MXCSR.DAZ"
`(if (= (bit-at mxcsr ,+mxcsr-daz+) 1) (fdaz ,fmt ,x) ,x))(defun x86-mxcsr-flags) source(defun x86-mxcsr-flags (f)
"IEEE flags (core's mask: invalid 1, divide-by-zero 2, overflow 4,
underflow 8, inexact 16) as MXCSR's IE (0), ZE (2), OE (3), UE (4), PE (5)"
(logior (logand f 1) (ash (logand f 30) 1)))(defmacro x86-de) source(defmacro x86-de (fmt flags &rest xs)
"MXCSR's denormal operand flag for the sources XS, the IEEE FLAGS of the
operation: a denormal source, unless DAZ reads it as zero or a higher
priority exception is taken instead: a NaN source, invalid, divide by zero
(Vol. 1, 11.5.2 *(unverified)*; as the CPU does, x86_fpdiff)"
`(if (and (= (bit-at mxcsr ,+mxcsr-daz+) 0)
(= (logand ,flags ,(logior +fflag-invalid+ +fflag-divzero+)) 0)
(not (or ,@(loop for x in xs collect `(fnan-p ,fmt ,x))))
(or ,@(loop for x in xs collect `(fsubnormal-p ,fmt ,x))))
,(ash 1 +mxcsr-de+)
0))(defmacro x86-nan-first) source(defmacro x86-nan-first (fmt sources result)
"RESULT, unless a source is a NaN: the first NaN of SOURCES quieted; an
invalid result (the primitive's default NaN) is the QNaN indefinite"
(let ((r (gensym "R")))
`(cond ,@(loop for s in sources collect `((fnan-p ,fmt ,s) (fquiet ,fmt ,s)))
(t (let ((,r ,result))
(if (fnan-p ,fmt ,r) (fneg ,fmt (fdefault-nan ,fmt)) ,r))))))(defmacro x86-fp) source(defmacro x86-fp (op fmt args)
"the IEEE primitive OP (fadd fsub fmul fdiv fsqrt) of FMT on the source
values ARGS, in the Operation's order (DEST := SRC1 + SRC2: SRC1 is the
first source of the NaN rule), as SSE/AVX compute it under MXCSR"
(let* ((vs (loop for a in args collect (gensym "S")))
(ds (loop for a in args collect (gensym "D")))
(r (gensym "R")) (f (gensym "F")))
`(let* (,@(mapcar #'list vs args)
,@(mapcar (lambda (d v) `(,d (x86-daz ,fmt ,v))) ds vs)
(,r (,op ,fmt (x86-rc mxcsr) ,@ds))
(,f (fflags ,op ,fmt (x86-rc mxcsr) ,@ds)))
(setq fpx (logior fpx (x86-mxcsr-flags ,f) (x86-de ,fmt ,f ,@vs)))
;; FTZ: an underflowing result is a zero, with the underflow and precision flags
(when (and (= (bit-at mxcsr ,+mxcsr-ftz+) 1) (fsubnormal-p ,fmt ,r))
(setq fpx (logior fpx (x86-mxcsr-flags (logior +fflag-underflow+ +fflag-inexact+)))
,r (fftz ,fmt ,r)))
(x86-nan-first ,fmt ,vs ,r))))(defmacro x86-fp-end) source(defmacro x86-fp-end ()
"the end of a floating-point instruction: its exceptions into MXCSR's
flags, and #XM for an unmasked one (MXCSR written, nothing else)"
`(progn
(setq mxcsr (logior mxcsr fpx))
(when (/= (logand fpx (lognot (bit-field mxcsr 7 6)) #x3F) 0)
(set-reg mxcsr mxcsr)
(raise :float))))(defmacro x86-fp-minmax) source(defmacro x86-fp-minmax (fmt args max)
"the SDM's MIN(SRC1, SRC2) of MINPD, MINPS, MINSD, MINSS (MAX: MAXPD...):
IF ((SRC1 = 0.0) and (SRC2 = 0.0)) THEN DEST := SRC2;
ELSE IF (SRC1 = NaN) THEN DEST := SRC2;
ELSE IF (SRC2 = NaN) THEN DEST := SRC2;
ELSE IF (SRC1 < SRC2) THEN DEST := SRC1; (MAX: SRC1 > SRC2)
ELSE DEST := SRC2;
on the sources as DAZ reads them; invalid for any NaN source, quiet or
signaling (the pages' SIMD Floating-Point Exceptions), else the denormal
operand flag"
(let ((s1 (gensym "S")) (s2 (gensym "S")) (d1 (gensym "D")) (d2 (gensym "D")) (f (gensym "F")))
`(let* ((,s1 ,(first args)) (,s2 ,(second args))
(,d1 (x86-daz ,fmt ,s1)) (,d2 (x86-daz ,fmt ,s2))
(,f (if (or (fnan-p ,fmt ,s1) (fnan-p ,fmt ,s2)) +fflag-invalid+ 0)))
(setq fpx (logior fpx (x86-mxcsr-flags ,f) (x86-de ,fmt ,f ,s1 ,s2)))
(cond ((and (fzero-p ,fmt ,d1) (fzero-p ,fmt ,d2)) ,d2)
((fnan-p ,fmt ,d1) ,d2)
((fnan-p ,fmt ,d2) ,d2)
((= ,(if max `(flt ,fmt ,d2 ,d1) `(flt ,fmt ,d1 ,d2)) 1) ,d1)
(t ,d2)))))(defmacro x86-fp-min) source(defmacro x86-fp-min (fmt args) "MINSD's MIN" `(x86-fp-minmax ,fmt ,args nil))(defmacro x86-fp-max) source(defmacro x86-fp-max (fmt args) "MAXSD's MAX" `(x86-fp-minmax ,fmt ,args t))(defmacro x86-fp-fma) source(defmacro x86-fp-fma (fmt args &rest options)
"the FMA pages' RoundFPControl_MXCSR(A*B + C) of the sources ARGS (A B C):
core's ffma, one rounding, in MXCSR's mode; :negate-product and
:negate-addend for FMSUB's A*B - C, FNMADD's -(A*B) + C, FNMSUB's
-(A*B) - C (the sign of the product or of C inverted, NaNs as they are).
The NaN rule takes the sources in the expression's order, A, B, C (as the
CPU does: x86_fpdiff, the 213 forms; VFMADD213SD's SRC2's NaN before
DEST's)"
(let* ((negp (member :negate-product options))
(negc (member :negate-addend options))
(vs (loop for a in args collect (gensym "S")))
(ds (loop for a in args collect (gensym "D")))
(r (gensym "R")) (f (gensym "F"))
(ops (list (if negp `(fneg ,fmt ,(first ds)) (first ds)) (second ds)
(if negc `(fneg ,fmt ,(third ds)) (third ds)))))
`(let* (,@(mapcar #'list vs args)
,@(mapcar (lambda (d v) `(,d (x86-daz ,fmt ,v))) ds vs)
(,r (ffma ,fmt (x86-rc mxcsr) ,@ops))
(,f (fflags ffma ,fmt (x86-rc mxcsr) ,@ops)))
(setq fpx (logior fpx (x86-mxcsr-flags ,f) (x86-de ,fmt ,f ,@vs)))
(when (and (= (bit-at mxcsr ,+mxcsr-ftz+) 1) (fsubnormal-p ,fmt ,r))
(setq fpx (logior fpx (x86-mxcsr-flags (logior +fflag-underflow+ +fflag-inexact+)))
,r (fftz ,fmt ,r)))
(x86-nan-first ,fmt ,vs ,r))))(defmacro x86-fp-cvt) source(defmacro x86-fp-cvt (to from args)
"CVTSD2SS's Convert_Double_Precision_To_Single_Precision_Floating_Point
(and the widening one) of the source (ARGS: one): core's fcvt in MXCSR's
mode under DAZ and FTZ; a NaN source converted as a NaN, quieted, its sign
and the high bits of its payload kept (Vol. 1, 4.8.3.5 *(unverified)*;
x86_fpdiff)"
(let ((s (gensym "S")) (d (gensym "D")) (r (gensym "R")) (f (gensym "F"))
(fm (second (float-format from))) (tm (second (float-format to))))
`(let* ((,s ,(first args)) (,d (x86-daz ,from ,s))
(,r (fcvt ,to ,from (x86-rc mxcsr) ,d))
(,f (fflags fcvt ,to ,from (x86-rc mxcsr) ,d)))
(setq fpx (logior fpx (x86-mxcsr-flags ,f) (x86-de ,from ,f ,s)))
(when (and (= (bit-at mxcsr ,+mxcsr-ftz+) 1) (fsubnormal-p ,to ,r))
(setq fpx (logior fpx (x86-mxcsr-flags (logior +fflag-underflow+ +fflag-inexact+)))
,r (fftz ,to ,r)))
(if (fnan-p ,from ,s)
(logior (fdefault-nan ,to) (ash (fsign ,from ,s) ,(reduce #'+ (float-format to)))
,(if (> tm fm)
`(ash (bit-field ,s 0 ,fm) ,(- tm fm))
`(bit-field ,s ,(- fm tm) ,tm)))
,r))))(defmacro x86-fp-from-int) source(defmacro x86-fp-from-int (to args)
"CVTSI2SD's Convert_Integer_To_Double_Precision_Floating_Point of the
integer (ARGS: one, signed or not as written): core's int-to-f in MXCSR's
mode; precision is its only exception"
(let ((i (gensym "I")))
`(let ((,i ,(first args)))
(setq fpx (logior fpx (x86-mxcsr-flags (fflags int-to-f ,to (x86-rc mxcsr) ,i))))
(int-to-f ,to (x86-rc mxcsr) ,i))))(defmacro x86-fp-to-int) source(defmacro x86-fp-to-int (from w signedness &rest options-and-args)
"CVTSD2SI's Convert_Double_Precision_Floating_Point_To_Integer of the
source (the last argument, a list of one): the integer of W bits it rounds
to in MXCSR's mode (:truncate: toward zero, CVTTSD2SI); a NaN or a value
out of range is the integer indefinite, 2^(W-1) (unsigned: 2^W - 1), and
invalid, as the pages' Description says ('the floating-point invalid
exception is raised, and if this exception is masked, the indefinite
integer value 80000000H is returned'); else precision if inexact"
(let* ((args (car (last options-and-args)))
(trunc (member :truncate options-and-args))
(signed (eq signedness :signed))
(lo (if signed (- (ash 1 (- w 1))) 0))
(hi (if signed (- (ash 1 (- w 1)) 1) (- (ash 1 w) 1)))
(s (gensym "S")) (d (gensym "D")) (rm (gensym "RM")) (v (gensym "V")))
`(let* ((,s ,(first args)) (,d (x86-daz ,from ,s))
(,rm ,(if trunc 2 '(x86-rc mxcsr)))
(,v (fcvt-to-int ,from ,rm ,d ,(- lo 1) ,(+ hi 1))))
(setq fpx (logior fpx (x86-mxcsr-flags (fflags fcvt-to-int ,from ,rm ,d ,lo ,hi))))
(if (or (fnan-p ,from ,d) (< ,v ,lo) (> ,v ,hi))
,(if signed (ash 1 (- w 1)) (- (ash 1 w) 1))
(wrap ,w ,v)))))(defmacro x86-fp-order) source(defmacro x86-fp-order (fmt a b)
"0 unordered, 1 A < B, 2 A = B, 3 A > B: core's quiet comparisons"
`(cond ((= (funord ,fmt ,a ,b) 1) 0) ((= (flt ,fmt ,a ,b) 1) 1) ((= (feq ,fmt ,a ,b) 1) 2) (t 3)))(defmacro x86-fp-cmp) source(defmacro x86-fp-cmp (fmt pred args)
"CMPSD's 'DEST[63:0] OP3 SRC[63:0]', the predicate PRED (0..31) of
*x86-cmp-predicates* on the sources ARGS (as DAZ reads them): 1 or 0;
invalid for an SNaN, or a QNaN under a signaling predicate (the pages'
SIMD Floating-Point Exceptions: 'Invalid if SNaN operand and invalid if
QNaN and predicate as listed in Table 3-8'), else denormal"
(let ((truth (loop for p in *x86-cmp-predicates* for k from 0
sum (ash (logand (x86-cmp-truth p) 15) (* 4 k))))
(signals (loop for p in *x86-cmp-predicates* for k from 0
sum (ash (bit-at (x86-cmp-truth p) 4) k)))
(s1 (gensym "S")) (s2 (gensym "S")) (o (gensym "O")) (p (gensym "P")) (f (gensym "F")))
`(let* ((,s1 ,(first args)) (,s2 ,(second args)) (,p ,pred)
(,o (x86-fp-order ,fmt (x86-daz ,fmt ,s1) (x86-daz ,fmt ,s2)))
(,f (if (or (fsnan-p ,fmt ,s1) (fsnan-p ,fmt ,s2) (and (= ,o 0) (= (bit-at ,signals ,p) 1)))
+fflag-invalid+ 0)))
(setq fpx (logior fpx (x86-mxcsr-flags ,f) (x86-de ,fmt ,f ,s1 ,s2)))
(bit-at ,truth (+ (* ,p 4) ,o)))))(defmacro x86-fp-compare) source(defmacro x86-fp-compare (fmt kind args)
"COMISD's OrderedCompare(DEST[63:0], SRC[63:0]) (KIND :signaling) and
UCOMISD's UnorderedCompare (:quiet) of ARGS as DAZ reads them: 0
unordered, 1 greater, 2 less, 3 equal; invalid for an SNaN (COMISD: any
NaN, 'COMISD signals an invalid operation exception if a source operand
is either a QNaN or SNaN'), else denormal"
(let ((s1 (gensym "S")) (s2 (gensym "S")) (o (gensym "O")) (f (gensym "F")))
`(let* ((,s1 ,(first args)) (,s2 ,(second args))
(,o (x86-fp-order ,fmt (x86-daz ,fmt ,s1) (x86-daz ,fmt ,s2)))
(,f (if ,(if (eq kind :signaling)
`(= ,o 0)
`(or (fsnan-p ,fmt ,s1) (fsnan-p ,fmt ,s2)))
+fflag-invalid+ 0)))
(setq fpx (logior fpx (x86-mxcsr-flags ,f) (x86-de ,fmt ,f ,s1 ,s2)))
(case ,o (0 0) (1 2) (2 3) (t 1)))))(defmacro x86-fp-round) source(defmacro x86-fp-round (fmt control args)
"ROUNDSD's RoundToInteger(SRC[63:0], ROUND_CONTROL) of the source (ARGS:
one): core's fround-int in the mode CONTROL (imm8) says, bits 1-0 as
MXCSR.RC, or MXCSR.RC's own when bit 2 is set; a NaN source quieted;
invalid for an SNaN, precision unless bit 3 suppresses it (the page's
Description and SIMD Floating-Point Exceptions)"
(let ((s (gensym "S")) (d (gensym "D")) (c (gensym "C")) (rm (gensym "RM")))
`(let* ((,s ,(first args)) (,d (x86-daz ,fmt ,s)) (,c ,control)
(,rm (if (= (bit-at ,c 2) 1) (x86-rc mxcsr) (x86-rc-code (bit-field ,c 0 2)))))
(setq fpx (logior fpx (x86-mxcsr-flags (logand (fflags fround-int ,fmt ,rm ,d)
(if (= (bit-at ,c 3) 1) ,(lognot +fflag-inexact+) -1)))))
(if (fnan-p ,fmt ,s) (fquiet ,fmt ,s) (fround-int ,fmt ,rm ,d)))))Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.