Peano
Loading...
Searching...
No Matches
CompressedFloat.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <concepts>
5
6// https://stackoverflow.com/a/3221914/5769882
7#define STRCAT_INNER(a, b) a##b
8#define STRCAT(a, b) STRCAT_INNER(a, b)
9
10template<typename X>
11concept NUM = std::integral<X> || std::floating_point<X>;
12
13namespace std {
14 template<typename CF>
15 CF abs(const CF &cf) {
16 auto i = cf.__get() > 0;
17 if (i) return cf;
18 else return -cf;
19 }
20}
21
22namespace tarch {
23namespace la {
24
25 template<typename T, unsigned char BITS = sizeof(T) * 8, unsigned char ALIGNMENT = alignof(T)> requires std::is_floating_point_v<T>
26 class alignas(ALIGNMENT) CompressedFloat {
27 __attribute__((packed)) std::uint64_t data: BITS;
28
29 T __get() const {
30 union {
31 std::uint64_t byte_data;
32 T float_data;
33 } U;
34
35 static_assert(sizeof(T) * 8 >= BITS);
36
37 auto dataLocal = this->data;
38
39 U.byte_data = dataLocal << (sizeof(T) * 8 - BITS);
40 auto float_data = U.float_data;
41 return float_data;
42 }
43
44 void __set(T float_data) {
45 union {
46 std::uint64_t byte_data;
47 T float_data;
48 } U;
49
50 U.float_data = float_data;
51 this->data = U.byte_data >> (sizeof(T) * 8 - BITS);
52 }
53
54 public:
55 CompressedFloat() = default;
56
57 CompressedFloat(const CompressedFloat &copy) = default;
58
59 CompressedFloat(const T &value) {
60 this->__set(value);
61 }
62
63 CompressedFloat &operator=(const CompressedFloat &other) = default;
64
65 template<NUM X>
66 CompressedFloat &operator=(const X &value) {
67 this->__set((T) value);
68 return *this;
69 }
70
71#define UNARY_PREFIX_OP(op) \
72 CompressedFloat &operator op() { \
73 auto f = this->__get(); \
74 f op; \
75 this->__set(f); \
76 return *this; \
77 } \
78
81
82#define UNARY_POSTFIX_OP(op) \
83 const CompressedFloat operator op(std::int32_t) { \
84 auto oldThis = CompressedFloat(*this); \
85 this->operator op(); \
86 return oldThis; \
87 } \
88
91
92 T operator -() const {
93 return -this->__get();
94 }
95
96#undef UNARY_POSTFIX_OP
97
98#define ARITHMETIC_OP(op) \
99 template<NUM X> \
100 CompressedFloat &operator STRCAT(op, =) (const X &scalar) { \
101 this->__set(this->__get() op scalar); \
102 return *this; \
103 } \
104 \
105 template<NUM X> \
106 T operator op (const X &scalar) const { \
107 return this->__get() op scalar; \
108 } \
109 \
110 template<NUM X> \
111 friend X& operator STRCAT(op, =) (X &scalar, const CompressedFloat cf) { \
112 scalar STRCAT(op, =) cf.__get(); \
113 return scalar; \
114 } \
115 \
116 template<NUM X> \
117 friend X operator op (const X &scalar, const CompressedFloat cf) { \
118 return scalar op cf.__get(); \
119 } \
120
125
126#undef ARITHMETIC_OP
127
128#define SELF_ARITHMETIC_OP(op) \
129 CompressedFloat &operator STRCAT(op, =) (const CompressedFloat &scalar) { \
130 this->__set(this->__get() op scalar.__get()); \
131 return *this; \
132 } \
133 \
134 T operator op (const CompressedFloat &scalar) const { \
135 return this->__get() op scalar; \
136 } \
137
142
143#undef SELF_ARITHMETIC_OP
144
145 bool operator !() const {
146 return !this->data;
147 }
148
149#define COMPARISON_OP(op) \
150 template<NUM X> \
151 bool operator op (const X &scalar) const { \
152 return this->__get() op scalar; \
153 } \
154 \
155 template<NUM X> \
156 friend bool operator op (const X &scalar, const CompressedFloat &cf) { \
157 return scalar op cf.__get(); \
158 } \
159
161 COMPARISON_OP(<=)
162 COMPARISON_OP(==)
163 COMPARISON_OP(!=)
164 COMPARISON_OP(>=)
166
167 #undef COMPARISON_OP
168
169#define SELF_COMPARISON_OP(op) \
170 bool operator op (const CompressedFloat &cf) const { \
171 return this->__get() op cf.__get(); \
172 } \
173
180
181#undef SELF_COMPARISON_OP
182
183 template<NUM X>
184 operator X() const {
185 return (X) this->__get();
186 }
187
189
190 };
191
192}
193}
194
195#undef STRCAT_INNER
196#undef STRCAT
#define SELF_ARITHMETIC_OP(op)
#define UNARY_PREFIX_OP(op)
#define ARITHMETIC_OP(op)
#define COMPARISON_OP(op)
#define UNARY_POSTFIX_OP(op)
#define SELF_COMPARISON_OP(op)
int __attribute__((optimize("O0"))) toolbox
CompressedFloat & operator=(const X &value)
CompressedFloat(const CompressedFloat &copy)=default
CompressedFloat & operator=(const CompressedFloat &other)=default
CompressedFloat(const T &value)
__attribute__((packed)) std T __get() const
STL namespace.
CF abs(const CF &cf)
Have to include this header, as I need access to the SYCL_EXTERNAL keyword.
Definition accelerator.h:19