Skip to content

Commit

Permalink
AK: Add very naive implementation of {sin,cos,tan} for aarch64
Browse files Browse the repository at this point in the history
The {sin,cos,tan} functions in AK are used as the implementation of the
same function in libm. We cannot use the __builtin_foo functions as
these would just call the libc functions. This was causing an infinite
loop. Fix this by adding a very naive implementation of
AK::{sin, cos,tan}, that is only valid for small inputs. For the other
functions in this file, I added a TODO() such that we'll crash, instead
of infinite looping.
  • Loading branch information
FireFox317 authored and linusg committed Apr 13, 2023
1 parent 9ed04bd commit 957f89c
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions AK/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ constexpr T fmod(T x, T y)
} while (fpu_status & 0x400);
return x;
#else
// TODO: Add implementation for this function.
TODO();
return __builtin_fmod(x, y);
#endif
}
Expand All @@ -109,6 +111,8 @@ constexpr T remainder(T x, T y)
} while (fpu_status & 0x400);
return x;
#else
// TODO: Add implementation for this function.
TODO();
return __builtin_fmod(x, y);
#endif
}
Expand Down Expand Up @@ -244,7 +248,9 @@ constexpr T sin(T angle)
: "0"(angle));
return ret;
#else
return __builtin_sin(angle);
// FIXME: This is a very naive implementation, and is only valid for small x.
// Probably a good idea to use a better algorithm in the future, such as a taylor approximation.
return angle;
#endif
}

Expand All @@ -261,7 +267,9 @@ constexpr T cos(T angle)
: "0"(angle));
return ret;
#else
return __builtin_cos(angle);
// FIXME: This is a very naive implementation, and is only valid for small x.
// Probably a good idea to use a better algorithm in the future, such as a taylor approximation.
return 1 - ((angle * angle) / 2);
#endif
}

Expand Down Expand Up @@ -298,7 +306,9 @@ constexpr T tan(T angle)

return ret;
#else
return __builtin_tan(angle);
// FIXME: This is a very naive implementation, and is only valid for small x.
// Probably a good idea to use a better algorithm in the future, such as a taylor approximation.
return angle;
#endif
}

Expand All @@ -316,6 +326,8 @@ constexpr T atan(T value)
: "0"(value));
return ret;
#else
// TODO: Add implementation for this function.
TODO();
return __builtin_atan(value);
#endif
}
Expand Down Expand Up @@ -371,6 +383,8 @@ constexpr T atan2(T y, T x)
: "st(1)");
return ret;
#else
// TODO: Add implementation for this function.
TODO();
return __builtin_atan2(y, x);
#endif
}
Expand Down Expand Up @@ -404,6 +418,8 @@ constexpr T log(T x)
: "0"(x));
return ret;
#else
// TODO: Add implementation for this function.
TODO();
return __builtin_log(x);
#endif
}
Expand All @@ -423,6 +439,8 @@ constexpr T log2(T x)
: "0"(x));
return ret;
#else
// TODO: Add implementation for this function.
TODO();
return __builtin_log2(x);
#endif
}
Expand All @@ -442,6 +460,8 @@ constexpr T log10(T x)
: "0"(x));
return ret;
#else
// TODO: Add implementation for this function.
TODO();
return __builtin_log10(x);
#endif
}
Expand All @@ -466,6 +486,8 @@ constexpr T exp(T exponent)
: "0"(exponent));
return res;
#else
// TODO: Add implementation for this function.
TODO();
return __builtin_exp(exponent);
#endif
}
Expand All @@ -488,6 +510,8 @@ constexpr T exp2(T exponent)
: "0"(exponent));
return res;
#else
// TODO: Add implementation for this function.
TODO();
return __builtin_exp2(exponent);
#endif
}
Expand Down

0 comments on commit 957f89c

Please sign in to comment.