Created
January 15, 2020 04:58
-
-
Save Yepoleb/0fdd4836f291f535c3b93cdaf6cf0c2b to your computer and use it in GitHub Desktop.
Check if two rectangles intersect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <xmmintrin.h> | |
#include <stdint.h> | |
struct alignas(16) Rect | |
{ | |
float left; | |
float right; | |
float top; | |
float bottom; | |
}; | |
bool intersect1(Rect a, Rect b) | |
{ | |
return | |
(a.left <= b.right) and | |
(a.top <= b.bottom) and | |
(b.left <= a.right) and | |
(b.top <= a.bottom) | |
; | |
} | |
bool intersect2(Rect a, Rect b) | |
{ | |
__m128 am = _mm_load_ps((float*)&a); | |
__m128 bm = _mm_load_ps((float*)&b); | |
__m128 c1 = _mm_shuffle_ps(am, bm, 0xDD); // [3, 1, 3, 1] | |
__m128 c2 = _mm_shuffle_ps(bm, am, 0x88); // [2, 0, 2, 0] | |
__m128 cmp = _mm_cmpgt_ps(c1, c2); | |
alignas(16) uint64_t cmpres[2]; | |
_mm_store_si128((__m128i*)&cmpres, _mm_castps_si128(cmp)); | |
return !(cmpres[0] | cmpres[1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment