1 /** 2 * SSE intrinsics. 3 * https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#techs=SSE 4 * 5 * Copyright: Copyright Guillaume Piolat 2016-2020. 6 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) 7 */ 8 module inteli.xmmintrin; 9 10 public import inteli.types; 11 12 import inteli.internals; 13 14 import inteli.mmx; 15 import inteli.emmintrin; 16 17 import core.stdc.stdlib: malloc, free; 18 import core.stdc.string: memcpy; 19 import core.exception: onOutOfMemoryError; 20 21 version(D_InlineAsm_X86) 22 version = InlineX86Asm; 23 else version(D_InlineAsm_X86_64) 24 version = InlineX86Asm; 25 26 27 // SSE1 28 29 nothrow @nogc: 30 31 32 enum int _MM_EXCEPT_INVALID = 0x0001; /// MXCSR Exception states. 33 enum int _MM_EXCEPT_DENORM = 0x0002; ///ditto 34 enum int _MM_EXCEPT_DIV_ZERO = 0x0004; ///ditto 35 enum int _MM_EXCEPT_OVERFLOW = 0x0008; ///ditto 36 enum int _MM_EXCEPT_UNDERFLOW = 0x0010; ///ditto 37 enum int _MM_EXCEPT_INEXACT = 0x0020; ///ditto 38 enum int _MM_EXCEPT_MASK = 0x003f; /// MXCSR Exception states mask. 39 40 enum int _MM_MASK_INVALID = 0x0080; /// MXCSR Exception masks. 41 enum int _MM_MASK_DENORM = 0x0100; ///ditto 42 enum int _MM_MASK_DIV_ZERO = 0x0200; ///ditto 43 enum int _MM_MASK_OVERFLOW = 0x0400; ///ditto 44 enum int _MM_MASK_UNDERFLOW = 0x0800; ///ditto 45 enum int _MM_MASK_INEXACT = 0x1000; ///ditto 46 enum int _MM_MASK_MASK = 0x1f80; /// MXCSR Exception masks mask. 47 48 enum int _MM_ROUND_NEAREST = 0x0000; /// MXCSR Rounding mode. 49 enum int _MM_ROUND_DOWN = 0x2000; ///ditto 50 enum int _MM_ROUND_UP = 0x4000; ///ditto 51 enum int _MM_ROUND_TOWARD_ZERO = 0x6000; ///ditto 52 enum int _MM_ROUND_MASK = 0x6000; /// MXCSR Rounding mode mask. 53 54 enum int _MM_FLUSH_ZERO_MASK = 0x8000; /// MXCSR Denormal flush to zero mask. 55 enum int _MM_FLUSH_ZERO_ON = 0x8000; /// MXCSR Denormal flush to zero modes. 56 enum int _MM_FLUSH_ZERO_OFF = 0x0000; ///ditto 57 58 /// Add packed single-precision (32-bit) floating-point elements in `a` and `b`. 59 __m128 _mm_add_ps(__m128 a, __m128 b) pure @safe 60 { 61 pragma(inline, true); 62 return a + b; 63 } 64 unittest 65 { 66 __m128 a = [1, 2, 3, 4]; 67 a = _mm_add_ps(a, a); 68 assert(a.array[0] == 2); 69 assert(a.array[1] == 4); 70 assert(a.array[2] == 6); 71 assert(a.array[3] == 8); 72 } 73 74 /// Add the lower single-precision (32-bit) floating-point element 75 /// in `a` and `b`, store the result in the lower element of result, 76 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 77 __m128 _mm_add_ss(__m128 a, __m128 b) pure @safe 78 { 79 static if (GDC_with_SSE) 80 { 81 return __builtin_ia32_addss(a, b); 82 } 83 else static if (DMD_with_DSIMD) 84 { 85 return cast(__m128) __simd(XMM.ADDSS, a, b); 86 } 87 else 88 { 89 a[0] += b[0]; 90 return a; 91 } 92 } 93 unittest 94 { 95 __m128 a = [1, 2, 3, 4]; 96 a = _mm_add_ss(a, a); 97 assert(a.array == [2.0f, 2, 3, 4]); 98 } 99 100 /// Compute the bitwise AND of packed single-precision (32-bit) floating-point elements in `a` and `b`. 101 __m128 _mm_and_ps (__m128 a, __m128 b) pure @safe 102 { 103 pragma(inline, true); 104 return cast(__m128)(cast(__m128i)a & cast(__m128i)b); 105 } 106 unittest 107 { 108 float a = 4.32f; 109 float b = -78.99f; 110 int correct = (*cast(int*)(&a)) & (*cast(int*)(&b)); 111 __m128 A = _mm_set_ps(a, b, a, b); 112 __m128 B = _mm_set_ps(b, a, b, a); 113 int4 R = cast(int4)( _mm_and_ps(A, B) ); 114 assert(R.array[0] == correct); 115 assert(R.array[1] == correct); 116 assert(R.array[2] == correct); 117 assert(R.array[3] == correct); 118 } 119 120 /// Compute the bitwise NOT of packed single-precision (32-bit) floating-point elements in `a` and then AND with `b`. 121 __m128 _mm_andnot_ps (__m128 a, __m128 b) pure @safe 122 { 123 static if (DMD_with_DSIMD) 124 return cast(__m128) __simd(XMM.ANDNPS, a, b); 125 else 126 return cast(__m128)( (~cast(__m128i)a) & cast(__m128i)b ); 127 } 128 unittest 129 { 130 float a = 4.32f; 131 float b = -78.99f; 132 int correct = ~(*cast(int*)(&a)) & (*cast(int*)(&b)); 133 int correct2 = (*cast(int*)(&a)) & ~(*cast(int*)(&b)); 134 __m128 A = _mm_set_ps(a, b, a, b); 135 __m128 B = _mm_set_ps(b, a, b, a); 136 int4 R = cast(int4)( _mm_andnot_ps(A, B) ); 137 assert(R.array[0] == correct2); 138 assert(R.array[1] == correct); 139 assert(R.array[2] == correct2); 140 assert(R.array[3] == correct); 141 } 142 143 /// Average packed unsigned 16-bit integers in ``a` and `b`. 144 __m64 _mm_avg_pu16 (__m64 a, __m64 b) pure @safe 145 { 146 return to_m64(_mm_avg_epu16(to_m128i(a), to_m128i(b))); 147 } 148 149 /// Average packed unsigned 8-bit integers in ``a` and `b`. 150 __m64 _mm_avg_pu8 (__m64 a, __m64 b) pure @safe 151 { 152 return to_m64(_mm_avg_epu8(to_m128i(a), to_m128i(b))); 153 } 154 155 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` for equality. 156 __m128 _mm_cmpeq_ps (__m128 a, __m128 b) pure @safe 157 { 158 static if (DMD_with_DSIMD) 159 return cast(__m128) __simd(XMM.CMPPS, a, b, 0); 160 else 161 return cast(__m128) cmpps!(FPComparison.oeq)(a, b); 162 } 163 unittest 164 { 165 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 166 __m128 B = _mm_setr_ps(3.0f, 2.0f, float.nan, float.nan); 167 __m128i R = cast(__m128i) _mm_cmpeq_ps(A, B); 168 int[4] correct = [0, -1, 0, 0]; 169 assert(R.array == correct); 170 } 171 172 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` for equality, 173 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 174 __m128 _mm_cmpeq_ss (__m128 a, __m128 b) pure @safe 175 { 176 static if (DMD_with_DSIMD) 177 return cast(__m128) __simd(XMM.CMPSS, a, b, 0); 178 else 179 return cast(__m128) cmpss!(FPComparison.oeq)(a, b); 180 } 181 unittest 182 { 183 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 184 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 185 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 186 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 187 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 188 __m128i R1 = cast(__m128i) _mm_cmpeq_ss(A, B); 189 __m128i R2 = cast(__m128i) _mm_cmpeq_ss(A, C); 190 __m128i R3 = cast(__m128i) _mm_cmpeq_ss(A, D); 191 __m128i R4 = cast(__m128i) _mm_cmpeq_ss(A, E); 192 int[4] correct1 = [-1, 0, 0, 0]; 193 int[4] correct2 = [0, 0, 0, 0]; 194 int[4] correct3 = [0, 0, 0, 0]; 195 int[4] correct4 = [0, 0, 0, 0]; 196 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 197 } 198 199 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` for greater-than-or-equal. 200 __m128 _mm_cmpge_ps (__m128 a, __m128 b) pure @safe 201 { 202 static if (DMD_with_DSIMD) 203 return cast(__m128) __simd(XMM.CMPPS, b, a, 2); 204 else 205 return cast(__m128) cmpps!(FPComparison.oge)(a, b); 206 } 207 unittest 208 { 209 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 210 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 211 __m128i R = cast(__m128i) _mm_cmpge_ps(A, B); 212 int[4] correct = [0, -1,-1, 0]; 213 assert(R.array == correct); 214 } 215 216 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` for greater-than-or-equal, 217 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 218 __m128 _mm_cmpge_ss (__m128 a, __m128 b) pure @safe 219 { 220 static if (DMD_with_DSIMD) 221 { 222 __m128 c = cast(__m128) __simd(XMM.CMPSS, b, a, 2); 223 a[0] = c[0]; 224 return a; 225 } 226 else 227 return cast(__m128) cmpss!(FPComparison.oge)(a, b); 228 } 229 unittest 230 { 231 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 232 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 233 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 234 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 235 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 236 __m128i R1 = cast(__m128i) _mm_cmpge_ss(A, B); 237 __m128i R2 = cast(__m128i) _mm_cmpge_ss(A, C); 238 __m128i R3 = cast(__m128i) _mm_cmpge_ss(A, D); 239 __m128i R4 = cast(__m128i) _mm_cmpge_ss(A, E); 240 int[4] correct1 = [-1, 0, 0, 0]; 241 int[4] correct2 = [-1, 0, 0, 0]; 242 int[4] correct3 = [0, 0, 0, 0]; 243 int[4] correct4 = [0, 0, 0, 0]; 244 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 245 } 246 247 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` for greater-than. 248 __m128 _mm_cmpgt_ps (__m128 a, __m128 b) pure @safe 249 { 250 static if (DMD_with_DSIMD) 251 return cast(__m128) __simd(XMM.CMPPS, b, a, 1); 252 else 253 return cast(__m128) cmpps!(FPComparison.ogt)(a, b); 254 } 255 unittest 256 { 257 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 258 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 259 __m128i R = cast(__m128i) _mm_cmpgt_ps(A, B); 260 int[4] correct = [0, 0,-1, 0]; 261 assert(R.array == correct); 262 } 263 264 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` for greater-than, 265 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 266 __m128 _mm_cmpgt_ss (__m128 a, __m128 b) pure @safe 267 { 268 static if (DMD_with_DSIMD) 269 { 270 __m128 c = cast(__m128) __simd(XMM.CMPSS, b, a, 1); 271 a[0] = c[0]; 272 return a; 273 } 274 else 275 return cast(__m128) cmpss!(FPComparison.ogt)(a, b); 276 } 277 unittest 278 { 279 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 280 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 281 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 282 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 283 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 284 __m128i R1 = cast(__m128i) _mm_cmpgt_ss(A, B); 285 __m128i R2 = cast(__m128i) _mm_cmpgt_ss(A, C); 286 __m128i R3 = cast(__m128i) _mm_cmpgt_ss(A, D); 287 __m128i R4 = cast(__m128i) _mm_cmpgt_ss(A, E); 288 int[4] correct1 = [0, 0, 0, 0]; 289 int[4] correct2 = [-1, 0, 0, 0]; 290 int[4] correct3 = [0, 0, 0, 0]; 291 int[4] correct4 = [0, 0, 0, 0]; 292 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 293 } 294 295 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` for less-than-or-equal. 296 __m128 _mm_cmple_ps (__m128 a, __m128 b) pure @safe 297 { 298 static if (DMD_with_DSIMD) 299 return cast(__m128) __simd(XMM.CMPPS, a, b, 2); 300 else 301 return cast(__m128) cmpps!(FPComparison.ole)(a, b); 302 } 303 unittest 304 { 305 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 306 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 307 __m128i R = cast(__m128i) _mm_cmple_ps(A, B); 308 int[4] correct = [-1, -1, 0, 0]; 309 assert(R.array == correct); 310 } 311 312 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` for less-than-or-equal, 313 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 314 __m128 _mm_cmple_ss (__m128 a, __m128 b) pure @safe 315 { 316 static if (DMD_with_DSIMD) 317 return cast(__m128) __simd(XMM.CMPSS, a, b, 2); 318 else 319 return cast(__m128) cmpss!(FPComparison.ole)(a, b); 320 } 321 unittest 322 { 323 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 324 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 325 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 326 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 327 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 328 __m128i R1 = cast(__m128i) _mm_cmple_ss(A, B); 329 __m128i R2 = cast(__m128i) _mm_cmple_ss(A, C); 330 __m128i R3 = cast(__m128i) _mm_cmple_ss(A, D); 331 __m128i R4 = cast(__m128i) _mm_cmple_ss(A, E); 332 int[4] correct1 = [-1, 0, 0, 0]; 333 int[4] correct2 = [0, 0, 0, 0]; 334 int[4] correct3 = [0, 0, 0, 0]; 335 int[4] correct4 = [-1, 0, 0, 0]; 336 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 337 } 338 339 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` for less-than. 340 __m128 _mm_cmplt_ps (__m128 a, __m128 b) pure @safe 341 { 342 static if (DMD_with_DSIMD) 343 return cast(__m128) __simd(XMM.CMPPS, a, b, 1); 344 else 345 return cast(__m128) cmpps!(FPComparison.olt)(a, b); 346 } 347 unittest 348 { 349 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 350 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 351 __m128i R = cast(__m128i) _mm_cmplt_ps(A, B); 352 int[4] correct = [-1, 0, 0, 0]; 353 assert(R.array == correct); 354 } 355 356 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` for less-than, 357 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 358 __m128 _mm_cmplt_ss (__m128 a, __m128 b) pure @safe 359 { 360 static if (DMD_with_DSIMD) 361 return cast(__m128) __simd(XMM.CMPSS, a, b, 1); 362 else 363 return cast(__m128) cmpss!(FPComparison.olt)(a, b); 364 } 365 unittest 366 { 367 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 368 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 369 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 370 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 371 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 372 __m128i R1 = cast(__m128i) _mm_cmplt_ss(A, B); 373 __m128i R2 = cast(__m128i) _mm_cmplt_ss(A, C); 374 __m128i R3 = cast(__m128i) _mm_cmplt_ss(A, D); 375 __m128i R4 = cast(__m128i) _mm_cmplt_ss(A, E); 376 int[4] correct1 = [0, 0, 0, 0]; 377 int[4] correct2 = [0, 0, 0, 0]; 378 int[4] correct3 = [0, 0, 0, 0]; 379 int[4] correct4 = [-1, 0, 0, 0]; 380 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 381 } 382 383 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` for not-equal. 384 __m128 _mm_cmpneq_ps (__m128 a, __m128 b) pure @safe 385 { 386 static if (DMD_with_DSIMD) 387 return cast(__m128) __simd(XMM.CMPPS, a, b, 4); 388 else 389 return cast(__m128) cmpps!(FPComparison.une)(a, b); 390 } 391 unittest 392 { 393 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 394 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 395 __m128i R = cast(__m128i) _mm_cmpneq_ps(A, B); 396 int[4] correct = [-1, 0, -1, -1]; 397 assert(R.array == correct); 398 } 399 400 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` for not-equal, 401 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 402 __m128 _mm_cmpneq_ss (__m128 a, __m128 b) pure @safe 403 { 404 static if (DMD_with_DSIMD) 405 return cast(__m128) __simd(XMM.CMPSS, a, b, 4); 406 else 407 return cast(__m128) cmpss!(FPComparison.une)(a, b); 408 } 409 unittest 410 { 411 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 412 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 413 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 414 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 415 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 416 __m128i R1 = cast(__m128i) _mm_cmpneq_ss(A, B); 417 __m128i R2 = cast(__m128i) _mm_cmpneq_ss(A, C); 418 __m128i R3 = cast(__m128i) _mm_cmpneq_ss(A, D); 419 __m128i R4 = cast(__m128i) _mm_cmpneq_ss(A, E); 420 int[4] correct1 = [0, 0, 0, 0]; 421 int[4] correct2 = [-1, 0, 0, 0]; 422 int[4] correct3 = [-1, 0, 0, 0]; 423 int[4] correct4 = [-1, 0, 0, 0]; 424 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 425 } 426 427 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` for not-greater-than-or-equal. 428 __m128 _mm_cmpnge_ps (__m128 a, __m128 b) pure @safe 429 { 430 static if (DMD_with_DSIMD) 431 return cast(__m128) __simd(XMM.CMPPS, b, a, 6); 432 else 433 return cast(__m128) cmpps!(FPComparison.ult)(a, b); 434 } 435 unittest 436 { 437 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 438 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 439 __m128i R = cast(__m128i) _mm_cmpnge_ps(A, B); 440 int[4] correct = [-1, 0, 0, -1]; 441 assert(R.array == correct); 442 } 443 444 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` for not-greater-than-or-equal, 445 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 446 __m128 _mm_cmpnge_ss (__m128 a, __m128 b) pure @safe 447 { 448 static if (DMD_with_DSIMD) 449 { 450 __m128 c = cast(__m128) __simd(XMM.CMPSS, b, a, 6); 451 a[0] = c[0]; 452 return a; 453 } 454 else 455 return cast(__m128) cmpss!(FPComparison.ult)(a, b); 456 } 457 unittest 458 { 459 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 460 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 461 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 462 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 463 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 464 __m128i R1 = cast(__m128i) _mm_cmpnge_ss(A, B); 465 __m128i R2 = cast(__m128i) _mm_cmpnge_ss(A, C); 466 __m128i R3 = cast(__m128i) _mm_cmpnge_ss(A, D); 467 __m128i R4 = cast(__m128i) _mm_cmpnge_ss(A, E); 468 int[4] correct1 = [0, 0, 0, 0]; 469 int[4] correct2 = [0, 0, 0, 0]; 470 int[4] correct3 = [-1, 0, 0, 0]; 471 int[4] correct4 = [-1, 0, 0, 0]; 472 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 473 } 474 475 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` for not-greater-than. 476 __m128 _mm_cmpngt_ps (__m128 a, __m128 b) pure @safe 477 { 478 static if (DMD_with_DSIMD) 479 return cast(__m128) __simd(XMM.CMPPS, b, a, 5); 480 else 481 return cast(__m128) cmpps!(FPComparison.ule)(a, b); 482 } 483 unittest 484 { 485 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 486 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 487 __m128i R = cast(__m128i) _mm_cmpngt_ps(A, B); 488 int[4] correct = [-1, -1, 0, -1]; 489 assert(R.array == correct); 490 } 491 492 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` for not-greater-than, 493 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 494 __m128 _mm_cmpngt_ss (__m128 a, __m128 b) pure @safe 495 { 496 static if (DMD_with_DSIMD) 497 { 498 __m128 c = cast(__m128) __simd(XMM.CMPSS, b, a, 5); 499 a[0] = c[0]; 500 return a; 501 } 502 else 503 return cast(__m128) cmpss!(FPComparison.ule)(a, b); 504 } 505 unittest 506 { 507 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 508 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 509 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 510 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 511 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 512 __m128i R1 = cast(__m128i) _mm_cmpngt_ss(A, B); 513 __m128i R2 = cast(__m128i) _mm_cmpngt_ss(A, C); 514 __m128i R3 = cast(__m128i) _mm_cmpngt_ss(A, D); 515 __m128i R4 = cast(__m128i) _mm_cmpngt_ss(A, E); 516 int[4] correct1 = [-1, 0, 0, 0]; 517 int[4] correct2 = [0, 0, 0, 0]; 518 int[4] correct3 = [-1, 0, 0, 0]; 519 int[4] correct4 = [-1, 0, 0, 0]; 520 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 521 } 522 523 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` for not-less-than-or-equal. 524 __m128 _mm_cmpnle_ps (__m128 a, __m128 b) pure @safe 525 { 526 static if (DMD_with_DSIMD) 527 return cast(__m128) __simd(XMM.CMPPS, a, b, 6); 528 else 529 return cast(__m128) cmpps!(FPComparison.ugt)(a, b); 530 } 531 unittest 532 { 533 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 534 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 535 __m128i R = cast(__m128i) _mm_cmpnle_ps(A, B); 536 int[4] correct = [0, 0, -1, -1]; 537 assert(R.array == correct); 538 } 539 540 541 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` for not-less-than-or-equal, 542 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 543 __m128 _mm_cmpnle_ss (__m128 a, __m128 b) pure @safe 544 { 545 static if (DMD_with_DSIMD) 546 return cast(__m128) __simd(XMM.CMPSS, a, b, 6); 547 else 548 return cast(__m128) cmpss!(FPComparison.ugt)(a, b); 549 } 550 unittest 551 { 552 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 553 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 554 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 555 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 556 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 557 __m128i R1 = cast(__m128i) _mm_cmpnle_ss(A, B); 558 __m128i R2 = cast(__m128i) _mm_cmpnle_ss(A, C); 559 __m128i R3 = cast(__m128i) _mm_cmpnle_ss(A, D); 560 __m128i R4 = cast(__m128i) _mm_cmpnle_ss(A, E); 561 int[4] correct1 = [0, 0, 0, 0]; 562 int[4] correct2 = [-1, 0, 0, 0]; 563 int[4] correct3 = [-1, 0, 0, 0]; 564 int[4] correct4 = [0, 0, 0, 0]; 565 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 566 } 567 568 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` for not-less-than. 569 __m128 _mm_cmpnlt_ps (__m128 a, __m128 b) pure @safe 570 { 571 static if (DMD_with_DSIMD) 572 return cast(__m128) __simd(XMM.CMPPS, a, b, 5); 573 else 574 return cast(__m128) cmpps!(FPComparison.uge)(a, b); 575 } 576 unittest 577 { 578 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 579 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 580 __m128i R = cast(__m128i) _mm_cmpnlt_ps(A, B); 581 int[4] correct = [0, -1, -1, -1]; 582 assert(R.array == correct); 583 } 584 585 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` for not-less-than, 586 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 587 __m128 _mm_cmpnlt_ss (__m128 a, __m128 b) pure @safe 588 { 589 static if (DMD_with_DSIMD) 590 return cast(__m128) __simd(XMM.CMPSS, a, b, 5); 591 else 592 return cast(__m128) cmpss!(FPComparison.uge)(a, b); 593 } 594 unittest 595 { 596 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 597 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 598 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 599 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 600 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 601 __m128i R1 = cast(__m128i) _mm_cmpnlt_ss(A, B); 602 __m128i R2 = cast(__m128i) _mm_cmpnlt_ss(A, C); 603 __m128i R3 = cast(__m128i) _mm_cmpnlt_ss(A, D); 604 __m128i R4 = cast(__m128i) _mm_cmpnlt_ss(A, E); 605 int[4] correct1 = [-1, 0, 0, 0]; 606 int[4] correct2 = [-1, 0, 0, 0]; 607 int[4] correct3 = [-1, 0, 0, 0]; 608 int[4] correct4 = [0, 0, 0, 0]; 609 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 610 } 611 612 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` to see if neither is NaN. 613 __m128 _mm_cmpord_ps (__m128 a, __m128 b) pure @safe 614 { 615 static if (DMD_with_DSIMD) 616 return cast(__m128) __simd(XMM.CMPPS, a, b, 7); 617 else 618 return cast(__m128) cmpps!(FPComparison.ord)(a, b); 619 } 620 unittest 621 { 622 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 623 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 624 __m128i R = cast(__m128i) _mm_cmpord_ps(A, B); 625 int[4] correct = [-1, -1, -1, 0]; 626 assert(R.array == correct); 627 } 628 629 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` to see if neither is NaN, 630 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 631 __m128 _mm_cmpord_ss (__m128 a, __m128 b) pure @safe 632 { 633 static if (DMD_with_DSIMD) 634 return cast(__m128) __simd(XMM.CMPSS, a, b, 7); 635 else 636 return cast(__m128) cmpss!(FPComparison.ord)(a, b); 637 } 638 unittest 639 { 640 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 641 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 642 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 643 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 644 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 645 __m128i R1 = cast(__m128i) _mm_cmpord_ss(A, B); 646 __m128i R2 = cast(__m128i) _mm_cmpord_ss(A, C); 647 __m128i R3 = cast(__m128i) _mm_cmpord_ss(A, D); 648 __m128i R4 = cast(__m128i) _mm_cmpord_ss(A, E); 649 int[4] correct1 = [-1, 0, 0, 0]; 650 int[4] correct2 = [-1, 0, 0, 0]; 651 int[4] correct3 = [0, 0, 0, 0]; 652 int[4] correct4 = [-1, 0, 0, 0]; 653 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 654 } 655 656 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b` to see if either is NaN. 657 __m128 _mm_cmpunord_ps (__m128 a, __m128 b) pure @safe 658 { 659 static if (DMD_with_DSIMD) 660 return cast(__m128) __simd(XMM.CMPPS, a, b, 3); 661 else 662 return cast(__m128) cmpps!(FPComparison.uno)(a, b); 663 } 664 unittest 665 { 666 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, float.nan); 667 __m128 B = _mm_setr_ps(3.0f, 2.0f, 1.0f, float.nan); 668 __m128i R = cast(__m128i) _mm_cmpunord_ps(A, B); 669 int[4] correct = [0, 0, 0, -1]; 670 assert(R.array == correct); 671 } 672 673 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b` to see if either is NaN. 674 /// and copy the upper 3 packed elements from `a` to the upper elements of result. 675 __m128 _mm_cmpunord_ss (__m128 a, __m128 b) pure @safe 676 { 677 static if (DMD_with_DSIMD) 678 return cast(__m128) __simd(XMM.CMPSS, a, b, 3); 679 else return cast(__m128) cmpss!(FPComparison.uno)(a, b); 680 } 681 unittest 682 { 683 __m128 A = _mm_setr_ps(3.0f, 0, 0, 0); 684 __m128 B = _mm_setr_ps(3.0f, float.nan, float.nan, float.nan); 685 __m128 C = _mm_setr_ps(2.0f, float.nan, float.nan, float.nan); 686 __m128 D = _mm_setr_ps(float.nan, float.nan, float.nan, float.nan); 687 __m128 E = _mm_setr_ps(4.0f, float.nan, float.nan, float.nan); 688 __m128i R1 = cast(__m128i) _mm_cmpunord_ss(A, B); 689 __m128i R2 = cast(__m128i) _mm_cmpunord_ss(A, C); 690 __m128i R3 = cast(__m128i) _mm_cmpunord_ss(A, D); 691 __m128i R4 = cast(__m128i) _mm_cmpunord_ss(A, E); 692 int[4] correct1 = [0, 0, 0, 0]; 693 int[4] correct2 = [0, 0, 0, 0]; 694 int[4] correct3 = [-1, 0, 0, 0]; 695 int[4] correct4 = [0, 0, 0, 0]; 696 assert(R1.array == correct1 && R2.array == correct2 && R3.array == correct3 && R4.array == correct4); 697 } 698 699 700 /// Compare the lower single-precision (32-bit) floating-point element in `a` and `b` for equality, 701 /// and return the boolean result (0 or 1). 702 int _mm_comieq_ss (__m128 a, __m128 b) pure @safe 703 { 704 return a.array[0] == b.array[0]; 705 } 706 unittest 707 { 708 assert(1 == _mm_comieq_ss(_mm_set_ss(78.0f), _mm_set_ss(78.0f))); 709 assert(0 == _mm_comieq_ss(_mm_set_ss(78.0f), _mm_set_ss(-78.0f))); 710 assert(0 == _mm_comieq_ss(_mm_set_ss(78.0f), _mm_set_ss(float.nan))); 711 assert(0 == _mm_comieq_ss(_mm_set_ss(float.nan), _mm_set_ss(-4.22f))); 712 assert(1 == _mm_comieq_ss(_mm_set_ss(0.0), _mm_set_ss(-0.0))); 713 } 714 715 /// Compare the lower single-precision (32-bit) floating-point element in `a` and `b` for greater-than-or-equal, 716 /// and return the boolean result (0 or 1). 717 int _mm_comige_ss (__m128 a, __m128 b) pure @safe 718 { 719 return a.array[0] >= b.array[0]; 720 } 721 unittest 722 { 723 assert(1 == _mm_comige_ss(_mm_set_ss(78.0f), _mm_set_ss(78.0f))); 724 assert(1 == _mm_comige_ss(_mm_set_ss(78.0f), _mm_set_ss(-78.0f))); 725 assert(0 == _mm_comige_ss(_mm_set_ss(-78.0f), _mm_set_ss(78.0f))); 726 assert(0 == _mm_comige_ss(_mm_set_ss(78.0f), _mm_set_ss(float.nan))); 727 assert(0 == _mm_comige_ss(_mm_set_ss(float.nan), _mm_set_ss(-4.22f))); 728 assert(1 == _mm_comige_ss(_mm_set_ss(-0.0f), _mm_set_ss(0.0f))); 729 } 730 731 /// Compare the lower single-precision (32-bit) floating-point element in `a` and `b` for greater-than, 732 /// and return the boolean result (0 or 1). 733 int _mm_comigt_ss (__m128 a, __m128 b) pure @safe // comiss + seta 734 { 735 return a.array[0] > b.array[0]; 736 } 737 unittest 738 { 739 assert(0 == _mm_comigt_ss(_mm_set_ss(78.0f), _mm_set_ss(78.0f))); 740 assert(1 == _mm_comigt_ss(_mm_set_ss(78.0f), _mm_set_ss(-78.0f))); 741 assert(0 == _mm_comigt_ss(_mm_set_ss(78.0f), _mm_set_ss(float.nan))); 742 assert(0 == _mm_comigt_ss(_mm_set_ss(float.nan), _mm_set_ss(-4.22f))); 743 assert(0 == _mm_comigt_ss(_mm_set_ss(0.0f), _mm_set_ss(-0.0f))); 744 } 745 746 /// Compare the lower single-precision (32-bit) floating-point element in `a` and `b` for less-than-or-equal, 747 /// and return the boolean result (0 or 1). 748 int _mm_comile_ss (__m128 a, __m128 b) pure @safe // comiss + setbe 749 { 750 return a.array[0] <= b.array[0]; 751 } 752 unittest 753 { 754 assert(1 == _mm_comile_ss(_mm_set_ss(78.0f), _mm_set_ss(78.0f))); 755 assert(0 == _mm_comile_ss(_mm_set_ss(78.0f), _mm_set_ss(-78.0f))); 756 assert(1 == _mm_comile_ss(_mm_set_ss(-78.0f), _mm_set_ss(78.0f))); 757 assert(0 == _mm_comile_ss(_mm_set_ss(78.0f), _mm_set_ss(float.nan))); 758 assert(0 == _mm_comile_ss(_mm_set_ss(float.nan), _mm_set_ss(-4.22f))); 759 assert(1 == _mm_comile_ss(_mm_set_ss(0.0f), _mm_set_ss(-0.0f))); 760 } 761 762 /// Compare the lower single-precision (32-bit) floating-point element in `a` and `b` for less-than, 763 /// and return the boolean result (0 or 1). 764 int _mm_comilt_ss (__m128 a, __m128 b) pure @safe // comiss + setb 765 { 766 return a.array[0] < b.array[0]; 767 } 768 unittest 769 { 770 assert(0 == _mm_comilt_ss(_mm_set_ss(78.0f), _mm_set_ss(78.0f))); 771 assert(0 == _mm_comilt_ss(_mm_set_ss(78.0f), _mm_set_ss(-78.0f))); 772 assert(1 == _mm_comilt_ss(_mm_set_ss(-78.0f), _mm_set_ss(78.0f))); 773 assert(0 == _mm_comilt_ss(_mm_set_ss(78.0f), _mm_set_ss(float.nan))); 774 assert(0 == _mm_comilt_ss(_mm_set_ss(float.nan), _mm_set_ss(-4.22f))); 775 assert(0 == _mm_comilt_ss(_mm_set_ss(-0.0f), _mm_set_ss(0.0f))); 776 } 777 778 /// Compare the lower single-precision (32-bit) floating-point element in `a` and `b` for not-equal, 779 /// and return the boolean result (0 or 1). 780 int _mm_comineq_ss (__m128 a, __m128 b) pure @safe // comiss + setne 781 { 782 return a.array[0] != b.array[0]; 783 } 784 unittest 785 { 786 assert(0 == _mm_comineq_ss(_mm_set_ss(78.0f), _mm_set_ss(78.0f))); 787 assert(1 == _mm_comineq_ss(_mm_set_ss(78.0f), _mm_set_ss(-78.0f))); 788 assert(1 == _mm_comineq_ss(_mm_set_ss(78.0f), _mm_set_ss(float.nan))); 789 assert(1 == _mm_comineq_ss(_mm_set_ss(float.nan), _mm_set_ss(-4.22f))); 790 assert(0 == _mm_comineq_ss(_mm_set_ss(0.0f), _mm_set_ss(-0.0f))); 791 } 792 793 /// Convert packed signed 32-bit integers in `b` to packed single-precision (32-bit) 794 /// floating-point elements, store the results in the lower 2 elements, 795 /// and copy the upper 2 packed elements from `a` to the upper elements of result. 796 alias _mm_cvt_pi2ps = _mm_cvtpi32_ps; 797 798 /// Convert 2 lower packed single-precision (32-bit) floating-point elements in `a` 799 /// to packed 32-bit integers. 800 __m64 _mm_cvt_ps2pi (__m128 a) @safe 801 { 802 return to_m64(_mm_cvtps_epi32(a)); 803 } 804 805 /// Convert the signed 32-bit integer `b` to a single-precision (32-bit) floating-point element, 806 /// store the result in the lower element, and copy the upper 3 packed elements from `a` to the 807 /// upper elements of the result. 808 __m128 _mm_cvt_si2ss (__m128 v, int x) pure @trusted 809 { 810 v.ptr[0] = cast(float)x; 811 return v; 812 } 813 unittest 814 { 815 __m128 a = _mm_cvt_si2ss(_mm_set1_ps(0.0f), 42); 816 assert(a.array == [42f, 0, 0, 0]); 817 } 818 819 /// Convert packed 16-bit integers in `a` to packed single-precision (32-bit) floating-point elements. 820 __m128 _mm_cvtpi16_ps (__m64 a) pure @safe 821 { 822 __m128i ma = to_m128i(a); 823 ma = _mm_unpacklo_epi16(ma, _mm_setzero_si128()); // Zero-extend to 32-bit 824 ma = _mm_srai_epi32(_mm_slli_epi32(ma, 16), 16); // Replicate sign bit 825 return _mm_cvtepi32_ps(ma); 826 } 827 unittest 828 { 829 __m64 A = _mm_setr_pi16(-1, 2, -3, 4); 830 __m128 R = _mm_cvtpi16_ps(A); 831 float[4] correct = [-1.0f, 2.0f, -3.0f, 4.0f]; 832 assert(R.array == correct); 833 } 834 835 /// Convert packed signed 32-bit integers in `b` to packed single-precision (32-bit) 836 /// floating-point elements, store the results in the lower 2 elements, 837 /// and copy the upper 2 packed elements from `a` to the upper elements of result. 838 __m128 _mm_cvtpi32_ps (__m128 a, __m64 b) pure @trusted 839 { 840 __m128 fb = _mm_cvtepi32_ps(to_m128i(b)); 841 a.ptr[0] = fb.array[0]; 842 a.ptr[1] = fb.array[1]; 843 return a; 844 } 845 unittest 846 { 847 __m128 R = _mm_cvtpi32_ps(_mm_set1_ps(4.0f), _mm_setr_pi32(1, 2)); 848 float[4] correct = [1.0f, 2.0f, 4.0f, 4.0f]; 849 assert(R.array == correct); 850 } 851 852 /// Convert packed signed 32-bit integers in `a` to packed single-precision (32-bit) floating-point elements, 853 /// store the results in the lower 2 elements, then covert the packed signed 32-bit integers in `b` to 854 /// single-precision (32-bit) floating-point element, and store the results in the upper 2 elements. 855 __m128 _mm_cvtpi32x2_ps (__m64 a, __m64 b) pure @trusted 856 { 857 long2 l; 858 l.ptr[0] = a.array[0]; 859 l.ptr[1] = b.array[0]; 860 return _mm_cvtepi32_ps(cast(__m128i)l); 861 } 862 unittest 863 { 864 __m64 A = _mm_setr_pi32(-45, 128); 865 __m64 B = _mm_setr_pi32(0, 1000); 866 __m128 R = _mm_cvtpi32x2_ps(A, B); 867 float[4] correct = [-45.0f, 128.0f, 0.0f, 1000.0f]; 868 assert(R.array == correct); 869 } 870 871 /// Convert the lower packed 8-bit integers in `a` to packed single-precision (32-bit) floating-point elements. 872 __m128 _mm_cvtpi8_ps (__m64 a) pure @safe 873 { 874 __m128i b = to_m128i(a); 875 876 // Zero extend to 32-bit 877 b = _mm_unpacklo_epi8(b, _mm_setzero_si128()); 878 b = _mm_unpacklo_epi16(b, _mm_setzero_si128()); 879 880 // Replicate sign bit 881 b = _mm_srai_epi32(_mm_slli_epi32(b, 24), 24); // Replicate sign bit 882 return _mm_cvtepi32_ps(b); 883 } 884 unittest 885 { 886 __m64 A = _mm_setr_pi8(-1, 2, -3, 4, 0, 0, 0, 0); 887 __m128 R = _mm_cvtpi8_ps(A); 888 float[4] correct = [-1.0f, 2.0f, -3.0f, 4.0f]; 889 assert(R.array == correct); 890 } 891 892 /// Convert packed single-precision (32-bit) floating-point elements in `a` to packed 16-bit integers. 893 /// Note: this intrinsic will generate 0x7FFF, rather than 0x8000, for input values between 0x7FFF and 0x7FFFFFFF. 894 __m64 _mm_cvtps_pi16 (__m128 a) @safe 895 { 896 // The C++ version of this intrinsic convert to 32-bit float, then use packssdw 897 // Which means the 16-bit integers should be saturated 898 __m128i b = _mm_cvtps_epi32(a); 899 b = _mm_packs_epi32(b, b); 900 return to_m64(b); 901 } 902 unittest 903 { 904 __m128 A = _mm_setr_ps(-1.0f, 2.0f, -33000.0f, 70000.0f); 905 short4 R = cast(short4) _mm_cvtps_pi16(A); 906 short[4] correct = [-1, 2, -32768, 32767]; 907 assert(R.array == correct); 908 } 909 910 /// Convert packed single-precision (32-bit) floating-point elements in `a` to packed 32-bit integers. 911 __m64 _mm_cvtps_pi32 (__m128 a) @safe 912 { 913 return to_m64(_mm_cvtps_epi32(a)); 914 } 915 unittest 916 { 917 __m128 A = _mm_setr_ps(-33000.0f, 70000.0f, -1.0f, 2.0f, ); 918 int2 R = cast(int2) _mm_cvtps_pi32(A); 919 int[2] correct = [-33000, 70000]; 920 assert(R.array == correct); 921 } 922 923 /// Convert packed single-precision (32-bit) floating-point elements in `a` to packed 8-bit integers, 924 /// and store the results in lower 4 elements. 925 /// Note: this intrinsic will generate 0x7F, rather than 0x80, for input values between 0x7F and 0x7FFFFFFF. 926 __m64 _mm_cvtps_pi8 (__m128 a) @safe 927 { 928 // The C++ version of this intrinsic convert to 32-bit float, then use packssdw + packsswb 929 // Which means the 8-bit integers should be saturated 930 __m128i b = _mm_cvtps_epi32(a); 931 b = _mm_packs_epi32(b, _mm_setzero_si128()); 932 b = _mm_packs_epi16(b, _mm_setzero_si128()); 933 return to_m64(b); 934 } 935 unittest 936 { 937 __m128 A = _mm_setr_ps(-1.0f, 2.0f, -129.0f, 128.0f); 938 byte8 R = cast(byte8) _mm_cvtps_pi8(A); 939 byte[8] correct = [-1, 2, -128, 127, 0, 0, 0, 0]; 940 assert(R.array == correct); 941 } 942 943 /// Convert packed unsigned 16-bit integers in `a` to packed single-precision (32-bit) floating-point elements. 944 __m128 _mm_cvtpu16_ps (__m64 a) pure @safe 945 { 946 __m128i ma = to_m128i(a); 947 ma = _mm_unpacklo_epi16(ma, _mm_setzero_si128()); // Zero-extend to 32-bit 948 return _mm_cvtepi32_ps(ma); 949 } 950 unittest 951 { 952 __m64 A = _mm_setr_pi16(-1, 2, -3, 4); 953 __m128 R = _mm_cvtpu16_ps(A); 954 float[4] correct = [65535.0f, 2.0f, 65533.0f, 4.0f]; 955 assert(R.array == correct); 956 } 957 958 /// Convert the lower packed unsigned 8-bit integers in `a` to packed single-precision (32-bit) floating-point element. 959 __m128 _mm_cvtpu8_ps (__m64 a) pure @safe 960 { 961 __m128i b = to_m128i(a); 962 963 // Zero extend to 32-bit 964 b = _mm_unpacklo_epi8(b, _mm_setzero_si128()); 965 b = _mm_unpacklo_epi16(b, _mm_setzero_si128()); 966 return _mm_cvtepi32_ps(b); 967 } 968 unittest 969 { 970 __m64 A = _mm_setr_pi8(-1, 2, -3, 4, 0, 0, 0, 0); 971 __m128 R = _mm_cvtpu8_ps(A); 972 float[4] correct = [255.0f, 2.0f, 253.0f, 4.0f]; 973 assert(R.array == correct); 974 } 975 976 /// Convert the signed 32-bit integer `b` to a single-precision (32-bit) floating-point element, 977 /// store the result in the lower element, and copy the upper 3 packed elements from `a` to the 978 /// upper elements of result. 979 __m128 _mm_cvtsi32_ss(__m128 v, int x) pure @trusted 980 { 981 v.ptr[0] = cast(float)x; 982 return v; 983 } 984 unittest 985 { 986 __m128 a = _mm_cvtsi32_ss(_mm_set1_ps(0.0f), 42); 987 assert(a.array == [42.0f, 0, 0, 0]); 988 } 989 990 991 /// Convert the signed 64-bit integer `b` to a single-precision (32-bit) floating-point element, 992 /// store the result in the lower element, and copy the upper 3 packed elements from `a` to the 993 /// upper elements of result. 994 __m128 _mm_cvtsi64_ss(__m128 v, long x) pure @trusted 995 { 996 v.ptr[0] = cast(float)x; 997 return v; 998 } 999 unittest 1000 { 1001 __m128 a = _mm_cvtsi64_ss(_mm_set1_ps(0.0f), 42); 1002 assert(a.array == [42.0f, 0, 0, 0]); 1003 } 1004 1005 /// Take the lower single-precision (32-bit) floating-point element of `a`. 1006 float _mm_cvtss_f32(__m128 a) pure @safe 1007 { 1008 return a.array[0]; 1009 } 1010 1011 /// Convert the lower single-precision (32-bit) floating-point element in `a` to a 32-bit integer. 1012 int _mm_cvtss_si32 (__m128 a) @safe // PERF GDC 1013 { 1014 static if (GDC_with_SSE) 1015 { 1016 return __builtin_ia32_cvtss2si(a); 1017 } 1018 else static if (LDC_with_SSE1) 1019 { 1020 return __builtin_ia32_cvtss2si(a); 1021 } 1022 else static if (DMD_with_DSIMD) 1023 { 1024 __m128 b; 1025 __m128i r = cast(__m128i) __simd(XMM.CVTPS2DQ, a); // Note: converts 4 integers. 1026 return r.array[0]; 1027 } 1028 else 1029 { 1030 return convertFloatToInt32UsingMXCSR(a.array[0]); 1031 } 1032 } 1033 unittest 1034 { 1035 assert(1 == _mm_cvtss_si32(_mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f))); 1036 } 1037 1038 /// Convert the lower single-precision (32-bit) floating-point element in `a` to a 64-bit integer. 1039 long _mm_cvtss_si64 (__m128 a) @safe 1040 { 1041 version(LDC) 1042 { 1043 version(X86_64) 1044 { 1045 return __builtin_ia32_cvtss2si64(a); 1046 } 1047 else 1048 { 1049 // Note: In 32-bit x86, there is no way to convert from float/double to 64-bit integer 1050 // using SSE instructions only. So the builtin doesn't exit for this arch. 1051 return convertFloatToInt64UsingMXCSR(a.array[0]); 1052 } 1053 } 1054 else 1055 { 1056 return convertFloatToInt64UsingMXCSR(a.array[0]); 1057 } 1058 } 1059 unittest 1060 { 1061 assert(1 == _mm_cvtss_si64(_mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f))); 1062 1063 uint savedRounding = _MM_GET_ROUNDING_MODE(); 1064 1065 _MM_SET_ROUNDING_MODE(_MM_ROUND_NEAREST); 1066 assert(-86186 == _mm_cvtss_si64(_mm_set1_ps(-86186.49f))); 1067 1068 _MM_SET_ROUNDING_MODE(_MM_ROUND_DOWN); 1069 assert(-86187 == _mm_cvtss_si64(_mm_set1_ps(-86186.1f))); 1070 1071 _MM_SET_ROUNDING_MODE(_MM_ROUND_UP); 1072 assert(86187 == _mm_cvtss_si64(_mm_set1_ps(86186.1f))); 1073 1074 _MM_SET_ROUNDING_MODE(_MM_ROUND_TOWARD_ZERO); 1075 assert(-86186 == _mm_cvtss_si64(_mm_set1_ps(-86186.9f))); 1076 1077 _MM_SET_ROUNDING_MODE(savedRounding); 1078 } 1079 1080 1081 /// Convert the lower single-precision (32-bit) floating-point element in `a` to a 32-bit 1082 /// integer with truncation. 1083 int _mm_cvtt_ss2si (__m128 a) pure @safe 1084 { 1085 // x86: cvttss2si always generated, even in -O0 1086 return cast(int)(a.array[0]); 1087 } 1088 alias _mm_cvttss_si32 = _mm_cvtt_ss2si; ///ditto 1089 unittest 1090 { 1091 assert(1 == _mm_cvtt_ss2si(_mm_setr_ps(1.9f, 2.0f, 3.0f, 4.0f))); 1092 } 1093 1094 1095 /// Convert packed single-precision (32-bit) floating-point elements in `a` to packed 32-bit 1096 /// integers with truncation. 1097 __m64 _mm_cvtt_ps2pi (__m128 a) pure @safe 1098 { 1099 return to_m64(_mm_cvttps_epi32(a)); 1100 } 1101 1102 /// Convert the lower single-precision (32-bit) floating-point element in `a` to a 64-bit 1103 /// integer with truncation. 1104 long _mm_cvttss_si64 (__m128 a) pure @safe 1105 { 1106 return cast(long)(a.array[0]); 1107 } 1108 unittest 1109 { 1110 assert(1 == _mm_cvttss_si64(_mm_setr_ps(1.9f, 2.0f, 3.0f, 4.0f))); 1111 } 1112 1113 /// Divide packed single-precision (32-bit) floating-point elements in `a` by packed elements in `b`. 1114 __m128 _mm_div_ps(__m128 a, __m128 b) pure @safe 1115 { 1116 pragma(inline, true); 1117 return a / b; 1118 } 1119 unittest 1120 { 1121 __m128 a = [1.5f, -2.0f, 3.0f, 1.0f]; 1122 a = _mm_div_ps(a, a); 1123 float[4] correct = [1.0f, 1.0f, 1.0f, 1.0f]; 1124 assert(a.array == correct); 1125 } 1126 1127 /// Divide the lower single-precision (32-bit) floating-point element in `a` by the lower 1128 /// single-precision (32-bit) floating-point element in `b`, store the result in the lower 1129 /// element of result, and copy the upper 3 packed elements from `a` to the upper elements of result. 1130 __m128 _mm_div_ss(__m128 a, __m128 b) pure @safe 1131 { 1132 static if (DMD_with_DSIMD) 1133 return cast(__m128) __simd(XMM.DIVSS, a, b); 1134 else static if (GDC_with_SSE) 1135 return __builtin_ia32_divss(a, b); 1136 else 1137 { 1138 a[0] /= b[0]; 1139 return a; 1140 } 1141 } 1142 unittest 1143 { 1144 __m128 a = [1.5f, -2.0f, 3.0f, 1.0f]; 1145 a = _mm_div_ss(a, a); 1146 float[4] correct = [1.0f, -2.0, 3.0f, 1.0f]; 1147 assert(a.array == correct); 1148 } 1149 1150 /// Extract a 16-bit unsigned integer from `a`, selected with `imm8`. Zero-extended. 1151 int _mm_extract_pi16 (__m64 a, int imm8) 1152 { 1153 short4 sa = cast(short4)a; 1154 return cast(ushort)(sa.array[imm8]); 1155 } 1156 unittest 1157 { 1158 __m64 A = _mm_setr_pi16(-1, 6, 0, 4); 1159 assert(_mm_extract_pi16(A, 0) == 65535); 1160 assert(_mm_extract_pi16(A, 1) == 6); 1161 assert(_mm_extract_pi16(A, 2) == 0); 1162 assert(_mm_extract_pi16(A, 3) == 4); 1163 } 1164 1165 /// Free aligned memory that was allocated with `_mm_malloc`. 1166 void _mm_free(void * mem_addr) @trusted 1167 { 1168 // support for free(NULL) 1169 if (mem_addr is null) 1170 return; 1171 1172 // Technically we don't need to store size and alignement in the chunk, but we do in case we 1173 // have to implement _mm_realloc 1174 1175 size_t pointerSize = (void*).sizeof; 1176 void** rawLocation = cast(void**)(cast(char*)mem_addr - size_t.sizeof); 1177 size_t* alignmentLocation = cast(size_t*)(cast(char*)mem_addr - 3 * pointerSize); 1178 size_t alignment = *alignmentLocation; 1179 assert(alignment != 0); 1180 assert(isPointerAligned(mem_addr, alignment)); 1181 free(*rawLocation); 1182 } 1183 1184 /// Get the exception mask bits from the MXCSR control and status register. 1185 /// The exception mask may contain any of the following flags: `_MM_MASK_INVALID`, 1186 /// `_MM_MASK_DIV_ZERO`, `_MM_MASK_DENORM`, `_MM_MASK_OVERFLOW`, `_MM_MASK_UNDERFLOW`, `_MM_MASK_INEXACT`. 1187 /// Note: won't correspond to reality on non-x86, where MXCSR this is emulated. 1188 uint _MM_GET_EXCEPTION_MASK() @safe 1189 { 1190 return _mm_getcsr() & _MM_MASK_MASK; 1191 } 1192 1193 /// Get the exception state bits from the MXCSR control and status register. 1194 /// The exception state may contain any of the following flags: `_MM_EXCEPT_INVALID`, 1195 /// `_MM_EXCEPT_DIV_ZERO`, `_MM_EXCEPT_DENORM`, `_MM_EXCEPT_OVERFLOW`, `_MM_EXCEPT_UNDERFLOW`, `_MM_EXCEPT_INEXACT`. 1196 /// Note: won't correspond to reality on non-x86, where MXCSR this is emulated. No exception reported. 1197 uint _MM_GET_EXCEPTION_STATE() @safe 1198 { 1199 return _mm_getcsr() & _MM_EXCEPT_MASK; 1200 } 1201 1202 /// Get the flush zero bits from the MXCSR control and status register. 1203 /// The flush zero may contain any of the following flags: `_MM_FLUSH_ZERO_ON` or `_MM_FLUSH_ZERO_OFF` 1204 uint _MM_GET_FLUSH_ZERO_MODE() @safe 1205 { 1206 return _mm_getcsr() & _MM_FLUSH_ZERO_MASK; 1207 } 1208 1209 /// Get the rounding mode bits from the MXCSR control and status register. The rounding mode may 1210 /// contain any of the following flags: `_MM_ROUND_NEAREST, `_MM_ROUND_DOWN`, `_MM_ROUND_UP`, `_MM_ROUND_TOWARD_ZERO`. 1211 uint _MM_GET_ROUNDING_MODE() @safe 1212 { 1213 return _mm_getcsr() & _MM_ROUND_MASK; 1214 } 1215 1216 /// Get the unsigned 32-bit value of the MXCSR control and status register. 1217 /// Note: this is emulated on ARM, because there is no MXCSR register then. 1218 uint _mm_getcsr() @trusted 1219 { 1220 static if (LDC_with_ARM) 1221 { 1222 // Note: we convert the ARM FPSCR into a x86 SSE control word. 1223 // However, only rounding mode and flush to zero are actually set. 1224 // The returned control word will have all exceptions masked, and no exception detected. 1225 1226 uint fpscr = arm_get_fpcr(); 1227 1228 uint cw = 0; // No exception detected 1229 if (fpscr & _MM_FLUSH_ZERO_MASK_ARM) 1230 { 1231 // ARM has one single flag for ARM. 1232 // It does both x86 bits. 1233 // https://developer.arm.com/documentation/dui0473/c/neon-and-vfp-programming/the-effects-of-using-flush-to-zero-mode 1234 cw |= _MM_FLUSH_ZERO_ON; 1235 cw |= 0x40; // set "denormals are zeros" 1236 } 1237 cw |= _MM_MASK_MASK; // All exception maske 1238 1239 // Rounding mode 1240 switch(fpscr & _MM_ROUND_MASK_ARM) 1241 { 1242 default: 1243 case _MM_ROUND_NEAREST_ARM: cw |= _MM_ROUND_NEAREST; break; 1244 case _MM_ROUND_DOWN_ARM: cw |= _MM_ROUND_DOWN; break; 1245 case _MM_ROUND_UP_ARM: cw |= _MM_ROUND_UP; break; 1246 case _MM_ROUND_TOWARD_ZERO_ARM: cw |= _MM_ROUND_TOWARD_ZERO; break; 1247 } 1248 return cw; 1249 } 1250 else version(GNU) 1251 { 1252 static if (GDC_with_SSE) 1253 { 1254 return __builtin_ia32_stmxcsr(); 1255 } 1256 else version(X86) 1257 { 1258 uint sseRounding = 0; 1259 asm pure nothrow @nogc @trusted 1260 { 1261 "stmxcsr %0;\n" 1262 : "=m" (sseRounding) 1263 : 1264 : ; 1265 } 1266 return sseRounding; 1267 } 1268 else 1269 static assert(false); 1270 } 1271 else version (InlineX86Asm) 1272 { 1273 uint controlWord; 1274 asm nothrow @nogc pure @safe 1275 { 1276 stmxcsr controlWord; 1277 } 1278 return controlWord; 1279 } 1280 else 1281 static assert(0, "Not yet supported"); 1282 } 1283 unittest 1284 { 1285 uint csr = _mm_getcsr(); 1286 } 1287 1288 /// Insert a 16-bit integer `i` inside `a` at the location specified by `imm8`. 1289 __m64 _mm_insert_pi16 (__m64 v, int i, int imm8) pure @trusted 1290 { 1291 short4 r = cast(short4)v; 1292 r.ptr[imm8 & 3] = cast(short)i; 1293 return cast(__m64)r; 1294 } 1295 unittest 1296 { 1297 __m64 A = _mm_set_pi16(3, 2, 1, 0); 1298 short4 R = cast(short4) _mm_insert_pi16(A, 42, 1 | 4); 1299 short[4] correct = [0, 42, 2, 3]; 1300 assert(R.array == correct); 1301 } 1302 1303 /// Load 128-bits (composed of 4 packed single-precision (32-bit) floating-point elements) from memory. 1304 // `p` must be aligned on a 16-byte boundary or a general-protection exception may be generated. 1305 __m128 _mm_load_ps(const(float)*p) pure @trusted // TODO shouldn't be trusted 1306 { 1307 pragma(inline, true); 1308 return *cast(__m128*)p; 1309 } 1310 unittest 1311 { 1312 static immutable align(16) float[4] correct = [1.0f, 2.0f, 3.0f, 4.0f]; 1313 __m128 A = _mm_load_ps(correct.ptr); 1314 assert(A.array == correct); 1315 } 1316 1317 /// Load a single-precision (32-bit) floating-point element from memory into all elements. 1318 __m128 _mm_load_ps1(const(float)*p) pure @trusted 1319 { 1320 return __m128(*p); 1321 } 1322 unittest 1323 { 1324 float n = 2.5f; 1325 float[4] correct = [2.5f, 2.5f, 2.5f, 2.5f]; 1326 __m128 A = _mm_load_ps1(&n); 1327 assert(A.array == correct); 1328 } 1329 1330 /// Load a single-precision (32-bit) floating-point element from memory into the lower of dst, and zero the upper 3 1331 /// elements. `mem_addr` does not need to be aligned on any particular boundary. 1332 __m128 _mm_load_ss (const(float)* mem_addr) pure @trusted 1333 { 1334 pragma(inline, true); 1335 static if (DMD_with_DSIMD) 1336 { 1337 return cast(__m128)__simd(XMM.LODSS, *cast(__m128*)mem_addr); 1338 } 1339 else 1340 { 1341 __m128 r; 1342 r.ptr[0] = *mem_addr; 1343 r.ptr[1] = 0; 1344 r.ptr[2] = 0; 1345 r.ptr[3] = 0; 1346 return r; 1347 } 1348 } 1349 unittest 1350 { 1351 float n = 2.5f; 1352 float[4] correct = [2.5f, 0.0f, 0.0f, 0.0f]; 1353 __m128 A = _mm_load_ss(&n); 1354 assert(A.array == correct); 1355 } 1356 1357 /// Load a single-precision (32-bit) floating-point element from memory into all elements. 1358 alias _mm_load1_ps = _mm_load_ps1; 1359 1360 /// Load 2 single-precision (32-bit) floating-point elements from memory into the upper 2 elements of result, 1361 /// and copy the lower 2 elements from `a` to result. `mem_addr does` not need to be aligned on any particular boundary. 1362 __m128 _mm_loadh_pi (__m128 a, const(__m64)* mem_addr) pure @trusted 1363 { 1364 pragma(inline, true); 1365 static if (DMD_with_DSIMD) 1366 { 1367 return cast(__m128) __simd(XMM.LODHPS, a, *cast(const(__m128)*)mem_addr); 1368 } 1369 else 1370 { 1371 // x86: movlhps generated since LDC 1.9.0 -O1 1372 long2 la = cast(long2)a; 1373 la.ptr[1] = (*mem_addr).array[0]; 1374 return cast(__m128)la; 1375 } 1376 } 1377 unittest 1378 { 1379 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f); 1380 __m128 B = _mm_setr_ps(5.0f, 6.0f, 7.0f, 8.0f); 1381 __m64 M = to_m64(cast(__m128i)B); 1382 __m128 R = _mm_loadh_pi(A, &M); 1383 float[4] correct = [1.0f, 2.0f, 5.0f, 6.0f]; 1384 assert(R.array == correct); 1385 } 1386 1387 /// Load 2 single-precision (32-bit) floating-point elements from memory into the lower 2 elements of result, 1388 /// and copy the upper 2 elements from `a` to result. `mem_addr` does not need to be aligned on any particular boundary. 1389 __m128 _mm_loadl_pi (__m128 a, const(__m64)* mem_addr) pure @trusted 1390 { 1391 pragma(inline, true); 1392 static if (DMD_with_DSIMD) 1393 { 1394 return cast(__m128) __simd(XMM.LODLPS, a, *cast(const(__m128)*)mem_addr); 1395 } 1396 else 1397 { 1398 // x86: movlpd/movlps generated with all LDC -01 1399 long2 la = cast(long2)a; 1400 la.ptr[0] = (*mem_addr).array[0]; 1401 return cast(__m128)la; 1402 } 1403 } 1404 unittest 1405 { 1406 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f); 1407 __m128 B = _mm_setr_ps(5.0f, 6.0f, 7.0f, 8.0f); 1408 __m64 M = to_m64(cast(__m128i)B); 1409 __m128 R = _mm_loadl_pi(A, &M); 1410 float[4] correct = [5.0f, 6.0f, 3.0f, 4.0f]; 1411 assert(R.array == correct); 1412 } 1413 1414 /// Load 4 single-precision (32-bit) floating-point elements from memory in reverse order. 1415 /// `mem_addr` must be aligned on a 16-byte boundary or a general-protection exception may be generated. 1416 __m128 _mm_loadr_ps (const(float)* mem_addr) pure @trusted // TODO shouldn't be trusted 1417 { 1418 __m128* aligned = cast(__m128*)mem_addr; // x86: movaps + shups since LDC 1.0.0 -O1 1419 __m128 a = *aligned; 1420 static if (DMD_with_DSIMD) 1421 { 1422 return cast(__m128) __simd(XMM.SHUFPS, a, a, 27); 1423 } 1424 else 1425 { 1426 __m128 r; 1427 r.ptr[0] = a.array[3]; 1428 r.ptr[1] = a.array[2]; 1429 r.ptr[2] = a.array[1]; 1430 r.ptr[3] = a.array[0]; 1431 return r; 1432 } 1433 } 1434 unittest 1435 { 1436 align(16) static immutable float[4] arr = [ 1.0f, 2.0f, 3.0f, 8.0f ]; 1437 __m128 A = _mm_loadr_ps(arr.ptr); 1438 float[4] correct = [ 8.0f, 3.0f, 2.0f, 1.0f ]; 1439 assert(A.array == correct); 1440 } 1441 1442 /// Load 128-bits (composed of 4 packed single-precision (32-bit) floating-point elements) from memory. 1443 /// `mem_addr` does not need to be aligned on any particular boundary. 1444 __m128 _mm_loadu_ps(const(float)* mem_addr) pure @trusted 1445 { 1446 pragma(inline, true); 1447 static if (GDC_with_SSE2) 1448 { 1449 return __builtin_ia32_loadups(mem_addr); 1450 } 1451 else version(LDC) 1452 { 1453 return loadUnaligned!(__m128)(mem_addr); 1454 } 1455 else version(DigitalMars) 1456 { 1457 static if (DMD_with_DSIMD) 1458 { 1459 return cast(__m128)__simd(XMM.LODUPS, *mem_addr); 1460 } 1461 else static if (SSESizedVectorsAreEmulated) 1462 { 1463 // Since this vector is emulated, it doesn't have alignement constraints 1464 // and as such we can just cast it. 1465 return *cast(__m128*)(mem_addr); 1466 } 1467 else 1468 { 1469 __m128 result; 1470 result.ptr[0] = mem_addr[0]; 1471 result.ptr[1] = mem_addr[1]; 1472 result.ptr[2] = mem_addr[2]; 1473 result.ptr[3] = mem_addr[3]; 1474 return result; 1475 } 1476 } 1477 else 1478 { 1479 __m128 result; 1480 result.ptr[0] = mem_addr[0]; 1481 result.ptr[1] = mem_addr[1]; 1482 result.ptr[2] = mem_addr[2]; 1483 result.ptr[3] = mem_addr[3]; 1484 return result; 1485 } 1486 } 1487 unittest 1488 { 1489 align(16) static immutable float[5] arr = [ 1.0f, 2.0f, 3.0f, 8.0f, 9.0f ]; // force unaligned load 1490 __m128 A = _mm_loadu_ps(&arr[1]); 1491 float[4] correct = [ 2.0f, 3.0f, 8.0f, 9.0f ]; 1492 assert(A.array == correct); 1493 } 1494 1495 /// Load unaligned 16-bit integer from memory into the first element, fill with zeroes otherwise. 1496 __m128i _mm_loadu_si16(const(void)* mem_addr) pure @trusted 1497 { 1498 static if (DMD_with_DSIMD) 1499 { 1500 int r = *cast(short*)(mem_addr); 1501 return cast(__m128i) __simd(XMM.LODD, *cast(__m128i*)&r); 1502 } 1503 else version(DigitalMars) 1504 { 1505 // Workaround issue: https://issues.dlang.org/show_bug.cgi?id=21672 1506 // DMD cannot handle the below code... 1507 align(16) short[8] r = [0, 0, 0, 0, 0, 0, 0, 0]; 1508 r[0] = *cast(short*)(mem_addr); 1509 return *cast(int4*)(r.ptr); 1510 } 1511 else 1512 { 1513 short r = *cast(short*)(mem_addr); 1514 short8 result = [0, 0, 0, 0, 0, 0, 0, 0]; 1515 result.ptr[0] = r; 1516 return cast(__m128i)result; 1517 } 1518 } 1519 unittest 1520 { 1521 short r = 13; 1522 short8 A = cast(short8) _mm_loadu_si16(&r); 1523 short[8] correct = [13, 0, 0, 0, 0, 0, 0, 0]; 1524 assert(A.array == correct); 1525 } 1526 1527 /// Load unaligned 64-bit integer from memory into the first element of result. 1528 /// Upper 64-bit is zeroed. 1529 __m128i _mm_loadu_si64(const(void)* mem_addr) pure @trusted 1530 { 1531 pragma(inline, true); 1532 static if (DMD_with_DSIMD) 1533 { 1534 return cast(__m128i) __simd(XMM.LODQ, *cast(__m128i*)mem_addr); 1535 } 1536 else 1537 { 1538 long r = *cast(long*)(mem_addr); 1539 long2 result = [0, 0]; 1540 result.ptr[0] = r; 1541 return cast(__m128i)result; 1542 } 1543 } 1544 unittest 1545 { 1546 long r = 446446446446; 1547 long2 A = cast(long2) _mm_loadu_si64(&r); 1548 long[2] correct = [446446446446, 0]; 1549 assert(A.array == correct); 1550 } 1551 1552 /// Allocate size bytes of memory, aligned to the alignment specified in align, 1553 /// and return a pointer to the allocated memory. `_mm_free` should be used to free 1554 /// memory that is allocated with `_mm_malloc`. 1555 void* _mm_malloc(size_t size, size_t alignment) @trusted 1556 { 1557 assert(alignment != 0); 1558 size_t request = requestedSize(size, alignment); 1559 void* raw = malloc(request); 1560 if (request > 0 && raw == null) // malloc(0) can validly return anything 1561 onOutOfMemoryError(); 1562 return storeRawPointerPlusInfo(raw, size, alignment); // PERF: no need to store size 1563 } 1564 1565 /// Conditionally store 8-bit integer elements from a into memory using mask (elements are not stored when the highest 1566 /// bit is not set in the corresponding element) and a non-temporal memory hint. 1567 void _mm_maskmove_si64 (__m64 a, __m64 mask, char* mem_addr) @trusted 1568 { 1569 // this works since mask is zero-extended 1570 return _mm_maskmoveu_si128 (to_m128i(a), to_m128i(mask), mem_addr); 1571 } 1572 1573 deprecated("Use _mm_maskmove_si64 instead") alias _m_maskmovq = _mm_maskmove_si64;/// 1574 1575 /// Compare packed signed 16-bit integers in `a` and `b`, and return packed maximum value. 1576 __m64 _mm_max_pi16 (__m64 a, __m64 b) pure @safe 1577 { 1578 return to_m64(_mm_max_epi16(to_m128i(a), to_m128i(b))); 1579 } 1580 1581 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b`, and return packed maximum values. 1582 __m128 _mm_max_ps(__m128 a, __m128 b) pure @safe 1583 { 1584 static if (DMD_with_DSIMD) 1585 { 1586 return cast(__m128) __simd(XMM.MAXPS, a, b); 1587 } 1588 else static if (GDC_with_SSE) 1589 { 1590 return __builtin_ia32_maxps(a, b); 1591 } 1592 else static if (LDC_with_SSE1) 1593 { 1594 return __builtin_ia32_maxps(a, b); 1595 } 1596 else 1597 { 1598 // ARM: Optimized into fcmgt + bsl since LDC 1.8 -02 1599 __m128 r; 1600 r[0] = (a[0] > b[0]) ? a[0] : b[0]; 1601 r[1] = (a[1] > b[1]) ? a[1] : b[1]; 1602 r[2] = (a[2] > b[2]) ? a[2] : b[2]; 1603 r[3] = (a[3] > b[3]) ? a[3] : b[3]; 1604 return r; 1605 } 1606 } 1607 unittest 1608 { 1609 __m128 A = _mm_setr_ps(1, 2, float.nan, 4); 1610 __m128 B = _mm_setr_ps(4, 1, 4, float.nan); 1611 __m128 M = _mm_max_ps(A, B); 1612 assert(M.array[0] == 4); 1613 assert(M.array[1] == 2); 1614 assert(M.array[2] == 4); // in case of NaN, second operand prevails (as it seems) 1615 assert(M.array[3] != M.array[3]); // in case of NaN, second operand prevails (as it seems) 1616 } 1617 1618 /// Compare packed unsigned 8-bit integers in `a` and `b`, and return packed maximum values. 1619 __m64 _mm_max_pu8 (__m64 a, __m64 b) pure @safe 1620 { 1621 return to_m64(_mm_max_epu8(to_m128i(a), to_m128i(b))); 1622 } 1623 1624 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b`, store the maximum value in the 1625 /// lower element of result, and copy the upper 3 packed elements from `a` to the upper element of result. 1626 __m128 _mm_max_ss(__m128 a, __m128 b) pure @safe 1627 { 1628 static if (DMD_with_DSIMD) 1629 { 1630 return cast(__m128) __simd(XMM.MAXSS, a, b); 1631 } 1632 else static if (GDC_with_SSE) 1633 { 1634 return __builtin_ia32_maxss(a, b); 1635 } 1636 else static if (LDC_with_SSE1) 1637 { 1638 return __builtin_ia32_maxss(a, b); 1639 } 1640 else 1641 { 1642 __m128 r = a; 1643 r[0] = (a[0] > b[0]) ? a[0] : b[0]; 1644 return r; 1645 } 1646 } 1647 unittest 1648 { 1649 __m128 A = _mm_setr_ps(1, 2, 3, 4); 1650 __m128 B = _mm_setr_ps(4, 1, 4, 1); 1651 __m128 C = _mm_setr_ps(float.nan, 1, 4, 1); 1652 __m128 M = _mm_max_ss(A, B); 1653 assert(M.array[0] == 4); 1654 assert(M.array[1] == 2); 1655 assert(M.array[2] == 3); 1656 assert(M.array[3] == 4); 1657 M = _mm_max_ps(A, C); // in case of NaN, second operand prevails 1658 assert(M.array[0] != M.array[0]); 1659 M = _mm_max_ps(C, A); // in case of NaN, second operand prevails 1660 assert(M.array[0] == 1); 1661 } 1662 1663 /// Compare packed signed 16-bit integers in a and b, and return packed minimum values. 1664 __m64 _mm_min_pi16 (__m64 a, __m64 b) pure @safe 1665 { 1666 return to_m64(_mm_min_epi16(to_m128i(a), to_m128i(b))); 1667 } 1668 1669 /// Compare packed single-precision (32-bit) floating-point elements in `a` and `b`, and return packed maximum values. 1670 __m128 _mm_min_ps(__m128 a, __m128 b) pure @safe 1671 { 1672 static if (DMD_with_DSIMD) 1673 { 1674 return cast(__m128) __simd(XMM.MINPS, a, b); 1675 } 1676 else static if (GDC_with_SSE) 1677 { 1678 return __builtin_ia32_minps(a, b); 1679 } 1680 else static if (LDC_with_SSE1) 1681 { 1682 // not technically needed, but better perf in debug mode 1683 return __builtin_ia32_minps(a, b); 1684 } 1685 else 1686 { 1687 // ARM: Optimized into fcmgt + bsl since LDC 1.8 -02 1688 __m128 r; 1689 r[0] = (a[0] < b[0]) ? a[0] : b[0]; 1690 r[1] = (a[1] < b[1]) ? a[1] : b[1]; 1691 r[2] = (a[2] < b[2]) ? a[2] : b[2]; 1692 r[3] = (a[3] < b[3]) ? a[3] : b[3]; 1693 return r; 1694 } 1695 } 1696 unittest 1697 { 1698 __m128 A = _mm_setr_ps(1, 2, float.nan, 4); 1699 __m128 B = _mm_setr_ps(4, 1, 4, float.nan); 1700 __m128 M = _mm_min_ps(A, B); 1701 assert(M.array[0] == 1); 1702 assert(M.array[1] == 1); 1703 assert(M.array[2] == 4); // in case of NaN, second operand prevails (as it seems) 1704 assert(M.array[3] != M.array[3]); // in case of NaN, second operand prevails (as it seems) 1705 } 1706 1707 /// Compare packed unsigned 8-bit integers in `a` and `b`, and return packed minimum values. 1708 __m64 _mm_min_pu8 (__m64 a, __m64 b) pure @safe 1709 { 1710 return to_m64(_mm_min_epu8(to_m128i(a), to_m128i(b))); 1711 } 1712 1713 /// Compare the lower single-precision (32-bit) floating-point elements in `a` and `b`, store the minimum value in the 1714 /// lower element of result, and copy the upper 3 packed elements from `a` to the upper element of result. 1715 __m128 _mm_min_ss(__m128 a, __m128 b) pure @safe 1716 { 1717 static if (DMD_with_DSIMD) 1718 { 1719 return cast(__m128) __simd(XMM.MINSS, a, b); 1720 } 1721 else static if (GDC_with_SSE) 1722 { 1723 return __builtin_ia32_minss(a, b); 1724 } 1725 else static if (LDC_with_SSE1) 1726 { 1727 return __builtin_ia32_minss(a, b); 1728 } 1729 else 1730 { 1731 // Generates minss since LDC 1.3 -O1 1732 __m128 r = a; 1733 r[0] = (a[0] < b[0]) ? a[0] : b[0]; 1734 return r; 1735 } 1736 } 1737 unittest 1738 { 1739 __m128 A = _mm_setr_ps(1, 2, 3, 4); 1740 __m128 B = _mm_setr_ps(4, 1, 4, 1); 1741 __m128 C = _mm_setr_ps(float.nan, 1, 4, 1); 1742 __m128 M = _mm_min_ss(A, B); 1743 assert(M.array[0] == 1); 1744 assert(M.array[1] == 2); 1745 assert(M.array[2] == 3); 1746 assert(M.array[3] == 4); 1747 M = _mm_min_ps(A, C); // in case of NaN, second operand prevails 1748 assert(M.array[0] != M.array[0]); 1749 M = _mm_min_ps(C, A); // in case of NaN, second operand prevails 1750 assert(M.array[0] == 1); 1751 } 1752 1753 /// Move the lower single-precision (32-bit) floating-point element from `b` to the lower element of result, and copy 1754 /// the upper 3 packed elements from `a` to the upper elements of result. 1755 __m128 _mm_move_ss (__m128 a, __m128 b) pure @trusted 1756 { 1757 // Workaround https://issues.dlang.org/show_bug.cgi?id=21673 1758 // inlining of this function fails. 1759 version(DigitalMars) asm nothrow @nogc pure { nop; } 1760 1761 a.ptr[0] = b.array[0]; 1762 return a; 1763 } 1764 unittest 1765 { 1766 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f); 1767 __m128 B = _mm_setr_ps(5.0f, 6.0f, 7.0f, 8.0f); 1768 __m128 R = _mm_move_ss(A, B); 1769 float[4] correct = [5.0f, 2.0f, 3.0f, 4.0f]; 1770 assert(R.array == correct); 1771 } 1772 1773 /// Move the upper 2 single-precision (32-bit) floating-point elements from `b` to the lower 2 elements of result, and 1774 /// copy the upper 2 elements from `a` to the upper 2 elements of dst. 1775 __m128 _mm_movehl_ps (__m128 a, __m128 b) pure @trusted 1776 { 1777 // Disabled because of https://issues.dlang.org/show_bug.cgi?id=19443 1778 /* 1779 static if (DMD_with_DSIMD) 1780 { 1781 1782 return cast(__m128) __simd(XMM.MOVHLPS, a, b); 1783 } 1784 else */ 1785 { 1786 a.ptr[0] = b.array[2]; 1787 a.ptr[1] = b.array[3]; 1788 return a; 1789 } 1790 } 1791 unittest 1792 { 1793 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f); 1794 __m128 B = _mm_setr_ps(5.0f, 6.0f, 7.0f, 8.0f); 1795 __m128 R = _mm_movehl_ps(A, B); 1796 float[4] correct = [7.0f, 8.0f, 3.0f, 4.0f]; 1797 assert(R.array == correct); 1798 } 1799 1800 /// Move the lower 2 single-precision (32-bit) floating-point elements from `b` to the upper 2 elements of result, and 1801 /// copy the lower 2 elements from `a` to the lower 2 elements of result 1802 __m128 _mm_movelh_ps (__m128 a, __m128 b) pure @trusted 1803 { 1804 // Disabled because of https://issues.dlang.org/show_bug.cgi?id=19443 1805 /* 1806 static if (DMD_with_DSIMD) 1807 { 1808 return cast(__m128) __simd(XMM.MOVLHPS, a, b); 1809 } 1810 else 1811 */ 1812 { 1813 a.ptr[2] = b.array[0]; 1814 a.ptr[3] = b.array[1]; 1815 return a; 1816 } 1817 } 1818 unittest 1819 { 1820 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f); 1821 __m128 B = _mm_setr_ps(5.0f, 6.0f, 7.0f, 8.0f); 1822 __m128 R = _mm_movelh_ps(A, B); 1823 float[4] correct = [1.0f, 2.0f, 5.0f, 6.0f]; 1824 assert(R.array == correct); 1825 } 1826 1827 /// Create mask from the most significant bit of each 8-bit element in `a`. 1828 int _mm_movemask_pi8 (__m64 a) pure @safe 1829 { 1830 return _mm_movemask_epi8(to_m128i(a)); 1831 } 1832 unittest 1833 { 1834 assert(0x9C == _mm_movemask_pi8(_mm_set_pi8(-1, 0, 0, -1, -1, -1, 0, 0))); 1835 } 1836 1837 /// Set each bit of result based on the most significant bit of the corresponding packed single-precision (32-bit) 1838 /// floating-point element in `a`. 1839 int _mm_movemask_ps (__m128 a) pure @trusted 1840 { 1841 // PERF: Not possible in D_SIMD because of https://issues.dlang.org/show_bug.cgi?id=8047 1842 static if (GDC_with_SSE) 1843 { 1844 return __builtin_ia32_movmskps(a); 1845 } 1846 else static if (LDC_with_SSE1) 1847 { 1848 return __builtin_ia32_movmskps(a); 1849 } 1850 else static if (LDC_with_ARM) 1851 { 1852 int4 ai = cast(int4)a; 1853 int4 shift31 = [31, 31, 31, 31]; 1854 ai = ai >>> shift31; 1855 int4 shift = [0, 1, 2, 3]; 1856 ai = ai << shift; // 4-way shift, only efficient on ARM. 1857 int r = ai.array[0] + (ai.array[1]) + (ai.array[2]) + (ai.array[3]); 1858 return r; 1859 } 1860 else 1861 { 1862 int4 ai = cast(int4)a; 1863 int r = 0; 1864 if (ai.array[0] < 0) r += 1; 1865 if (ai.array[1] < 0) r += 2; 1866 if (ai.array[2] < 0) r += 4; 1867 if (ai.array[3] < 0) r += 8; 1868 return r; 1869 } 1870 } 1871 unittest 1872 { 1873 int4 A = [-1, 0, -43, 0]; 1874 assert(5 == _mm_movemask_ps(cast(float4)A)); 1875 } 1876 1877 /// Multiply packed single-precision (32-bit) floating-point elements in `a` and `b`. 1878 __m128 _mm_mul_ps(__m128 a, __m128 b) pure @safe 1879 { 1880 pragma(inline, true); 1881 return a * b; 1882 } 1883 unittest 1884 { 1885 __m128 a = [1.5f, -2.0f, 3.0f, 1.0f]; 1886 a = _mm_mul_ps(a, a); 1887 float[4] correct = [2.25f, 4.0f, 9.0f, 1.0f]; 1888 assert(a.array == correct); 1889 } 1890 1891 /// Multiply the lower single-precision (32-bit) floating-point element in `a` and `b`, store the result in the lower 1892 /// element of result, and copy the upper 3 packed elements from `a` to the upper elements of result. 1893 __m128 _mm_mul_ss(__m128 a, __m128 b) pure @safe 1894 { 1895 static if (DMD_with_DSIMD) 1896 return cast(__m128) __simd(XMM.MULSS, a, b); 1897 else static if (GDC_with_SSE) 1898 return __builtin_ia32_mulss(a, b); 1899 else 1900 { 1901 a[0] *= b[0]; 1902 return a; 1903 } 1904 } 1905 unittest 1906 { 1907 __m128 a = [1.5f, -2.0f, 3.0f, 1.0f]; 1908 a = _mm_mul_ss(a, a); 1909 float[4] correct = [2.25f, -2.0f, 3.0f, 1.0f]; 1910 assert(a.array == correct); 1911 } 1912 1913 /// Multiply the packed unsigned 16-bit integers in `a` and `b`, producing intermediate 32-bit integers, 1914 /// and return the high 16 bits of the intermediate integers. 1915 __m64 _mm_mulhi_pu16 (__m64 a, __m64 b) pure @safe 1916 { 1917 return to_m64(_mm_mulhi_epu16(to_m128i(a), to_m128i(b))); 1918 } 1919 unittest 1920 { 1921 __m64 A = _mm_setr_pi16(0, -16, 2, 3); 1922 __m64 B = _mm_set1_pi16(16384); 1923 short4 R = cast(short4)_mm_mulhi_pu16(A, B); 1924 short[4] correct = [0, 0x3FFC, 0, 0]; 1925 assert(R.array == correct); 1926 } 1927 1928 /// Compute the bitwise OR of packed single-precision (32-bit) floating-point elements in `a` and `b`, and 1929 /// return the result. 1930 __m128 _mm_or_ps (__m128 a, __m128 b) pure @safe 1931 { 1932 static if (DMD_with_DSIMD) 1933 return cast(__m128)__simd(XMM.ORPS, a, b); 1934 else 1935 return cast(__m128)(cast(__m128i)a | cast(__m128i)b); 1936 } 1937 // TODO unittest and force inline 1938 1939 deprecated("Use _mm_avg_pu8 instead") alias _m_pavgb = _mm_avg_pu8;/// 1940 deprecated("Use _mm_avg_pu16 instead") alias _m_pavgw = _mm_avg_pu16;/// 1941 deprecated("Use _mm_extract_pi16 instead") alias _m_pextrw = _mm_extract_pi16;/// 1942 deprecated("Use _mm_insert_pi16 instead") alias _m_pinsrw = _mm_insert_pi16;/// 1943 deprecated("Use _mm_max_pi16 instead") alias _m_pmaxsw = _mm_max_pi16;/// 1944 deprecated("Use _mm_max_pu8 instead") alias _m_pmaxub = _mm_max_pu8;/// 1945 deprecated("Use _mm_min_pi16 instead") alias _m_pminsw = _mm_min_pi16;/// 1946 deprecated("Use _mm_min_pu8 instead") alias _m_pminub = _mm_min_pu8;/// 1947 deprecated("Use _mm_movemask_pi8 instead") alias _m_pmovmskb = _mm_movemask_pi8;/// 1948 deprecated("Use _mm_mulhi_pu16 instead") alias _m_pmulhuw = _mm_mulhi_pu16;/// 1949 1950 enum _MM_HINT_T0 = 3; /// 1951 enum _MM_HINT_T1 = 2; /// 1952 enum _MM_HINT_T2 = 1; /// 1953 enum _MM_HINT_NTA = 0; /// 1954 1955 1956 version(LDC) 1957 { 1958 // Starting with LLVM 10, it seems llvm.prefetch has changed its name. 1959 // Was reported at: https://github.com/ldc-developers/ldc/issues/3397 1960 static if (__VERSION__ >= 2091) 1961 { 1962 pragma(LDC_intrinsic, "llvm.prefetch.p0i8") // was "llvm.prefetch" 1963 void llvm_prefetch_fixed(void* ptr, uint rw, uint locality, uint cachetype) pure @safe; 1964 } 1965 } 1966 1967 /// Fetch the line of data from memory that contains address `p` to a location in the 1968 /// cache hierarchy specified by the locality hint i. 1969 /// 1970 /// Warning: `locality` is a compile-time parameter, unlike in Intel Intrinsics API. 1971 void _mm_prefetch(int locality)(const(void)* p) pure @trusted 1972 { 1973 static if (GDC_with_SSE) 1974 { 1975 return __builtin_prefetch(p, (locality & 0x4) >> 2, locality & 0x3); 1976 } 1977 else static if (DMD_with_DSIMD) 1978 { 1979 enum bool isWrite = (locality & 0x4) != 0; 1980 enum level = locality & 3; 1981 return prefetch!(isWrite, level)(p); 1982 } 1983 else version(LDC) 1984 { 1985 static if (__VERSION__ >= 2091) 1986 { 1987 // const_cast here. `llvm_prefetch` wants a mutable pointer 1988 llvm_prefetch_fixed( cast(void*)p, 0, locality, 1); 1989 } 1990 else 1991 { 1992 // const_cast here. `llvm_prefetch` wants a mutable pointer 1993 llvm_prefetch( cast(void*)p, 0, locality, 1); 1994 } 1995 } 1996 else version(D_InlineAsm_X86_64) 1997 { 1998 static if (locality == _MM_HINT_NTA) 1999 { 2000 asm pure nothrow @nogc @trusted 2001 { 2002 mov RAX, p; 2003 prefetchnta [RAX]; 2004 } 2005 } 2006 else static if (locality == _MM_HINT_T0) 2007 { 2008 asm pure nothrow @nogc @trusted 2009 { 2010 mov RAX, p; 2011 prefetcht0 [RAX]; 2012 } 2013 } 2014 else static if (locality == _MM_HINT_T1) 2015 { 2016 asm pure nothrow @nogc @trusted 2017 { 2018 mov RAX, p; 2019 prefetcht1 [RAX]; 2020 } 2021 } 2022 else static if (locality == _MM_HINT_T2) 2023 { 2024 asm pure nothrow @nogc @trusted 2025 { 2026 mov RAX, p; 2027 prefetcht2 [RAX]; 2028 } 2029 } 2030 else 2031 assert(false); // invalid locality hint 2032 } 2033 else version(D_InlineAsm_X86) 2034 { 2035 static if (locality == _MM_HINT_NTA) 2036 { 2037 asm pure nothrow @nogc @trusted 2038 { 2039 mov EAX, p; 2040 prefetchnta [EAX]; 2041 } 2042 } 2043 else static if (locality == _MM_HINT_T0) 2044 { 2045 asm pure nothrow @nogc @trusted 2046 { 2047 mov EAX, p; 2048 prefetcht0 [EAX]; 2049 } 2050 } 2051 else static if (locality == _MM_HINT_T1) 2052 { 2053 asm pure nothrow @nogc @trusted 2054 { 2055 mov EAX, p; 2056 prefetcht1 [EAX]; 2057 } 2058 } 2059 else static if (locality == _MM_HINT_T2) 2060 { 2061 asm pure nothrow @nogc @trusted 2062 { 2063 mov EAX, p; 2064 prefetcht2 [EAX]; 2065 } 2066 } 2067 else 2068 assert(false); // invalid locality hint 2069 } 2070 else 2071 { 2072 // Generic version: do nothing. From bitter experience, 2073 // it's unlikely you get ANY speed-up with manual prefetching. 2074 // Prefetching or not doesn't change program behaviour. 2075 } 2076 } 2077 unittest 2078 { 2079 // From Intel documentation: 2080 // "The amount of data prefetched is also processor implementation-dependent. It will, however, be a minimum of 2081 // 32 bytes." 2082 ubyte[256] cacheline; // though it seems it cannot generate GP fault 2083 _mm_prefetch!_MM_HINT_T0(cacheline.ptr); 2084 _mm_prefetch!_MM_HINT_T1(cacheline.ptr); 2085 _mm_prefetch!_MM_HINT_T2(cacheline.ptr); 2086 _mm_prefetch!_MM_HINT_NTA(cacheline.ptr); 2087 } 2088 2089 deprecated("Use _mm_sad_pu8 instead") alias _m_psadbw = _mm_sad_pu8;/// 2090 deprecated("Use _mm_shuffle_pi16 instead") alias _m_pshufw = _mm_shuffle_pi16;/// 2091 2092 2093 /// Compute the approximate reciprocal of packed single-precision (32-bit) floating-point elements in a`` , 2094 /// and return the results. The maximum relative error for this approximation is less than 1.5*2^-12. 2095 __m128 _mm_rcp_ps (__m128 a) pure @trusted 2096 { 2097 static if (DMD_with_DSIMD) 2098 { 2099 return cast(__m128) __simd(XMM.RCPPS, a); 2100 } 2101 else static if (GDC_with_SSE) 2102 { 2103 return __builtin_ia32_rcpps(a); 2104 } 2105 else static if (LDC_with_SSE1) 2106 { 2107 return __builtin_ia32_rcpps(a); 2108 } 2109 else 2110 { 2111 a.ptr[0] = 1.0f / a.array[0]; 2112 a.ptr[1] = 1.0f / a.array[1]; 2113 a.ptr[2] = 1.0f / a.array[2]; 2114 a.ptr[3] = 1.0f / a.array[3]; 2115 return a; 2116 } 2117 } 2118 unittest 2119 { 2120 __m128 A = _mm_setr_ps(2.34f, -70000.0f, 0.00001f, 345.5f); 2121 __m128 groundTruth = _mm_set1_ps(1.0f) / A; 2122 __m128 result = _mm_rcp_ps(A); 2123 foreach(i; 0..4) 2124 { 2125 double relError = (cast(double)(groundTruth.array[i]) / result.array[i]) - 1; 2126 assert(abs_double(relError) < 0.00037); // 1.5*2^-12 is 0.00036621093 2127 } 2128 } 2129 2130 /// Compute the approximate reciprocal of the lower single-precision (32-bit) floating-point element in `a`, store it 2131 /// in the lower element of the result, and copy the upper 3 packed elements from `a` to the upper elements of result. 2132 /// The maximum relative error for this approximation is less than 1.5*2^-12. 2133 __m128 _mm_rcp_ss (__m128 a) pure @trusted 2134 { 2135 static if (DMD_with_DSIMD) 2136 { 2137 return cast(__m128) __simd(XMM.RCPSS, a); 2138 } 2139 else static if (GDC_with_SSE) 2140 { 2141 return __builtin_ia32_rcpss(a); 2142 } 2143 else static if (LDC_with_SSE1) 2144 { 2145 return __builtin_ia32_rcpss(a); 2146 } 2147 else 2148 { 2149 a.ptr[0] = 1.0f / a.array[0]; 2150 return a; 2151 } 2152 } 2153 unittest 2154 { 2155 __m128 A = _mm_setr_ps(2.34f, -70000.0f, 0.00001f, 345.5f); 2156 __m128 correct = _mm_setr_ps(1 / 2.34f, -70000.0f, 0.00001f, 345.5f); 2157 __m128 R = _mm_rcp_ss(A); 2158 double relError = (cast(double)(correct.array[0]) / R.array[0]) - 1; 2159 assert(abs_double(relError) < 0.00037); // 1.5*2^-12 is 0.00036621093 2160 assert(R.array[1] == correct.array[1]); 2161 assert(R.array[2] == correct.array[2]); 2162 assert(R.array[3] == correct.array[3]); 2163 } 2164 2165 /// Reallocate `size` bytes of memory, aligned to the alignment specified in `alignment`, 2166 /// and return a pointer to the newly allocated memory. 2167 /// `_mm_free` or `alignedRealloc` with size 0 should be used to free memory that is 2168 /// allocated with `_mm_malloc` or `_mm_realloc`. 2169 /// Previous data is preserved. 2170 void* _mm_realloc(void* aligned, size_t size, size_t alignment) nothrow @nogc // #BONUS 2171 { 2172 return alignedReallocImpl!true(aligned, size, alignment); 2173 } 2174 unittest 2175 { 2176 enum NALLOC = 8; 2177 enum size_t[8] ALIGNMENTS = [1, 2, 4, 8, 16, 32, 64, 128]; 2178 2179 void*[NALLOC] alloc; 2180 2181 foreach(t; 0..100) 2182 { 2183 foreach(n; 0..NALLOC) 2184 { 2185 size_t alignment = ALIGNMENTS[n]; 2186 size_t s = ( (n + t * 69096) & 0xffff ); 2187 alloc[n] = _mm_realloc(alloc[n], s, alignment); 2188 assert(isPointerAligned(alloc[n], alignment)); 2189 foreach(b; 0..s) 2190 (cast(ubyte*)alloc[n])[b] = cast(ubyte)n; 2191 } 2192 } 2193 foreach(n; 0..NALLOC) 2194 { 2195 alloc[n] = _mm_realloc(alloc[n], 0, ALIGNMENTS[n]); 2196 } 2197 } 2198 2199 /// Reallocate `size` bytes of memory, aligned to the alignment specified in `alignment`, 2200 /// and return a pointer to the newly allocated memory. 2201 /// `_mm_free` or `alignedRealloc` with size 0 should be used to free memory that is 2202 /// allocated with `_mm_malloc` or `_mm_realloc`. 2203 /// Previous data is discarded. 2204 void* _mm_realloc_discard(void* aligned, size_t size, size_t alignment) nothrow @nogc // #BONUS 2205 { 2206 return alignedReallocImpl!false(aligned, size, alignment); 2207 } 2208 2209 /// Compute the approximate reciprocal square root of packed single-precision (32-bit) floating-point elements in `a`. 2210 /// The maximum relative error for this approximation is less than 1.5*2^-12. 2211 __m128 _mm_rsqrt_ps (__m128 a) pure @trusted 2212 { 2213 static if (DMD_with_DSIMD) 2214 { 2215 return cast(__m128) __simd(XMM.RSQRTPS, a); 2216 } 2217 else static if (GDC_with_SSE) 2218 { 2219 return __builtin_ia32_rsqrtps(a); 2220 } 2221 else static if (LDC_with_SSE1) 2222 { 2223 return __builtin_ia32_rsqrtps(a); 2224 } 2225 else version(LDC) 2226 { 2227 a[0] = 1.0f / llvm_sqrt(a[0]); 2228 a[1] = 1.0f / llvm_sqrt(a[1]); 2229 a[2] = 1.0f / llvm_sqrt(a[2]); 2230 a[3] = 1.0f / llvm_sqrt(a[3]); 2231 return a; 2232 } 2233 else 2234 { 2235 a.ptr[0] = 1.0f / sqrt(a.array[0]); 2236 a.ptr[1] = 1.0f / sqrt(a.array[1]); 2237 a.ptr[2] = 1.0f / sqrt(a.array[2]); 2238 a.ptr[3] = 1.0f / sqrt(a.array[3]); 2239 return a; 2240 } 2241 } 2242 unittest 2243 { 2244 __m128 A = _mm_setr_ps(2.34f, 70000.0f, 0.00001f, 345.5f); 2245 __m128 groundTruth = _mm_setr_ps(0.65372045f, 0.00377964473f, 316.227766f, 0.05379921937f); 2246 __m128 result = _mm_rsqrt_ps(A); 2247 foreach(i; 0..4) 2248 { 2249 double relError = (cast(double)(groundTruth.array[i]) / result.array[i]) - 1; 2250 assert(abs_double(relError) < 0.00037); // 1.5*2^-12 is 0.00036621093 2251 } 2252 } 2253 2254 /// Compute the approximate reciprocal square root of the lower single-precision (32-bit) floating-point element in `a`, 2255 /// store the result in the lower element. Copy the upper 3 packed elements from `a` to the upper elements of result. 2256 /// The maximum relative error for this approximation is less than 1.5*2^-12. 2257 __m128 _mm_rsqrt_ss (__m128 a) pure @trusted 2258 { 2259 static if (DMD_with_DSIMD) 2260 { 2261 return cast(__m128) __simd(XMM.RSQRTSS, a); 2262 } 2263 else static if (GDC_with_SSE) 2264 { 2265 return __builtin_ia32_rsqrtss(a); 2266 } 2267 else static if (LDC_with_SSE1) 2268 { 2269 return __builtin_ia32_rsqrtss(a); 2270 } 2271 else version(LDC) 2272 { 2273 a[0] = 1.0f / llvm_sqrt(a[0]); 2274 return a; 2275 } 2276 else 2277 { 2278 a[0] = 1.0f / sqrt(a[0]); 2279 return a; 2280 } 2281 } 2282 unittest // this one test 4 different intrinsics: _mm_rsqrt_ss, _mm_rsqrt_ps, _mm_rcp_ps, _mm_rcp_ss 2283 { 2284 double maxRelativeError = 0.000245; // -72 dB, stuff is apparently more precise than said in the doc? 2285 void testApproximateSSE(float number) nothrow @nogc 2286 { 2287 __m128 A = _mm_set1_ps(number); 2288 2289 // test _mm_rcp_ps 2290 __m128 B = _mm_rcp_ps(A); 2291 foreach(i; 0..4) 2292 { 2293 double exact = 1.0f / A.array[i]; 2294 double ratio = cast(double)(B.array[i]) / cast(double)(exact); 2295 assert(abs_double(ratio - 1) <= maxRelativeError); 2296 } 2297 2298 // test _mm_rcp_ss 2299 { 2300 B = _mm_rcp_ss(A); 2301 double exact = 1.0f / A.array[0]; 2302 double ratio = cast(double)(B.array[0]) / cast(double)(exact); 2303 assert(abs_double(ratio - 1) <= maxRelativeError); 2304 } 2305 2306 // test _mm_rsqrt_ps 2307 B = _mm_rsqrt_ps(A); 2308 foreach(i; 0..4) 2309 { 2310 double exact = 1.0f / sqrt(A.array[i]); 2311 double ratio = cast(double)(B.array[i]) / cast(double)(exact); 2312 assert(abs_double(ratio - 1) <= maxRelativeError); 2313 } 2314 2315 // test _mm_rsqrt_ss 2316 { 2317 B = _mm_rsqrt_ss(A); 2318 double exact = 1.0f / sqrt(A.array[0]); 2319 double ratio = cast(double)(B.array[0]) / cast(double)(exact); 2320 assert(abs_double(ratio - 1) <= maxRelativeError); 2321 } 2322 } 2323 2324 testApproximateSSE(0.00001f); 2325 testApproximateSSE(1.1f); 2326 testApproximateSSE(345.0f); 2327 testApproximateSSE(2.45674864151f); 2328 testApproximateSSE(700000.0f); 2329 testApproximateSSE(10000000.0f); 2330 testApproximateSSE(27841456468.0f); 2331 } 2332 2333 /// Compute the absolute differences of packed unsigned 8-bit integers in `a` and `b`, then horizontally sum each 2334 /// consecutive 8 differences to produce four unsigned 16-bit integers, and pack these unsigned 16-bit integers in the 2335 /// low 16 bits of result. 2336 __m64 _mm_sad_pu8 (__m64 a, __m64 b) pure @safe 2337 { 2338 return to_m64(_mm_sad_epu8(to_m128i(a), to_m128i(b))); 2339 } 2340 2341 /// Set the exception mask bits of the MXCSR control and status register to the value in unsigned 32-bit integer 2342 /// `_MM_MASK_xxxx`. The exception mask may contain any of the following flags: `_MM_MASK_INVALID`, `_MM_MASK_DIV_ZERO`, 2343 /// `_MM_MASK_DENORM`, `_MM_MASK_OVERFLOW`, `_MM_MASK_UNDERFLOW`, `_MM_MASK_INEXACT`. 2344 void _MM_SET_EXCEPTION_MASK(int _MM_MASK_xxxx) @safe 2345 { 2346 // Note: unsupported on ARM 2347 _mm_setcsr((_mm_getcsr() & ~_MM_MASK_MASK) | _MM_MASK_xxxx); 2348 } 2349 2350 /// Set the exception state bits of the MXCSR control and status register to the value in unsigned 32-bit integer 2351 /// `_MM_EXCEPT_xxxx`. The exception state may contain any of the following flags: `_MM_EXCEPT_INVALID`, 2352 /// `_MM_EXCEPT_DIV_ZERO`, `_MM_EXCEPT_DENORM`, `_MM_EXCEPT_OVERFLOW`, `_MM_EXCEPT_UNDERFLOW`, `_MM_EXCEPT_INEXACT`. 2353 void _MM_SET_EXCEPTION_STATE(int _MM_EXCEPT_xxxx) @safe 2354 { 2355 // Note: unsupported on ARM 2356 _mm_setcsr((_mm_getcsr() & ~_MM_EXCEPT_MASK) | _MM_EXCEPT_xxxx); 2357 } 2358 2359 /// Set the flush zero bits of the MXCSR control and status register to the value in unsigned 32-bit integer 2360 /// `_MM_FLUSH_xxxx`. The flush zero may contain any of the following flags: `_MM_FLUSH_ZERO_ON` or `_MM_FLUSH_ZERO_OFF`. 2361 void _MM_SET_FLUSH_ZERO_MODE(int _MM_FLUSH_xxxx) @safe 2362 { 2363 _mm_setcsr((_mm_getcsr() & ~_MM_FLUSH_ZERO_MASK) | _MM_FLUSH_xxxx); 2364 } 2365 2366 /// Set packed single-precision (32-bit) floating-point elements with the supplied values. 2367 __m128 _mm_set_ps (float e3, float e2, float e1, float e0) pure @trusted 2368 { 2369 // Note: despite appearances, generates sensible code, 2370 // inlines correctly and is constant folded 2371 float[4] result = [e0, e1, e2, e3]; 2372 return loadUnaligned!(float4)(result.ptr); 2373 } 2374 unittest 2375 { 2376 __m128 A = _mm_set_ps(3, 2, 1, 546); 2377 float[4] correct = [546.0f, 1.0f, 2.0f, 3.0f]; 2378 assert(A.array == correct); 2379 } 2380 2381 deprecated("Use _mm_set1_ps instead") alias _mm_set_ps1 = _mm_set1_ps; /// 2382 2383 /// Set the rounding mode bits of the MXCSR control and status register to the value in unsigned 32-bit integer 2384 /// `_MM_ROUND_xxxx`. The rounding mode may contain any of the following flags: `_MM_ROUND_NEAREST`, `_MM_ROUND_DOWN`, 2385 /// `_MM_ROUND_UP`, `_MM_ROUND_TOWARD_ZERO`. 2386 void _MM_SET_ROUNDING_MODE(int _MM_ROUND_xxxx) @safe 2387 { 2388 // Work-around for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98607 2389 version(GNU) asm @trusted { "" : : : "memory"; } 2390 _mm_setcsr((_mm_getcsr() & ~_MM_ROUND_MASK) | _MM_ROUND_xxxx); 2391 } 2392 2393 /// Copy single-precision (32-bit) floating-point element `a` to the lower element of result, and zero the upper 3 elements. 2394 __m128 _mm_set_ss (float a) pure @trusted 2395 { 2396 static if (DMD_with_DSIMD) 2397 { 2398 return cast(__m128) __simd(XMM.LODSS, a); 2399 } 2400 else 2401 { 2402 __m128 r = _mm_setzero_ps(); 2403 r.ptr[0] = a; 2404 return r; 2405 } 2406 } 2407 unittest 2408 { 2409 float[4] correct = [42.0f, 0.0f, 0.0f, 0.0f]; 2410 __m128 A = _mm_set_ss(42.0f); 2411 assert(A.array == correct); 2412 } 2413 2414 /// Broadcast single-precision (32-bit) floating-point value `a` to all elements. 2415 __m128 _mm_set1_ps (float a) pure @trusted 2416 { 2417 pragma(inline, true); 2418 __m128 r = a; 2419 return r; 2420 } 2421 unittest 2422 { 2423 float[4] correct = [42.0f, 42.0f, 42.0f, 42.0f]; 2424 __m128 A = _mm_set1_ps(42.0f); 2425 assert(A.array == correct); 2426 } 2427 2428 /// Set the MXCSR control and status register with the value in unsigned 32-bit integer `controlWord`. 2429 void _mm_setcsr(uint controlWord) @trusted 2430 { 2431 static if (LDC_with_ARM) 2432 { 2433 // Convert from SSE to ARM control word. This is done _partially_ 2434 // and only support rounding mode changes. 2435 2436 // "To alter some bits of a VFP system register without 2437 // affecting other bits, use a read-modify-write procedure" 2438 uint fpscr = arm_get_fpcr(); 2439 2440 // Bits 23 to 22 are rounding modes, however not used in NEON 2441 fpscr = fpscr & ~_MM_ROUND_MASK_ARM; 2442 switch(controlWord & _MM_ROUND_MASK) 2443 { 2444 default: 2445 case _MM_ROUND_NEAREST: fpscr |= _MM_ROUND_NEAREST_ARM; break; 2446 case _MM_ROUND_DOWN: fpscr |= _MM_ROUND_DOWN_ARM; break; 2447 case _MM_ROUND_UP: fpscr |= _MM_ROUND_UP_ARM; break; 2448 case _MM_ROUND_TOWARD_ZERO: fpscr |= _MM_ROUND_TOWARD_ZERO_ARM; break; 2449 } 2450 fpscr = fpscr & ~_MM_FLUSH_ZERO_MASK_ARM; 2451 if (controlWord & _MM_FLUSH_ZERO_MASK) 2452 fpscr |= _MM_FLUSH_ZERO_MASK_ARM; 2453 arm_set_fpcr(fpscr); 2454 } 2455 else version(GNU) 2456 { 2457 static if (GDC_with_SSE) 2458 { 2459 // Work-around for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98607 2460 version(GNU) asm @trusted { "" : : : "memory"; } 2461 __builtin_ia32_ldmxcsr(controlWord); 2462 } 2463 else version(X86) 2464 { 2465 asm nothrow @nogc @trusted 2466 { 2467 "ldmxcsr %0;\n" 2468 : 2469 : "m" (controlWord) 2470 : ; 2471 } 2472 } 2473 else 2474 static assert(false); 2475 } 2476 else version (InlineX86Asm) 2477 { 2478 asm nothrow @nogc @safe 2479 { 2480 ldmxcsr controlWord; 2481 } 2482 } 2483 else 2484 static assert(0, "Not yet supported"); 2485 } 2486 unittest 2487 { 2488 _mm_setcsr(_mm_getcsr()); 2489 } 2490 2491 /// Set packed single-precision (32-bit) floating-point elements with the supplied values in reverse order. 2492 __m128 _mm_setr_ps (float e3, float e2, float e1, float e0) pure @trusted 2493 { 2494 pragma(inline, true); 2495 version(LDC) 2496 { 2497 float[4] result = [e3, e2, e1, e0]; 2498 return loadUnaligned!(float4)(result.ptr); 2499 } 2500 else 2501 { 2502 __m128 r; 2503 r.ptr[0] = e3; 2504 r.ptr[1] = e2; 2505 r.ptr[2] = e1; 2506 r.ptr[3] = e0; 2507 return r; 2508 } 2509 } 2510 unittest 2511 { 2512 __m128 A = _mm_setr_ps(3, 2, 1, 546); 2513 float[4] correct = [3.0f, 2.0f, 1.0f, 546.0f]; 2514 assert(A.array == correct); 2515 assert(A.array[0] == 3.0f); 2516 assert(A.array[1] == 2.0f); 2517 assert(A.array[2] == 1.0f); 2518 assert(A.array[3] == 546.0f); 2519 } 2520 2521 /// Return vector of type `__m128` with all elements set to zero. 2522 __m128 _mm_setzero_ps() pure @trusted 2523 { 2524 pragma(inline, true); 2525 float4 r; 2526 r = 0.0f; 2527 return r; 2528 } 2529 unittest 2530 { 2531 __m128 R = _mm_setzero_ps(); 2532 float[4] correct = [0.0f, 0, 0, 0]; 2533 assert(R.array == correct); 2534 } 2535 2536 /// Perform a serializing operation on all store-to-memory instructions that were issued prior 2537 /// to this instruction. Guarantees that every store instruction that precedes, in program order, 2538 /// is globally visible before any store instruction which follows the fence in program order. 2539 void _mm_sfence() @trusted 2540 { 2541 version(GNU) 2542 { 2543 static if (GDC_with_SSE) 2544 { 2545 __builtin_ia32_sfence(); 2546 } 2547 else version(X86) 2548 { 2549 asm pure nothrow @nogc @trusted 2550 { 2551 "sfence;\n" : : : ; 2552 } 2553 } 2554 else 2555 static assert(false); 2556 } 2557 else static if (LDC_with_SSE1) 2558 { 2559 __builtin_ia32_sfence(); 2560 } 2561 else static if (DMD_with_asm) 2562 { 2563 asm nothrow @nogc pure @safe 2564 { 2565 sfence; 2566 } 2567 } 2568 else version(LDC) 2569 { 2570 llvm_memory_fence(); // PERF: this generates mfence instead of sfence 2571 } 2572 else 2573 static assert(false); 2574 } 2575 unittest 2576 { 2577 _mm_sfence(); 2578 } 2579 2580 /// Warning: the immediate shuffle value `imm8` is given at compile-time instead of runtime. 2581 __m64 _mm_shuffle_pi16(int imm8)(__m64 a) pure @safe 2582 { 2583 return cast(__m64) shufflevector!(short4, ( (imm8 >> 0) & 3 ), 2584 ( (imm8 >> 2) & 3 ), 2585 ( (imm8 >> 4) & 3 ), 2586 ( (imm8 >> 6) & 3 ))(cast(short4)a, cast(short4)a); 2587 } 2588 unittest 2589 { 2590 __m64 A = _mm_setr_pi16(0, 1, 2, 3); 2591 enum int SHUFFLE = _MM_SHUFFLE(0, 1, 2, 3); 2592 short4 B = cast(short4) _mm_shuffle_pi16!SHUFFLE(A); 2593 short[4] expectedB = [ 3, 2, 1, 0 ]; 2594 assert(B.array == expectedB); 2595 } 2596 2597 /// Warning: the immediate shuffle value `imm8` is given at compile-time instead of runtime. 2598 __m128 _mm_shuffle_ps(ubyte imm)(__m128 a, __m128 b) pure @safe 2599 { 2600 return shufflevector!(__m128, imm & 3, (imm>>2) & 3, 4 + ((imm>>4) & 3), 4 + ((imm>>6) & 3) )(a, b); 2601 } 2602 2603 /// Compute the square root of packed single-precision (32-bit) floating-point elements in `a`. 2604 __m128 _mm_sqrt_ps(__m128 a) @trusted 2605 { 2606 static if (GDC_with_SSE) 2607 { 2608 return __builtin_ia32_sqrtps(a); 2609 } 2610 else version(LDC) 2611 { 2612 // Disappeared with LDC 1.11 2613 static if (__VERSION__ < 2081) 2614 return __builtin_ia32_sqrtps(a); 2615 else 2616 { 2617 a[0] = llvm_sqrt(a[0]); 2618 a[1] = llvm_sqrt(a[1]); 2619 a[2] = llvm_sqrt(a[2]); 2620 a[3] = llvm_sqrt(a[3]); 2621 return a; 2622 } 2623 } 2624 else 2625 { 2626 a.ptr[0] = sqrt(a.array[0]); 2627 a.ptr[1] = sqrt(a.array[1]); 2628 a.ptr[2] = sqrt(a.array[2]); 2629 a.ptr[3] = sqrt(a.array[3]); 2630 return a; 2631 } 2632 } 2633 unittest 2634 { 2635 __m128 A = _mm_sqrt_ps(_mm_set1_ps(4.0f)); 2636 assert(A.array[0] == 2.0f); 2637 assert(A.array[1] == 2.0f); 2638 assert(A.array[2] == 2.0f); 2639 assert(A.array[3] == 2.0f); 2640 } 2641 2642 /// Compute the square root of the lower single-precision (32-bit) floating-point element in `a`, store it in the lower 2643 /// element, and copy the upper 3 packed elements from `a` to the upper elements of result. 2644 __m128 _mm_sqrt_ss(__m128 a) @trusted 2645 { 2646 static if (GDC_with_SSE) 2647 { 2648 return __builtin_ia32_sqrtss(a); 2649 } 2650 else version(LDC) 2651 { 2652 a.ptr[0] = llvm_sqrt(a.array[0]); 2653 return a; 2654 } 2655 else 2656 { 2657 a.ptr[0] = sqrt(a.array[0]); 2658 return a; 2659 } 2660 } 2661 unittest 2662 { 2663 __m128 A = _mm_sqrt_ss(_mm_set1_ps(4.0f)); 2664 assert(A.array[0] == 2.0f); 2665 assert(A.array[1] == 4.0f); 2666 assert(A.array[2] == 4.0f); 2667 assert(A.array[3] == 4.0f); 2668 } 2669 2670 /// Store 128-bits (composed of 4 packed single-precision (32-bit) floating-point elements) from `a` into memory. 2671 /// `mem_addr` must be aligned on a 16-byte boundary or a general-protection exception may be generated. 2672 void _mm_store_ps (float* mem_addr, __m128 a) pure 2673 { 2674 pragma(inline, true); 2675 __m128* aligned = cast(__m128*)mem_addr; 2676 *aligned = a; 2677 } 2678 2679 deprecated("Use _mm_store1_ps instead") alias _mm_store_ps1 = _mm_store1_ps; /// 2680 2681 /// Store the lower single-precision (32-bit) floating-point element from `a` into memory. 2682 /// `mem_addr` does not need to be aligned on any particular boundary. 2683 void _mm_store_ss (float* mem_addr, __m128 a) pure @safe 2684 { 2685 pragma(inline, true); 2686 *mem_addr = a.array[0]; 2687 } 2688 unittest 2689 { 2690 float a; 2691 _mm_store_ss(&a, _mm_set_ps(3, 2, 1, 546)); 2692 assert(a == 546); 2693 } 2694 2695 /// Store the lower single-precision (32-bit) floating-point element from `a` into 4 contiguous elements in memory. 2696 /// `mem_addr` must be aligned on a 16-byte boundary or a general-protection exception may be generated. 2697 void _mm_store1_ps(float* mem_addr, __m128 a) pure @trusted // TODO: shouldn't be trusted 2698 { 2699 __m128* aligned = cast(__m128*)mem_addr; 2700 __m128 r; 2701 r.ptr[0] = a.array[0]; 2702 r.ptr[1] = a.array[0]; 2703 r.ptr[2] = a.array[0]; 2704 r.ptr[3] = a.array[0]; 2705 *aligned = r; 2706 } 2707 unittest 2708 { 2709 align(16) float[4] A; 2710 _mm_store1_ps(A.ptr, _mm_set_ss(42.0f)); 2711 float[4] correct = [42.0f, 42, 42, 42]; 2712 assert(A == correct); 2713 } 2714 2715 /// Store the upper 2 single-precision (32-bit) floating-point elements from `a` into memory. 2716 void _mm_storeh_pi(__m64* p, __m128 a) pure @trusted 2717 { 2718 pragma(inline, true); 2719 long2 la = cast(long2)a; 2720 (*p).ptr[0] = la.array[1]; 2721 } 2722 unittest 2723 { 2724 __m64 R = _mm_setzero_si64(); 2725 long2 A = [13, 25]; 2726 _mm_storeh_pi(&R, cast(__m128)A); 2727 assert(R.array[0] == 25); 2728 } 2729 2730 /// Store the lower 2 single-precision (32-bit) floating-point elements from `a` into memory. 2731 void _mm_storel_pi(__m64* p, __m128 a) pure @trusted 2732 { 2733 pragma(inline, true); 2734 long2 la = cast(long2)a; 2735 (*p).ptr[0] = la.array[0]; 2736 } 2737 unittest 2738 { 2739 __m64 R = _mm_setzero_si64(); 2740 long2 A = [13, 25]; 2741 _mm_storel_pi(&R, cast(__m128)A); 2742 assert(R.array[0] == 13); 2743 } 2744 2745 /// Store 4 single-precision (32-bit) floating-point elements from `a` into memory in reverse order. 2746 /// `mem_addr` must be aligned on a 16-byte boundary or a general-protection exception may be generated. 2747 void _mm_storer_ps(float* mem_addr, __m128 a) pure @trusted // TODO should not be trusted 2748 { 2749 __m128* aligned = cast(__m128*)mem_addr; 2750 __m128 r; 2751 r.ptr[0] = a.array[3]; 2752 r.ptr[1] = a.array[2]; 2753 r.ptr[2] = a.array[1]; 2754 r.ptr[3] = a.array[0]; 2755 *aligned = r; 2756 } 2757 unittest 2758 { 2759 align(16) float[4] A; 2760 _mm_storer_ps(A.ptr, _mm_setr_ps(1.0f, 2, 3, 4)); 2761 float[4] correct = [4.0f, 3.0f, 2.0f, 1.0f]; 2762 assert(A == correct); 2763 } 2764 2765 /// Store 128-bits (composed of 4 packed single-precision (32-bit) floating-point elements) from `a` into memory. 2766 /// `mem_addr` does not need to be aligned on any particular boundary. 2767 void _mm_storeu_ps(float* mem_addr, __m128 a) pure @safe // TODO should not be trusted 2768 { 2769 pragma(inline, true); 2770 storeUnaligned!(float4)(a, mem_addr); 2771 } 2772 2773 /// Store 64-bits of integer data from `a` into memory using a non-temporal memory hint. 2774 void _mm_stream_pi (__m64* mem_addr, __m64 a) 2775 { 2776 // BUG see `_mm_stream_ps` for an explanation why we don't implement non-temporal moves 2777 *mem_addr = a; // it's a regular move instead 2778 } 2779 2780 /// Store 128-bits (composed of 4 packed single-precision (32-bit) floating-point elements) from `a`s into memory using 2781 /// a non-temporal memory hint. mem_addr must be aligned on a 16-byte boundary or a general-protection exception may be 2782 /// generated. 2783 void _mm_stream_ps (float* mem_addr, __m128 a) 2784 { 2785 // BUG: can't implement non-temporal store with LDC inlineIR since !nontemporal 2786 // needs some IR outside this function that would say: 2787 // 2788 // !0 = !{ i32 1 } 2789 // 2790 // It's a LLVM IR metadata description. 2791 __m128* dest = cast(__m128*)mem_addr; 2792 *dest = a; // it's a regular move instead 2793 } 2794 unittest 2795 { 2796 align(16) float[4] A; 2797 _mm_stream_ps(A.ptr, _mm_set1_ps(78.0f)); 2798 assert(A[0] == 78.0f && A[1] == 78.0f && A[2] == 78.0f && A[3] == 78.0f); 2799 } 2800 2801 /// Subtract packed single-precision (32-bit) floating-point elements in `b` from packed single-precision (32-bit) 2802 /// floating-point elements in `a`. 2803 __m128 _mm_sub_ps(__m128 a, __m128 b) pure @safe 2804 { 2805 pragma(inline, true); 2806 return a - b; 2807 } 2808 unittest 2809 { 2810 __m128 a = [1.5f, -2.0f, 3.0f, 1.0f]; 2811 a = _mm_sub_ps(a, a); 2812 float[4] correct = [0.0f, 0.0f, 0.0f, 0.0f]; 2813 assert(a.array == correct); 2814 } 2815 2816 /// Subtract the lower single-precision (32-bit) floating-point element in `b` from the lower single-precision (32-bit) 2817 /// floating-point element in `a`, store the subtration result in the lower element of result, and copy the upper 3 2818 /// packed elements from a to the upper elements of result. 2819 __m128 _mm_sub_ss(__m128 a, __m128 b) pure @safe 2820 { 2821 static if (DMD_with_DSIMD) 2822 return cast(__m128) __simd(XMM.SUBSS, a, b); 2823 else static if (GDC_with_SSE) 2824 return __builtin_ia32_subss(a, b); 2825 else 2826 { 2827 a[0] -= b[0]; 2828 return a; 2829 } 2830 } 2831 unittest 2832 { 2833 __m128 a = [1.5f, -2.0f, 3.0f, 1.0f]; 2834 a = _mm_sub_ss(a, a); 2835 float[4] correct = [0.0f, -2.0, 3.0f, 1.0f]; 2836 assert(a.array == correct); 2837 } 2838 2839 /// Transpose the 4x4 matrix formed by the 4 rows of single-precision (32-bit) floating-point elements in row0, row1, 2840 /// row2, and row3, and store the transposed matrix in these vectors (row0 now contains column 0, etc.). 2841 void _MM_TRANSPOSE4_PS (ref __m128 row0, ref __m128 row1, ref __m128 row2, ref __m128 row3) pure @safe 2842 { 2843 __m128 tmp3, tmp2, tmp1, tmp0; 2844 tmp0 = _mm_unpacklo_ps(row0, row1); 2845 tmp2 = _mm_unpacklo_ps(row2, row3); 2846 tmp1 = _mm_unpackhi_ps(row0, row1); 2847 tmp3 = _mm_unpackhi_ps(row2, row3); 2848 row0 = _mm_movelh_ps(tmp0, tmp2); 2849 row1 = _mm_movehl_ps(tmp2, tmp0); 2850 row2 = _mm_movelh_ps(tmp1, tmp3); 2851 row3 = _mm_movehl_ps(tmp3, tmp1); 2852 } 2853 unittest 2854 { 2855 __m128 l0 = _mm_setr_ps(0, 1, 2, 3); 2856 __m128 l1 = _mm_setr_ps(4, 5, 6, 7); 2857 __m128 l2 = _mm_setr_ps(8, 9, 10, 11); 2858 __m128 l3 = _mm_setr_ps(12, 13, 14, 15); 2859 _MM_TRANSPOSE4_PS(l0, l1, l2, l3); 2860 float[4] r0 = [0.0f, 4, 8, 12]; 2861 float[4] r1 = [1.0f, 5, 9, 13]; 2862 float[4] r2 = [2.0f, 6, 10, 14]; 2863 float[4] r3 = [3.0f, 7, 11, 15]; 2864 assert(l0.array == r0); 2865 assert(l1.array == r1); 2866 assert(l2.array == r2); 2867 assert(l3.array == r3); 2868 } 2869 2870 // Note: the only difference between these intrinsics is the signalling 2871 // behaviour of quiet NaNs. This is incorrect but the case where 2872 // you would want to differentiate between qNaN and sNaN and then 2873 // treat them differently on purpose seems extremely rare. 2874 alias _mm_ucomieq_ss = _mm_comieq_ss; 2875 alias _mm_ucomige_ss = _mm_comige_ss; 2876 alias _mm_ucomigt_ss = _mm_comigt_ss; 2877 alias _mm_ucomile_ss = _mm_comile_ss; 2878 alias _mm_ucomilt_ss = _mm_comilt_ss; 2879 alias _mm_ucomineq_ss = _mm_comineq_ss; 2880 2881 /// Return vector of type `__m128` with undefined elements. 2882 __m128 _mm_undefined_ps() pure @safe 2883 { 2884 pragma(inline, true); 2885 __m128 undef = void; 2886 return undef; 2887 } 2888 2889 /// Unpack and interleave single-precision (32-bit) floating-point elements from the high half `a` and `b`. 2890 __m128 _mm_unpackhi_ps (__m128 a, __m128 b) pure @trusted 2891 { 2892 version(LDC) 2893 { 2894 // x86: plain version generates unpckhps with LDC 1.0.0 -O1, but shufflevector 8 less instructions in -O0 2895 return shufflevectorLDC!(__m128, 2, 6, 3, 7)(a, b); 2896 } 2897 else 2898 { 2899 __m128 r; 2900 r.ptr[0] = a.array[2]; 2901 r.ptr[1] = b.array[2]; 2902 r.ptr[2] = a.array[3]; 2903 r.ptr[3] = b.array[3]; 2904 return r; 2905 } 2906 } 2907 unittest 2908 { 2909 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f); 2910 __m128 B = _mm_setr_ps(5.0f, 6.0f, 7.0f, 8.0f); 2911 __m128 R = _mm_unpackhi_ps(A, B); 2912 float[4] correct = [3.0f, 7.0f, 4.0f, 8.0f]; 2913 assert(R.array == correct); 2914 } 2915 2916 /// Unpack and interleave single-precision (32-bit) floating-point elements from the low half of `a` and `b`. 2917 __m128 _mm_unpacklo_ps (__m128 a, __m128 b) pure @trusted 2918 { 2919 version(LDC) 2920 { 2921 // x86: plain version generates unpckhps with LDC 1.0.0 -O1, but shufflevector 8 less instructions in -O0 2922 return shufflevectorLDC!(__m128, 0, 4, 1, 5)(a, b); 2923 } 2924 else 2925 { 2926 __m128 r; 2927 r.ptr[0] = a.array[0]; 2928 r.ptr[1] = b.array[0]; 2929 r.ptr[2] = a.array[1]; 2930 r.ptr[3] = b.array[1]; 2931 return r; 2932 } 2933 } 2934 unittest 2935 { 2936 __m128 A = _mm_setr_ps(1.0f, 2.0f, 3.0f, 4.0f); 2937 __m128 B = _mm_setr_ps(5.0f, 6.0f, 7.0f, 8.0f); 2938 __m128 R = _mm_unpacklo_ps(A, B); 2939 float[4] correct = [1.0f, 5.0f, 2.0f, 6.0f]; 2940 assert(R.array == correct); 2941 } 2942 2943 /// Compute the bitwise XOR of packed single-precision (32-bit) floating-point elements in `a` and `b`. 2944 __m128 _mm_xor_ps (__m128 a, __m128 b) pure @safe 2945 { 2946 return cast(__m128)(cast(__m128i)a ^ cast(__m128i)b); 2947 } 2948 // TODO unittest and force inline 2949 2950 private 2951 { 2952 // Returns: `true` if the pointer is suitably aligned. 2953 bool isPointerAligned(void* p, size_t alignment) pure 2954 { 2955 assert(alignment != 0); 2956 return ( cast(size_t)p & (alignment - 1) ) == 0; 2957 } 2958 2959 // Returns: next pointer aligned with alignment bytes. 2960 void* nextAlignedPointer(void* start, size_t alignment) pure 2961 { 2962 return cast(void*)nextMultipleOf(cast(size_t)(start), alignment); 2963 } 2964 2965 // Returns number of bytes to actually allocate when asking 2966 // for a particular alignment 2967 @nogc size_t requestedSize(size_t askedSize, size_t alignment) pure 2968 { 2969 enum size_t pointerSize = size_t.sizeof; 2970 return askedSize + alignment - 1 + pointerSize * 3; 2971 } 2972 2973 // Store pointer given by malloc + size + alignment 2974 @nogc void* storeRawPointerPlusInfo(void* raw, size_t size, size_t alignment) pure 2975 { 2976 enum size_t pointerSize = size_t.sizeof; 2977 char* start = cast(char*)raw + pointerSize * 3; 2978 void* aligned = nextAlignedPointer(start, alignment); 2979 void** rawLocation = cast(void**)(cast(char*)aligned - pointerSize); 2980 *rawLocation = raw; 2981 size_t* sizeLocation = cast(size_t*)(cast(char*)aligned - 2 * pointerSize); 2982 *sizeLocation = size; 2983 size_t* alignmentLocation = cast(size_t*)(cast(char*)aligned - 3 * pointerSize); 2984 *alignmentLocation = alignment; 2985 assert( isPointerAligned(aligned, alignment) ); 2986 return aligned; 2987 } 2988 2989 // Returns: x, multiple of powerOfTwo, so that x >= n. 2990 @nogc size_t nextMultipleOf(size_t n, size_t powerOfTwo) pure nothrow 2991 { 2992 // check power-of-two 2993 assert( (powerOfTwo != 0) && ((powerOfTwo & (powerOfTwo - 1)) == 0)); 2994 2995 size_t mask = ~(powerOfTwo - 1); 2996 return (n + powerOfTwo - 1) & mask; 2997 } 2998 2999 void* alignedReallocImpl(bool PreserveDataIfResized)(void* aligned, size_t size, size_t alignment) 3000 { 3001 if (aligned is null) 3002 return _mm_malloc(size, alignment); 3003 3004 assert(alignment != 0); 3005 assert(isPointerAligned(aligned, alignment)); 3006 3007 size_t previousSize = *cast(size_t*)(cast(char*)aligned - size_t.sizeof * 2); 3008 size_t prevAlignment = *cast(size_t*)(cast(char*)aligned - size_t.sizeof * 3); 3009 3010 // It is illegal to change the alignment across calls. 3011 assert(prevAlignment == alignment); 3012 3013 void* raw = *cast(void**)(cast(char*)aligned - size_t.sizeof); 3014 size_t request = requestedSize(size, alignment); 3015 size_t previousRequest = requestedSize(previousSize, alignment); 3016 assert(previousRequest - request == previousSize - size); 3017 3018 // Heuristic: if a requested size is within 50% to 100% of what is already allocated 3019 // then exit with the same pointer 3020 // PERF it seems like `realloc` should do that, not us. 3021 if ( (previousRequest < request * 4) && (request <= previousRequest) ) 3022 return aligned; 3023 3024 void* newRaw = malloc(request); 3025 if (request > 0 && newRaw == null) // realloc(0) can validly return anything 3026 onOutOfMemoryError(); 3027 3028 void* newAligned = storeRawPointerPlusInfo(newRaw, size, alignment); 3029 3030 static if (PreserveDataIfResized) 3031 { 3032 size_t minSize = size < previousSize ? size : previousSize; 3033 memcpy(newAligned, aligned, minSize); 3034 } 3035 3036 // Free previous data 3037 _mm_free(aligned); 3038 assert(isPointerAligned(newAligned, alignment)); 3039 return newAligned; 3040 } 3041 } 3042 3043 unittest 3044 { 3045 assert(nextMultipleOf(0, 4) == 0); 3046 assert(nextMultipleOf(1, 4) == 4); 3047 assert(nextMultipleOf(2, 4) == 4); 3048 assert(nextMultipleOf(3, 4) == 4); 3049 assert(nextMultipleOf(4, 4) == 4); 3050 assert(nextMultipleOf(5, 4) == 8); 3051 3052 { 3053 void* p = _mm_malloc(23, 16); 3054 assert(p !is null); 3055 assert(((cast(size_t)p) & 0xf) == 0); 3056 _mm_free(p); 3057 } 3058 3059 void* nullAlloc = _mm_malloc(0, 32); 3060 assert(nullAlloc != null); 3061 _mm_free(nullAlloc); 3062 } 3063 3064 // For some reason, order of declaration is important for this one 3065 // so it is misplaced. 3066 // Note: is just another name for _mm_cvtss_si32 3067 alias _mm_cvt_ss2si = _mm_cvtss_si32;