2025-10-22 21:17:53 +00:00
|
|
|
#include <stdint.h>
|
2025-11-02 17:36:56 +00:00
|
|
|
#include "stencil_helper.h"
|
2025-10-22 21:17:53 +00:00
|
|
|
|
|
|
|
|
//double (*math_pow)(double, double);
|
|
|
|
|
|
2025-10-31 15:59:46 +00:00
|
|
|
volatile extern int dummy_int;
|
|
|
|
|
volatile extern float dummy_float;
|
2025-10-22 21:17:53 +00:00
|
|
|
|
2025-11-02 17:36:56 +00:00
|
|
|
NOINLINE int floor_div(float arg1, float arg2) {
|
2025-10-22 21:17:53 +00:00
|
|
|
float x = arg1 / arg2;
|
|
|
|
|
int i = (int)x;
|
|
|
|
|
if (x < 0 && x != (float)i) i -= 1;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-02 17:36:56 +00:00
|
|
|
NOINLINE float aux_sqrt(float x) {
|
2025-11-01 20:51:29 +00:00
|
|
|
if (x <= 0.0f) return 0.0f;
|
2025-10-25 00:21:43 +00:00
|
|
|
|
2025-11-01 20:51:29 +00:00
|
|
|
// --- Improved initial guess using bit-level trick ---
|
|
|
|
|
union { float f; uint32_t i; } conv = { x };
|
|
|
|
|
conv.i = (conv.i >> 1) + 0x1fc00000; // better bias constant
|
|
|
|
|
float y = conv.f;
|
2025-10-25 00:21:43 +00:00
|
|
|
|
2025-11-01 20:51:29 +00:00
|
|
|
// --- Fixed number of Newton-Raphson iterations ---
|
|
|
|
|
y = 0.5f * (y + x / y);
|
|
|
|
|
y = 0.5f * (y + x / y);
|
|
|
|
|
y = 0.5f * (y + x / y); // 3 fixed iterations
|
2025-10-25 00:21:43 +00:00
|
|
|
|
2025-11-01 20:51:29 +00:00
|
|
|
return y;
|
2025-10-25 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-02 17:36:56 +00:00
|
|
|
NOINLINE float aux_get_42(float n) {
|
2025-10-26 15:09:02 +00:00
|
|
|
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
|
|
|
}
|