Skip to content

Commit e6ed9f6

Browse files
committed
Fix MSVC half float support
Also added f16x4 test
1 parent dbc58f5 commit e6ed9f6

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

include/math/simd/vector/half.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#pragma once
2121
#include "math/simd/vector/float.hpp"
2222

23+
#if defined(MATH_SIMD_SUPPORT_AVX2)
2324
namespace math
2425
{
2526

@@ -45,7 +46,7 @@ struct [[nodiscard]] alignas(8) f16x4
4546
*/
4647
f16x4() noexcept
4748
{
48-
#if defined(__F16C__)
49+
#if defined(MATH_SIMD_SUPPORT_AVX2)
4950
data = 0;
5051
#elif defined(MATH_SIMD_SUPPORT_NEON)
5152
data = vdup_n_f16(0.0f);
@@ -59,7 +60,7 @@ struct [[nodiscard]] alignas(8) f16x4
5960
*/
6061
explicit f16x4(half xyzw) noexcept
6162
{
62-
#if defined(__F16C__)
63+
#if defined(MATH_SIMD_SUPPORT_AVX2)
6364
_mm_storel_epi64((__m128i*)&data, _mm_cvtps_ph(_mm_set1_ps(xyzw), _MM_FROUND_TO_NEAREST_INT));
6465
#elif defined(MATH_SIMD_SUPPORT_NEON)
6566
data = vdup_n_f16(xyzw);
@@ -77,7 +78,7 @@ struct [[nodiscard]] alignas(8) f16x4
7778
*/
7879
f16x4(half x, half y, half z, half w) noexcept
7980
{
80-
#if defined(__F16C__)
81+
#if defined(MATH_SIMD_SUPPORT_AVX2)
8182
_mm_storel_epi64((__m128i*)&data, _mm_cvtps_ph(_mm_set_ps(w, z, y, x), _MM_FROUND_TO_NEAREST_INT));
8283
#elif defined(MATH_SIMD_SUPPORT_NEON)
8384
data = (float16x4_t){ x, y, z, w };
@@ -95,7 +96,7 @@ struct [[nodiscard]] alignas(8) f16x4
9596
*/
9697
f16x4(half x, half y, half z) noexcept
9798
{
98-
#if defined(__F16C__)
99+
#if defined(MATH_SIMD_SUPPORT_AVX2)
99100
_mm_storel_epi64((__m128i*)&data, _mm_cvtps_ph(_mm_set_ps(z, z, y, x), _MM_FROUND_TO_NEAREST_INT));
100101
#elif defined(MATH_SIMD_SUPPORT_NEON)
101102
data = (float16x4_t){ x, y, z, z };
@@ -118,7 +119,7 @@ struct [[nodiscard]] alignas(8) f16x4
118119
*/
119120
explicit f16x4(f32x4 v) noexcept
120121
{
121-
#if defined(__F16C__)
122+
#if defined(MATH_SIMD_SUPPORT_AVX2)
122123
_mm_storel_epi64((__m128i*)&data, _mm_cvtps_ph(v.data, _MM_FROUND_TO_NEAREST_INT));
123124
#elif defined(MATH_SIMD_SUPPORT_NEON)
124125
data = vcvt_f16_f32(v.data);
@@ -188,7 +189,7 @@ struct [[nodiscard]] alignas(8) f16x4
188189
*/
189190
explicit operator f32x4() const noexcept
190191
{
191-
#if defined(__F16C__)
192+
#if defined(MATH_SIMD_SUPPORT_AVX2)
192193
return _mm_cvtph_ps(_mm_loadl_epi64((__m128i*)&data));
193194
#elif defined(MATH_SIMD_SUPPORT_NEON)
194195
return vcvt_f32_f16(data);
@@ -200,4 +201,5 @@ struct [[nodiscard]] alignas(8) f16x4
200201
// TODO: math functions after adding AVX512 support.
201202
};
202203

203-
} // namespace math
204+
} // namespace math
205+
#endif // MATH_SIMD_SUPPORT_AVX2

include/math/types.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
#pragma once
2121

2222
#define __STDC_WANT_IEC_60559_TYPES_EXT__
23+
#include <cfloat>
2324
#include <cstdint>
2425
#include <cstddef>
2526

27+
#ifndef FLT16_MIN
28+
#define _Float16 uint16_t // Note: MSVC fix
29+
#endif
30+
2631
namespace math
2732
{
2833

tests/test-vector.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ static void testFloatVectors()
164164
cmp(simdSin, sin(vecRight));
165165
cmp(simdCos, cos(vecRight));
166166
}
167+
cmp((f32x4)(f16x4)simdLeft, vecLeft, 1.0e-3f);
168+
cmp((f32x4)(f16x4)simdRight, vecRight, 1.0e-3f);
167169
}
168170
static void testIntVectors()
169171
{

0 commit comments

Comments
 (0)