Commit 5e632b09 authored by Michael R. Crusoe's avatar Michael R. Crusoe

avx512: naive implementation of fpclass

parent b71b58c2
...@@ -434,6 +434,91 @@ simde_math_fpclassify(double v) { ...@@ -434,6 +434,91 @@ simde_math_fpclassify(double v) {
#endif #endif
} }
#define SIMDE_MATH_FP_QNAN 0x01
#define SIMDE_MATH_FP_PZERO 0x02
#define SIMDE_MATH_FP_NZERO 0x04
#define SIMDE_MATH_FP_PINF 0x08
#define SIMDE_MATH_FP_NINF 0x10
#define SIMDE_MATH_FP_DENORMAL 0x20
#define SIMDE_MATH_FP_NEGATIVE 0x40
#define SIMDE_MATH_FP_SNAN 0x80
static HEDLEY_INLINE
int8_t
simde_math_fpclassf(float v, const int imm8) {
union {
float f;
uint32_t u;
} fu;
fu.f = v;
uint32_t bits = fu.u;
int8_t NegNum = (bits >> 31) & 1;
uint32_t const ExpMask = 0x3F800000; // [30:23]
uint32_t const MantMask = 0x007FFFFF; // [22:0]
int8_t ExpAllOnes = ((bits & ExpMask) == ExpMask);
int8_t ExpAllZeros = ((bits & ExpMask) == 0);
int8_t MantAllZeros = ((bits & MantMask) == 0);
int8_t ZeroNumber = ExpAllZeros && MantAllZeros;
int8_t SignalingBit = (bits >> 22) & 1;
int8_t result = 0;
int8_t qNaN_res = ExpAllOnes && !MantAllZeros && SignalingBit;
int8_t Pzero_res = !NegNum && ExpAllZeros && MantAllZeros;
int8_t Nzero_res = NegNum && ExpAllZeros && MantAllZeros;
int8_t Pinf_res = !NegNum && ExpAllOnes && MantAllZeros;
int8_t Ninf_res = NegNum && ExpAllOnes && MantAllZeros;
int8_t Denorm_res = ExpAllZeros && !MantAllZeros;
int8_t FinNeg_res = NegNum && !ExpAllOnes && !ZeroNumber;
int8_t sNaN_res = ExpAllOnes && !MantAllZeros && !SignalingBit;
result = ((((imm8 >> 0) & 1) && qNaN_res) || \
(((imm8 >> 1) & 1) && Pzero_res) || \
(((imm8 >> 2) & 1) && Nzero_res) || \
(((imm8 >> 3) & 1) && Pinf_res) || \
(((imm8 >> 4) & 1) && Ninf_res) || \
(((imm8 >> 5) & 1) && Denorm_res) || \
(((imm8 >> 6) & 1) && FinNeg_res) || \
(((imm8 >> 7) & 1) && sNaN_res));
return result;
}
static HEDLEY_INLINE
int8_t
simde_math_fpclass(double v, const int imm8) {
union {
double d;
uint64_t u;
} du;
du.d = v;
uint64_t bits = du.u;
int8_t NegNum = (bits >> 63) & 1;
uint64_t const ExpMask = 0x3FF0000000000000; // [62:52]
uint64_t const MantMask = 0x000FFFFFFFFFFFFF; // [51:0]
int8_t ExpAllOnes = ((bits & ExpMask) == ExpMask);
int8_t ExpAllZeros = ((bits & ExpMask) == 0);
int8_t MantAllZeros = ((bits & MantMask) == 0);
int8_t ZeroNumber = ExpAllZeros && MantAllZeros;
int8_t SignalingBit = (bits >> 51) & 1;
int8_t result = 0;
int8_t qNaN_res = ExpAllOnes && !MantAllZeros && SignalingBit;
int8_t Pzero_res = !NegNum && ExpAllZeros && MantAllZeros;
int8_t Nzero_res = NegNum && ExpAllZeros && MantAllZeros;
int8_t Pinf_res = !NegNum && ExpAllOnes && MantAllZeros;
int8_t Ninf_res = NegNum && ExpAllOnes && MantAllZeros;
int8_t Denorm_res = ExpAllZeros && !MantAllZeros;
int8_t FinNeg_res = NegNum && !ExpAllOnes && !ZeroNumber;
int8_t sNaN_res = ExpAllOnes && !MantAllZeros && !SignalingBit;
result = ((((imm8 >> 0) & 1) && qNaN_res) || \
(((imm8 >> 1) & 1) && Pzero_res) || \
(((imm8 >> 2) & 1) && Nzero_res) || \
(((imm8 >> 3) & 1) && Pinf_res) || \
(((imm8 >> 4) & 1) && Ninf_res) || \
(((imm8 >> 5) & 1) && Denorm_res) || \
(((imm8 >> 6) & 1) && FinNeg_res) || \
(((imm8 >> 7) & 1) && sNaN_res));
return result;
}
/*** Manipulation functions ***/ /*** Manipulation functions ***/
#if !defined(simde_math_nextafter) #if !defined(simde_math_nextafter)
......
...@@ -71,6 +71,7 @@ ...@@ -71,6 +71,7 @@
#include "avx512/fmsub.h" #include "avx512/fmsub.h"
#include "avx512/fnmadd.h" #include "avx512/fnmadd.h"
#include "avx512/fnmsub.h" #include "avx512/fnmsub.h"
#include "avx512/fpclass.h"
#include "avx512/insert.h" #include "avx512/insert.h"
#include "avx512/kshift.h" #include "avx512/kshift.h"
#include "avx512/knot.h" #include "avx512/knot.h"
......
/* SPDX-License-Identifier: MIT
*
* 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.
*
* Copyright:
* 2023 Michael R. Crusoe <crusoe@debian.org>
*/
#if !defined(SIMDE_X86_AVX512_FPCLASS_H)
#define SIMDE_X86_AVX512_FPCLASS_H
#include "types.h"
HEDLEY_DIAGNOSTIC_PUSH
SIMDE_DISABLE_UNWANTED_DIAGNOSTICS
SIMDE_BEGIN_DECLS_
SIMDE_FUNCTION_ATTRIBUTES
simde__mmask8
simde_mm256_fpclass_ps_mask(simde__m256 a, int imm8)
SIMDE_REQUIRE_CONSTANT_RANGE(imm8, 0, 0x88) {
simde__mmask8 r = 0;
simde__m256_private a_ = simde__m256_to_private(a);
for (size_t i = 0 ; i < (sizeof(a_.f32) / sizeof(a_.f32[0])) ; i++) {
r |= simde_math_fpclassf(a_.f32[i], imm8) ? (UINT8_C(1) << i) : 0;
}
return r;
}
#if defined(SIMDE_X86_AVX512DQ_NATIVE) && defined(SIMDE_X86_AVX512VL_NATIVE)
# define simde_mm256_fpclass_ps_mask(a, imm8) _mm256_fpclass_ps_mask((a), (imm8))
#endif
#if defined(SIMDE_X86_AVX512DQ_ENABLE_NATIVE_ALIASES) && defined(SIMDE_X86_AVX512VL_ENABLE_NATIVE_ALIASES)
# undef _mm256_fpclass_ps_mask
# define _mm256_fpclass_ps_mask(a, imm8) simde_mm256_fpclass_ps_mask((a), (imm8))
#endif
SIMDE_FUNCTION_ATTRIBUTES
simde__mmask8
simde_mm512_fpclass_pd_mask(simde__m512d a, int imm8)
SIMDE_REQUIRE_CONSTANT_RANGE(imm8, 0, 0x88) {
simde__mmask8 r = 0;
simde__m512d_private a_ = simde__m512d_to_private(a);
for (size_t i = 0 ; i < (sizeof(a_.f64) / sizeof(a_.f64[0])) ; i++) {
r |= simde_math_fpclass(a_.f64[i], imm8) ? (UINT8_C(1) << i) : 0;
}
return r;
}
#if defined(SIMDE_X86_AVX512DQ_NATIVE)
# define simde_mm512_fpclass_pd_mask(a, imm8) _mm512_fpclass_pd_mask((a), (imm8))
#endif
#if defined(SIMDE_X86_AVX512DQ_ENABLE_NATIVE_ALIASES)
# undef _mm512_fpclass_pd_mask
# define _mm512_fpclass_pd_mask(a, imm8) simde_mm512_fpclass_pd_mask((a), (imm8))
#endif
SIMDE_END_DECLS_
HEDLEY_DIAGNOSTIC_POP
#endif /* !defined(SIMDE_X86_AVX512_FPCLASS_H) */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment