| 1 |
@node cl-simd |
| 2 |
@section cl-simd |
| 3 |
@cindex SSE2 Intrinsics |
| 4 |
@cindex Intrinsics, SSE2 |
| 5 |
|
| 6 |
The @code{cl-simd} module provides access to SSE2 instructions |
| 7 |
(which are nowadays supported by any CPU compatible with x86-64) |
| 8 |
in the form of @emph{intrinsic functions}, similar to the way |
| 9 |
adopted by modern C compilers. It also provides some lisp-specific |
| 10 |
functionality, like setf-able intrinsics for accessing lisp arrays. |
| 11 |
|
| 12 |
When this module is loaded, it defines an @code{:sse2} feature, |
| 13 |
which can be subsequently used for conditional compilation of |
| 14 |
code that depends on it. Intrinsic functions are available from |
| 15 |
the @code{sse} package. |
| 16 |
|
| 17 |
This API, with minor technical differences, is supported by both |
| 18 |
ECL and SBCL (x86-64 only). |
| 19 |
|
| 20 |
@menu |
| 21 |
* SSE pack types:: |
| 22 |
* SSE array type:: |
| 23 |
* Differences from C intrinsics:: |
| 24 |
* Comparisons and NaN handling:: |
| 25 |
* Simple extensions:: |
| 26 |
* Lisp array accessors:: |
| 27 |
* Example:: |
| 28 |
@end menu |
| 29 |
|
| 30 |
@node SSE pack types |
| 31 |
@subsection SSE pack types |
| 32 |
|
| 33 |
The package defines and/or exports the following types to |
| 34 |
represent 128-bit SSE register contents: |
| 35 |
|
| 36 |
@anchor{Type sse:sse-pack} |
| 37 |
@deftp {Type} @somepkg{sse-pack,sse} @&optional item-type |
| 38 |
The generic SSE pack type. |
| 39 |
@end deftp |
| 40 |
|
| 41 |
@anchor{Type sse:int-sse-pack} |
| 42 |
@deftp {Type} @somepkg{int-sse-pack,sse} |
| 43 |
Same as @code{(sse-pack integer)}. |
| 44 |
@end deftp |
| 45 |
|
| 46 |
@anchor{Type sse:float-sse-pack} |
| 47 |
@deftp {Type} @somepkg{float-sse-pack,sse} |
| 48 |
Same as @code{(sse-pack single-float)}. |
| 49 |
@end deftp |
| 50 |
|
| 51 |
@anchor{Type sse:double-sse-pack} |
| 52 |
@deftp {Type} @somepkg{double-sse-pack,sse} |
| 53 |
Same as @code{(sse-pack double-float)}. |
| 54 |
@end deftp |
| 55 |
|
| 56 |
Declaring variable types using the subtype appropriate |
| 57 |
for your data is likely to lead to more efficient code |
| 58 |
(especially on ECL). However, the compiler implicitly |
| 59 |
casts between any subtypes of sse-pack when needed. |
| 60 |
|
| 61 |
Printed representation of SSE packs can be controlled |
| 62 |
by binding @code{*sse-pack-print-mode*}: |
| 63 |
|
| 64 |
@anchor{Variable sse:*sse-pack-print-mode*} |
| 65 |
@defvr {Variable} @somepkg{@earmuffs{sse-pack-print-mode},sse} |
| 66 |
When set to one of @code{:int}, @code{:float} or |
| 67 |
@code{:double}, specifies the way SSE packs are |
| 68 |
printed. A @code{NIL} value (default) instructs |
| 69 |
the implementation to make its best effort to |
| 70 |
guess from the data and context. |
| 71 |
@end defvr |
| 72 |
|
| 73 |
@node SSE array type |
| 74 |
@subsection SSE array type |
| 75 |
|
| 76 |
@anchor{Type sse:sse-array} |
| 77 |
@deftp {Type} @somepkg{sse-array,sse} element-type @&optional dimensions |
| 78 |
Expands to a lisp array type that is efficiently |
| 79 |
supported by AREF-like accessors. |
| 80 |
It should be assumed to be a subtype of @code{SIMPLE-ARRAY}. |
| 81 |
The type expander signals warnings or errors if it detects |
| 82 |
that the element-type argument value is inappropriate or unsafe. |
| 83 |
@end deftp |
| 84 |
|
| 85 |
@anchor{Function sse:make-sse-array} |
| 86 |
@deffn {Function} @somepkg{make-sse-array,sse} dimensions @&key element-type initial-element displaced-to displaced-index-offset |
| 87 |
Creates an object of type @code{sse-array}, or signals an error. |
| 88 |
In non-displaced case ensures alignment of the beginning of data to |
| 89 |
the 16-byte boundary. |
| 90 |
Unlike @code{make-array}, the element type defaults to (unsigned-byte 8). |
| 91 |
@end deffn |
| 92 |
|
| 93 |
On ECL this function supports full-featured displacement. |
| 94 |
On SBCL it has to simulate it by sharing the underlying |
| 95 |
data vector, and does not support nonzero index offset. |
| 96 |
|
| 97 |
@node Differences from C intrinsics |
| 98 |
@subsection Differences from C intrinsics |
| 99 |
|
| 100 |
Intel Compiler, GCC and |
| 101 |
@url{http://msdn.microsoft.com/en-us/library/y0dh78ez%28VS.80%29.aspx,MSVC} |
| 102 |
all support the same set |
| 103 |
of SSE intrinsics, originally designed by Intel. This |
| 104 |
package generally follows the naming scheme of the C |
| 105 |
version, with the following exceptions: |
| 106 |
|
| 107 |
@itemize |
| 108 |
@item |
| 109 |
Underscores are replaced with dashes, and the @code{_mm_} |
| 110 |
prefix is removed in favor of packages. |
| 111 |
|
| 112 |
@item |
| 113 |
The 'e' from @code{epi} is dropped because MMX is obsolete |
| 114 |
and won't be supported. |
| 115 |
|
| 116 |
@item |
| 117 |
@code{_si128} functions are renamed to @code{-pi} for uniformity |
| 118 |
and brevity. The author has personally found this discrepancy |
| 119 |
in the original C intrinsics naming highly jarring. |
| 120 |
|
| 121 |
@item |
| 122 |
Comparisons are named using graphic characters, e.g. @code{<=-ps} |
| 123 |
for @code{cmpleps}, or @code{/>-ps} for @code{cmpngtps}. In some |
| 124 |
places the set of comparison functions is extended to cover the |
| 125 |
full possible range. |
| 126 |
|
| 127 |
@item |
| 128 |
Scalar comparison predicates are named like @code{..-ss?} for |
| 129 |
@code{comiss}, and @code{..-ssu?} for @code{ucomiss} wrappers. |
| 130 |
|
| 131 |
@item |
| 132 |
Conversion functions are renamed to @code{convert-*-to-*} and |
| 133 |
@code{truncate-*-to-*}. |
| 134 |
|
| 135 |
@item |
| 136 |
A few functions are completely renamed: @code{cpu-mxcsr} (setf-able), |
| 137 |
@code{cpu-pause}, @code{cpu-load-fence}, @code{cpu-store-fence}, |
| 138 |
@code{cpu-memory-fence}, @code{cpu-clflush}, @code{cpu-prefetch-*}. |
| 139 |
@end itemize |
| 140 |
|
| 141 |
In addition, foreign pointer access intrinsics have an additional |
| 142 |
optional integer offset parameter to allow more efficient coding |
| 143 |
of pointer deference, and the most common ones have been renamed |
| 144 |
and made SETF-able: |
| 145 |
|
| 146 |
@itemize |
| 147 |
@item |
| 148 |
@code{mem-ref-ss}, @code{mem-ref-ps}, @code{mem-ref-aps} |
| 149 |
|
| 150 |
@item |
| 151 |
@code{mem-ref-sd}, @code{mem-ref-pd}, @code{mem-ref-apd} |
| 152 |
|
| 153 |
@item |
| 154 |
@code{mem-ref-pi}, @code{mem-ref-api}, @code{mem-ref-si64} |
| 155 |
@end itemize |
| 156 |
|
| 157 |
(The @code{-ap*} version requires alignment.) |
| 158 |
|
| 159 |
@node Comparisons and NaN handling |
| 160 |
@subsection Comparisons and NaN handling |
| 161 |
|
| 162 |
Floating-point arithmetic intrinsics have trivial IEEE semantics |
| 163 |
when given QNaN and SNaN arguments. Comparisons have more complex |
| 164 |
behavior, detailed in the following table: |
| 165 |
|
| 166 |
@multitable { @code{/>=-ss, />=-ps} } { @code{/>=-sd, />=-pd} } { Not greater or equal } { Result for NaN } { QNaN traps } |
| 167 |
@item Single-float @tab Double-float @tab Condition @tab Result for NaN @tab QNaN traps |
| 168 |
@item @code{=-ss}, @code{=-ps} @tab @code{=-sd}, @code{=-pd} @tab Equal @tab False @tab No |
| 169 |
@item @code{<-ss}, @code{<-ps} @tab @code{<-sd}, @code{<-pd} @tab Less @tab False @tab Yes |
| 170 |
@item @code{<=-ss}, @code{<=-ps} @tab @code{<=-sd}, @code{<=-pd} @tab Less or equal @tab False @tab Yes |
| 171 |
@item @code{>-ss}, @code{>-ps} @tab @code{>-sd}, @code{>-pd} @tab Greater @tab False @tab Yes |
| 172 |
@item @code{>=-ss}, @code{>=-ps} @tab @code{>=-sd}, @code{>=-pd} @tab Greater or equal @tab False @tab Yes |
| 173 |
@item @code{/=-ss}, @code{/=-ps} @tab @code{/=-sd}, @code{/=-pd} @tab Not equal @tab True @tab No |
| 174 |
@item @code{/<-ss}, @code{/<-ps} @tab @code{/<-sd}, @code{/<-pd} @tab Not less @tab True @tab Yes |
| 175 |
@item @code{/<=-ss}, @code{/<=-ps} @tab @code{/<=-sd}, @code{/<=-pd} @tab Not less or equal @tab True @tab Yes |
| 176 |
@item @code{/>-ss}, @code{/>-ps} @tab @code{/>-sd}, @code{/>-pd} @tab Not greater @tab True @tab Yes |
| 177 |
@item @code{/>=-ss}, @code{/>=-ps} @tab @code{/>=-sd}, @code{/>=-pd} @tab Not greater or equal @tab True @tab Yes |
| 178 |
@item @code{cmpord-ss}, @code{cmpord-ps} @tab @code{cmpord-sd}, @code{cmpord-pd} |
| 179 |
@tab Ordered, i.e. no NaN args @tab False @tab No |
| 180 |
@item @code{cmpunord-ss}, @code{cmpunord-ps} @tab @code{cmpunord-sd}, @code{cmpunord-pd} |
| 181 |
@tab Unordered, i.e. with NaN args @tab True @tab No |
| 182 |
@end multitable |
| 183 |
|
| 184 |
Likewise for scalar comparison predicates, i.e. functions that return the |
| 185 |
result of the comparison as a Lisp boolean instead of a bitmask sse-pack: |
| 186 |
|
| 187 |
@multitable { Single-float } { Double-float } { Not greater or equal } { Result for NaN } { QNaN traps } |
| 188 |
@item Single-float @tab Double-float @tab Condition @tab Result for NaN @tab QNaN traps |
| 189 |
@item @code{=-ss?} @tab @code{=-sd?} @tab Equal @tab True @tab Yes |
| 190 |
@item @code{=-ssu?} @tab @code{=-sdu?} @tab Equal @tab True @tab No |
| 191 |
@item @code{<-ss?} @tab @code{<-sd?} @tab Less @tab True @tab Yes |
| 192 |
@item @code{<-ssu?} @tab @code{<-sdu?} @tab Less @tab True @tab No |
| 193 |
@item @code{<=-ss?} @tab @code{<=-sd?} @tab Less or equal @tab True @tab Yes |
| 194 |
@item @code{<=-ssu?} @tab @code{<=-sdu?} @tab Less or equal @tab True @tab No |
| 195 |
@item @code{>-ss?} @tab @code{>-sd?} @tab Greater @tab False @tab Yes |
| 196 |
@item @code{>-ssu?} @tab @code{>-sdu?} @tab Greater @tab False @tab No |
| 197 |
@item @code{>=-ss?} @tab @code{>=-sd?} @tab Greater or equal @tab False @tab Yes |
| 198 |
@item @code{>=-ssu?} @tab @code{>=-sdu?} @tab Greater or equal @tab False @tab No |
| 199 |
@item @code{/=-ss?} @tab @code{/=-sd?} @tab Not equal @tab False @tab Yes |
| 200 |
@item @code{/=-ssu?} @tab @code{/=-sdu?} @tab Not equal @tab False @tab No |
| 201 |
@end multitable |
| 202 |
|
| 203 |
Note that MSDN specifies different return values for the C counterparts of some |
| 204 |
of these functions when called with NaN arguments, but that seems to disagree |
| 205 |
with the actually generated code. |
| 206 |
|
| 207 |
@node Simple extensions |
| 208 |
@subsection Simple extensions |
| 209 |
|
| 210 |
This module extends the set of basic intrinsics with the following |
| 211 |
simple compound functions: |
| 212 |
|
| 213 |
@itemize |
| 214 |
@item |
| 215 |
@code{neg-ss}, @code{neg-ps}, @code{neg-sd}, @code{neg-pd}, |
| 216 |
@code{neg-pi8}, @code{neg-pi16}, @code{neg-pi32}, @code{neg-pi64}: |
| 217 |
|
| 218 |
implement numeric negation of the corresponding data type. |
| 219 |
|
| 220 |
@item |
| 221 |
@code{not-ps}, @code{not-pd}, @code{not-pi}: |
| 222 |
|
| 223 |
implement bitwise logical inversion. |
| 224 |
|
| 225 |
@item |
| 226 |
@code{if-ps}, @code{if-pd}, @code{if-pi}: |
| 227 |
|
| 228 |
perform element-wise combining of two values based on a boolean |
| 229 |
condition vector produced as a combination of comparison function |
| 230 |
results through bitwise logical functions. |
| 231 |
|
| 232 |
The condition value must use all-zero bitmask for false, and |
| 233 |
all-one bitmask for true as a value for each logical vector |
| 234 |
element. The result is undefined if any other bit pattern is used. |
| 235 |
|
| 236 |
N.B.: these are @emph{functions}, so both branches of the |
| 237 |
conditional are always evaluated. |
| 238 |
@end itemize |
| 239 |
|
| 240 |
The module also provides symbol macros that expand into expressions |
| 241 |
producing certain constants in the most efficient way: |
| 242 |
|
| 243 |
@itemize |
| 244 |
@item |
| 245 |
0.0-ps 0.0-pd 0-pi for zero |
| 246 |
|
| 247 |
@item |
| 248 |
true-ps true-pd true-pi for all 1 bitmask |
| 249 |
|
| 250 |
@item |
| 251 |
false-ps false-pd false-pi for all 0 bitmask (same as zero) |
| 252 |
@end itemize |
| 253 |
|
| 254 |
@node Lisp array accessors |
| 255 |
@subsection Lisp array accessors |
| 256 |
|
| 257 |
In order to provide better integration with ordinary lisp code, |
| 258 |
this module implements a set of AREF-like memory accessors: |
| 259 |
|
| 260 |
@itemize |
| 261 |
@item |
| 262 |
@code{(ROW-MAJOR-)?AREF-PREFETCH-(T0|T1|T2|NTA)} for cache prefetch. |
| 263 |
|
| 264 |
@item |
| 265 |
@code{(ROW-MAJOR-)?AREF-CLFLUSH} for cache flush. |
| 266 |
|
| 267 |
@item |
| 268 |
@code{(ROW-MAJOR-)?AREF-[AS]?P[SDI]} for whole-pack read & write. |
| 269 |
|
| 270 |
@item |
| 271 |
@code{(ROW-MAJOR-)?AREF-S(S|D|I64)} for scalar read & write. |
| 272 |
@end itemize |
| 273 |
|
| 274 |
(Where A = aligned; S = aligned streamed write.) |
| 275 |
|
| 276 |
These accessors can be used with any non-bit specialized |
| 277 |
array or vector, without restriction on the precise element |
| 278 |
type (although it should be declared at compile time to |
| 279 |
ensure generation of the fastest code). |
| 280 |
|
| 281 |
Additional index bound checking is done to ensure that enough |
| 282 |
bytes of memory are accessible after the specified index. |
| 283 |
|
| 284 |
As an exception, ROW-MAJOR-AREF-PREFETCH-* does not do any |
| 285 |
range checks at all, because the prefetch instructions |
| 286 |
are officially safe to use with bad addresses. The |
| 287 |
AREF-PREFETCH-* and *-CLFLUSH functions do only ordinary |
| 288 |
index checks without the usual 16-byte extension. |
| 289 |
|
| 290 |
@node Example |
| 291 |
@subsection Example |
| 292 |
|
| 293 |
This code processes several single-float arrays, storing |
| 294 |
either the value of a*b, or c/3.5 into result, depending |
| 295 |
on the sign of mode: |
| 296 |
|
| 297 |
@example |
| 298 |
(loop for i from 0 below 128 by 4 |
| 299 |
do (setf (aref-ps result i) |
| 300 |
(if-ps (<-ps (aref-ps mode i) 0.0-ps) |
| 301 |
(mul-ps (aref-ps a i) (aref-ps b i)) |
| 302 |
(div-ps (aref-ps c i) (set1-ps 3.5))))) |
| 303 |
@end example |
| 304 |
|
| 305 |
As already noted above, both branches of the if are always |
| 306 |
evaluated. |