Last active
July 11, 2022 01:21
-
-
Save jeaiii/3d5be5ff0b9baa5348a56acca4cc19d1 to your computer and use it in GitHub Desktop.
sse2 mm_floor_ps
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
/* | |
MIT License | |
Copyright(c) 2022 James Edward Anhalt III - https://github.com/jeaiii | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
#include <xmmintrin.h> | |
// truncate float to int | |
// go back to float -> 'f' | |
// compare original 'n' to 'f' | |
// if 'n' < 'f' then we need to subtract one | |
// mm_cmplt_ps creates a mask of 0xffffffff (true) and 0x00000000 (false) in each lane | |
// this is -1 as a 32bit integer | |
// covert the integer -1s and 0s to floats | |
// and add it to 'f' (so we subtract 1 or do nothing) | |
// https://godbolt.org/z/YzjPqPda1 | |
__m128 mm_floor_ps(__m128 n) | |
{ | |
auto f = _mm_cvtepi32_ps(_mm_cvttps_epi32(n)); | |
return _mm_add_ps(f, _mm_cvtepi32_ps(_mm_castps_si128(_mm_cmplt_ps(n, f)))); | |
} | |
float sse2_floor(float n) | |
{ | |
return _mm_cvtss_f32(mm_floor_ps(_mm_set_ss(n))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment