Helper Reference Manual 1.7.1
Loading...
Searching...
No Matches
stress.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2024 Nokia
3 */
4
11#ifndef ODPH_STRESS_H_
12#define ODPH_STRESS_H_
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
36static inline uint32_t odph_stress_pow2_u32(uint32_t value)
37{
38 uint64_t v = (uint64_t)value;
39 uint64_t res = v * v;
40
41 if (odp_unlikely(res > UINT32_MAX))
42 return UINT32_MAX;
43
44 return (uint32_t)res;
45}
46
54static inline uint32_t odph_stress_log2_u32(uint32_t value)
55{
56 uint32_t ret = 0;
57
58 while ((value >>= 1) != 0)
59 ret++;
60
61 return ret;
62}
63
71static inline uint32_t odph_stress_sqrt_u32(uint32_t value)
72{
73 uint64_t x;
74 uint64_t pow = 1;
75
76 if (odp_unlikely(value == 0 || value == 1))
77 return value;
78
79 if (value & 0xffff0000) {
80 if (value & 0xff000000) {
81 if (value & 0xf0000000) {
82 x = 16384;
83 if (value & 0xe0000000)
84 x = 23170;
85 if (value & 0xc0000000)
86 x = 32768;
87 if (value & 0x80000000)
88 x = 46340;
89 } else {
90 /* value & 0x0f000000 */
91 x = 4096;
92 if (value & 0x0e000000)
93 x = 5792;
94 if (value & 0x0c000000)
95 x = 8192;
96 if (value & 0x08000000)
97 x = 11585;
98 }
99 } else {
100 if (value & 0x00f00000) {
101 x = 1024;
102 if (value & 0x00e00000)
103 x = 1448;
104 if (value & 0x00c00000)
105 x = 2048;
106 if (value & 0x00800000)
107 x = 2896;
108 } else {
109 /* value & 0x000f0000 */
110 x = 256;
111 if (value & 0x000e0000)
112 x = 362;
113 if (value & 0x000c0000)
114 x = 512;
115 if (value & 0x00080000)
116 x = 724;
117 }
118 }
119 } else {
120 /* value & 0xffff */
121 x = 1;
122
123 if (value >= 16384) {
124 x = 128;
125 if (value >= 25600)
126 x = 160;
127 if (value >= 36864)
128 x = 192;
129 if (value >= 50176)
130 x = 224;
131 } else {
132 if (value >= 1024)
133 x = 32;
134 if (value >= 4096)
135 x = 64;
136 if (value >= 9216)
137 x = 96;
138 }
139 }
140
141 while (pow <= value) {
142 x++;
143 pow = x * x;
144 }
145
146 return (uint32_t)(x - 1);
147}
148
156static inline float odph_stress_sqrt_f32(float value)
157{
158 double x;
159 double pow = 1;
160
161 if (odp_unlikely(value < 1.0))
162 return 0;
163
164 if (value >= 65536) {
165 if (value >= 16777215) {
166 if (value >= 268435456) {
167 x = 16384;
168 if (value >= 536870912)
169 x = 23170;
170 if (value >= 1073741824)
171 x = 32768;
172 if (value >= 2147483648)
173 x = 46340;
174 } else {
175 x = 4096;
176 if (value >= 33554432)
177 x = 5792;
178 if (value >= 67108864)
179 x = 8192;
180 if (value >= 134217728)
181 x = 11585;
182 }
183 } else {
184 if (value >= 1048576) {
185 x = 1024;
186 if (value >= 2097152)
187 x = 1448;
188 if (value >= 4194304)
189 x = 2048;
190 if (value >= 8388608)
191 x = 2896;
192 } else {
193 x = 256;
194 if (value >= 131072)
195 x = 362;
196 if (value >= 262144)
197 x = 512;
198 if (value >= 524288)
199 x = 724;
200 }
201 }
202 } else {
203 x = 1;
204
205 if (value >= 16384) {
206 x = 128;
207 if (value >= 25600)
208 x = 160;
209 if (value >= 36864)
210 x = 192;
211 if (value >= 50176)
212 x = 224;
213 } else {
214 if (value >= 1024)
215 x = 32;
216 if (value >= 4096)
217 x = 64;
218 if (value >= 9216)
219 x = 96;
220 }
221 }
222
223 while (pow <= value) {
224 x = x + 1;
225 pow = x * x;
226 }
227
228 return (float)(x - 1);
229}
230
234#ifdef __cplusplus
235}
236#endif
237
238#endif
static uint32_t odph_stress_log2_u32(uint32_t value)
Returns base 2 logarithm of 'value'.
Definition stress.h:54
static float odph_stress_sqrt_f32(float value)
Calculates square root of a floating point value, rounded down to the nearest integer.
Definition stress.h:156
static uint32_t odph_stress_pow2_u32(uint32_t value)
Returns 'value' raised to the power of 2.
Definition stress.h:36
static uint32_t odph_stress_sqrt_u32(uint32_t value)
Calculates square root of a 32-bit unsigned integer value.
Definition stress.h:71