2025-10-22 21:17:53 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
//double (*math_pow)(double, double);
|
|
|
|
|
|
|
|
|
|
volatile int dummy_int = 1337;
|
|
|
|
|
volatile float dummy_float = 1337;
|
|
|
|
|
|
|
|
|
|
__attribute__((noinline)) int floor_div(float arg1, float arg2) {
|
|
|
|
|
float x = arg1 / arg2;
|
|
|
|
|
int i = (int)x;
|
|
|
|
|
if (x < 0 && x != (float)i) i -= 1;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-26 15:08:33 +00:00
|
|
|
__attribute__((noinline)) float sqrt(float n) {
|
2025-10-25 00:21:43 +00:00
|
|
|
if (n < 0) return -1;
|
|
|
|
|
|
|
|
|
|
float x = n; // initial guess
|
|
|
|
|
float epsilon = 0.00001; // desired accuracy
|
|
|
|
|
|
|
|
|
|
while ((x - n / x) > epsilon || (x - n / x) < -epsilon) {
|
|
|
|
|
x = 0.5 * (x + n / x);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-26 15:08:33 +00:00
|
|
|
__attribute__((noinline)) float sqrt2(float n) {
|
|
|
|
|
return n * 20.5 + 4.5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__attribute__((noinline)) float get_42(float n) {
|
|
|
|
|
return n * + 42.0;
|
2025-10-26 13:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 21:17:53 +00:00
|
|
|
float fast_pow_float(float base, float exponent) {
|
|
|
|
|
union {
|
|
|
|
|
float f;
|
|
|
|
|
uint32_t i;
|
|
|
|
|
} u;
|
|
|
|
|
|
|
|
|
|
u.f = base;
|
|
|
|
|
int32_t x = u.i;
|
|
|
|
|
int32_t y = (int32_t)(exponent * (x - 1072632447) + 1072632447);
|
|
|
|
|
u.i = (uint32_t)y;
|
|
|
|
|
return u.f;
|
2025-10-26 13:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
// Test aux functions
|
|
|
|
|
float a = 16.0f;
|
|
|
|
|
float sqrt_a = fast_sqrt(a);
|
|
|
|
|
float pow_a = fast_pow_float(a, 0.5f);
|
|
|
|
|
float sqrt2_a = fast_sqrt2(a);
|
|
|
|
|
return 0;
|
2025-10-22 21:17:53 +00:00
|
|
|
}
|