Argon 0.1.0
Loading...
Searching...
No Matches
float_fixups.hpp
1#pragma once
2#include <array>
3#include <cstdint>
4#include "argon/features.h"
5
6#ifndef __ARM_FEATURE_MVE
7#ifdef __ARM_NEON
8#include <arm_neon.h>
9#else
11#endif
12
24namespace argon::helpers {
25
26#if defined(__arm__) && !defined(__aarch64__) && defined(__clang__)
27#define ARGON_CLANG_AARCH32_CONVERT true
28#else
29#define ARGON_CLANG_AARCH32_CONVERT false
30#endif
31
32[[gnu::always_inline]] inline uint32_t saturate_to_uint32(float x) {
33 if (!(x > 0.0f))
34 return 0; // also NaN
35 if (x >= 4294967296.0f)
36 return UINT32_MAX;
37 return static_cast<uint32_t>(x);
38}
39
40[[gnu::always_inline]] inline int32x4_t convert_to_int32(float32x4_t a) {
41#if ARGON_CLANG_AARCH32_CONVERT
42 int32x4_t r;
43 asm("vcvt.s32.f32 %q0, %q1" : "=w"(r) : "w"(a));
44 return r;
45#else
46 return vcvtq_s32_f32(a);
47#endif
48}
49
50[[gnu::always_inline]] inline int32x2_t convert_to_int32(float32x2_t a) {
51#if ARGON_CLANG_AARCH32_CONVERT
52 int32x2_t r;
53 asm("vcvt.s32.f32 %P0, %P1" : "=w"(r) : "w"(a));
54 return r;
55#else
56 return vcvt_s32_f32(a);
57#endif
58}
59
60[[gnu::always_inline]] inline uint32x4_t convert_to_uint32(float32x4_t a) {
61#if ARGON_CLANG_AARCH32_CONVERT
62 uint32x4_t r;
63 asm("vcvt.u32.f32 %q0, %q1" : "=w"(r) : "w"(a));
64 return r;
65#elif ARGON_PLATFORM_SIMDE
66 std::array<float, 4> in;
67 vst1q_f32(in.data(), a);
68 const std::array<uint32_t, 4> out{saturate_to_uint32(in[0]), saturate_to_uint32(in[1]), saturate_to_uint32(in[2]),
69 saturate_to_uint32(in[3])};
70 return vld1q_u32(out.data());
71#else
72 return vcvtq_u32_f32(a);
73#endif
74}
75
76[[gnu::always_inline]] inline uint32x2_t convert_to_uint32(float32x2_t a) {
77#if ARGON_CLANG_AARCH32_CONVERT
78 uint32x2_t r;
79 asm("vcvt.u32.f32 %P0, %P1" : "=w"(r) : "w"(a));
80 return r;
81#elif ARGON_PLATFORM_SIMDE
82 std::array<float, 2> in;
83 vst1_f32(in.data(), a);
84 const std::array<uint32_t, 2> out{saturate_to_uint32(in[0]), saturate_to_uint32(in[1])};
85 return vld1_u32(out.data());
86#else
87 return vcvt_u32_f32(a);
88#endif
89}
90
91// SIMDe's fixed-point converts saturate only above 2^31 (2^32 unsigned) and cast a lane of exactly that out of range.
92// Scaling by 2^fracbits is exact in float (an overflow goes to ±inf, which saturates the same), so on SIMDe the plain
93// converts above give vcvt #fracbits's result: truncate, saturate, NaN -> 0.
94
95template <int fracbits>
96[[gnu::always_inline]] inline int32x4_t convert_n_to_int32(float32x4_t a) {
97#if ARGON_PLATFORM_SIMDE
98 return convert_to_int32(vmulq_n_f32(a, static_cast<float>(1ULL << fracbits)));
99#else
100 return vcvtq_n_s32_f32(a, fracbits);
101#endif
102}
103
104template <int fracbits>
105[[gnu::always_inline]] inline int32x2_t convert_n_to_int32(float32x2_t a) {
106#if ARGON_PLATFORM_SIMDE
107 return convert_to_int32(vmul_n_f32(a, static_cast<float>(1ULL << fracbits)));
108#else
109 return vcvt_n_s32_f32(a, fracbits);
110#endif
111}
112
113template <int fracbits>
114[[gnu::always_inline]] inline uint32x4_t convert_n_to_uint32(float32x4_t a) {
115#if ARGON_PLATFORM_SIMDE
116 return convert_to_uint32(vmulq_n_f32(a, static_cast<float>(1ULL << fracbits)));
117#else
118 return vcvtq_n_u32_f32(a, fracbits);
119#endif
120}
121
122template <int fracbits>
123[[gnu::always_inline]] inline uint32x2_t convert_n_to_uint32(float32x2_t a) {
124#if ARGON_PLATFORM_SIMDE
125 return convert_to_uint32(vmul_n_f32(a, static_cast<float>(1ULL << fracbits)));
126#else
127 return vcvt_n_u32_f32(a, fracbits);
128#endif
129}
130
131#undef ARGON_CLANG_AARCH32_CONVERT
132
133#if ARGON_GCC_AARCH32_FLOAT
134[[gnu::always_inline]] inline uint32x4_t greater_than(float32x4_t a, float32x4_t b) {
135 uint32x4_t r;
136 asm("vcgt.f32 %q0, %q1, %q2" : "=w"(r) : "w"(a), "w"(b));
137 return r;
138}
139[[gnu::always_inline]] inline uint32x2_t greater_than(float32x2_t a, float32x2_t b) {
140 uint32x2_t r;
141 asm("vcgt.f32 %P0, %P1, %P2" : "=w"(r) : "w"(a), "w"(b));
142 return r;
143}
144[[gnu::always_inline]] inline uint32x4_t greater_than_or_equal(float32x4_t a, float32x4_t b) {
145 uint32x4_t r;
146 asm("vcge.f32 %q0, %q1, %q2" : "=w"(r) : "w"(a), "w"(b));
147 return r;
148}
149[[gnu::always_inline]] inline uint32x2_t greater_than_or_equal(float32x2_t a, float32x2_t b) {
150 uint32x2_t r;
151 asm("vcge.f32 %P0, %P1, %P2" : "=w"(r) : "w"(a), "w"(b));
152 return r;
153}
154#endif
155
156} // namespace argon::helpers
157#endif
Header file for SIMD features and platform detection.
Contains helper templates and concepts for type manipulation and compile-time utilities.
Definition argon.hpp:156
SIMDe's NEON implementation (x86 and other non-Arm hosts) under the NEON intrinsic names.