Skip to content

Commit

Permalink
Optional bugfix for .rodata pooling bug, more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuyler36 committed Nov 10, 2024
1 parent 0696190 commit 9fc8ce8
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion include/MSL_C/w_math.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
#ifndef W_MATH_H
#define W_MATH_H

extern inline float sqrtf(float x) {
#ifndef BUGFIXES
#define SQRTF_LINKAGE extern
#else
// making the function static instead of extern resolves the bug
#define SQRTF_LINKAGE static
#endif

/**
* Float square root implementation.
*
* NOTE: this function causes a bug in the Metrowerks C Compiler from GC MW 1.3.X
* to be exhibited. Weak extern inlined functions that contain an initialized
* static variable turn off data pooling for the translation unit which they are
* included in. As w_math.h seems to be included in a top-level header that
* the AC devs used, every translation unit in foresta.rel includes sqrtf.
* The effect is that the .rodata section has pooling entirely disabled.
*/
SQRTF_LINKAGE inline float sqrtf(float x) {
static const double _half = .5;
static const double _three = 3.0;
volatile float y;

if (x > 0.0f) {
double guess = __frsqrte((double)x); // returns an approximation to
guess = _half * guess * (_three - guess * guess * x); // now have 12 sig bits
Expand All @@ -13,9 +31,14 @@ extern inline float sqrtf(float x) {
y = (float)(x * guess);
return y;
}

return x;
}

#ifdef SQRTF_LINKAGE
#undef SQRTF_LINKAGE
#endif

extern inline double fabs(double x) {
return __fabs(x);
}
Expand Down

0 comments on commit 9fc8ce8

Please sign in to comment.