aboutsummaryrefslogtreecommitdiffstats
path: root/gb_math.h
diff options
context:
space:
mode:
authorGravatar gingerBill 2016-05-01 02:10:32 +0100
committerGravatar gingerBill 2016-05-01 02:10:32 +0100
commitc6d3f17e27f91b54759618984e640f9eba59d179 (patch)
tree9a28f7fce790a9a70edad65f64d93a2d046a295d /gb_math.h
parentOS X Support (diff)
Implement rough versions of mod, remainder, copy_sign
Diffstat (limited to 'gb_math.h')
-rw-r--r--gb_math.h115
1 files changed, 27 insertions, 88 deletions
diff --git a/gb_math.h b/gb_math.h
index c352644..41e17b8 100644
--- a/gb_math.h
+++ b/gb_math.h
@@ -1,9 +1,9 @@
-/* gb_math.h - v0.06 - public domain C math library - no warranty implied; use at your own risk */
-/* A C math library geared towards game development */
-/* use '#define GB_MATH_IMPLEMENTATION' before including to create the implementation in _ONE_ file */
+/* gb_math.h - v0.06a - public domain C math library - no warranty implied; use at your own risk
+ A C math library geared towards game development
+ use '#define GB_MATH_IMPLEMENTATION' before including to create the implementation in _ONE_ file
-/*
Version History:
+ 0.06a - Implement rough versions of mod, remainder, copy_sign
0.06 - Windows GCC Support and C90-ish Support
0.05 - Less/no dependencies or CRT
0.04d - License Update
@@ -205,6 +205,8 @@ GB_MATH_DEF float gb_angle_diff(float radians_a, float radians_b);
#define gb_max(a, b) ((a) > (b) ? (a) : (b))
#endif
+GB_MATH_DEF float gb_copy_sign(float x, float y);
+GB_MATH_DEF float gb_remainder(float x, float y);
GB_MATH_DEF float gb_mod(float x, float y);
GB_MATH_DEF float gb_sqrt(float a);
GB_MATH_DEF float gb_rsqrt(float a);
@@ -771,95 +773,32 @@ gb_angle_diff(float radians_a, float radians_b)
return delta;
}
-
float
-gb_mod(float x, float y)
+gb_copy_sign(float x, float y)
{
- static float const gb__zero[] = {0, -0};
-
- int n, hx, hy, hz, ix, iy, sx, i;
-
- hx = *(int *)&x;
- hy = *(int *)&y;
- sx = hx & 0x80000000;
- hx ^= sx;
- hy &= 0x7fffffff;
-
- /* NOTE(bill): Purge off exception values */
- if (hy == 0||(hx >= 0x7f800000)|| /* NOTE(bill): Y=0,or x not finite */
- (hy > 0x7f800000)) /* NOTE(bill): Or y is NaN */
- return (x*y)/(x*y);
- if (hx < hy) return x; /* NOTE(bill): |x|<|y| return x */
- if (hx == hy) return gb__zero[(unsigned int)sx>>31]; /* NOTE(bill): |x|=|y| return x*0 */
-
- /* NOTE(bill): Determine ix = ilogb(x) */
- if (hx < 0x00800000) { /* NOTE(bill): Subnormal x */
- for (ix = -126, i = (hx<<8); i > 0; i <<= 1)
- ix -=1;
- } else {
- ix = (hx>>23)-127;
- }
-
- /* NOTE(bill): Determine iy = ilogb(y) */
- if (hy < 0x00800000) { /* NOTE(bill): Subnormal y */
- for (iy = -126, i = (hy<<8); i >= 0; i <<=1 )
- iy -=1;
- } else {
- iy = (hy>>23)-127;
- }
-
- /* NOTE(bill): Set up {hx, lx}, {hy, ly} and align y to x */
- if (ix >= -126) {
- hx = 0x00800000|(0x007fffff&hx);
- } else { /* NOTE(bill): Subnormal x, shift x to normal */
- n = -126-ix;
- hx = hx<<n;
- }
- if (iy >= -126) {
- hy = 0x00800000|(0x007fffff&hy);
- } else { /* NOTE(bill): Subnormal y, shift y to normal */
- n = -126-iy;
- hy = hy<<n;
- }
+ int ix, iy;
+ ix = *(int *)&x;
+ iy = *(int *)&y;
- /* NOTE(bill): Fix point fmod */
- n = ix - iy;
- while(n--) {
- hz= hx-hy;
- if (hz < 0) {
- hx = hx+hx;
- } else {
- if (hz == 0) /* NOTE(bill): Return gb_sign(x)*0 */
- return gb__zero[(unsigned int)sx>>31];
- hx = hz+hz;
- }
- }
- hz = hx-hy;
- if (hz >= 0)
- hx = hz;
+ ix &= 0x7fffffff;
+ ix |= iy & 0x80000000;
+ return *(float *)ix;
+}
- /* NOTE(bill): Convert back to floating value and restore the sign */
- if (hx == 0) /* NOTE(bill): Return sign(x)*0 */
- return gb__zero[(unsigned int)sx>>31];
+float
+gb_remainder(float x, float y)
+{
+ return x - (gb_round(x/y)*y);
+}
- while (hx < 0x00800000) { /* NOTE(bill): Normalize x */
- hx = hx+hx;
- iy -= 1;
- }
- if (iy >= -126) { /* NOTE(bill): Normalize output */
- int t;
- hx = ((hx-0x00800000) | ((iy + 127)<<23));
- t = hx | sx;
- x = *(float *)&t;
- } else {
- int t;
- n = -126 - iy;
- hx >>= n;
- t = hx | sx;
- x = *(float *)&t;
- x *= 1.0f;
- }
- return x;
+float
+gb_mod(float x, float y)
+{
+ float result;
+ y = gb_abs(y);
+ result = gb_remainder(gb_abs(x), (y = gb_abs(y)));
+ if (gb_sign(result)) result += y;
+ return gb_copy_sign(result, x);
}
Bump up version numbers for release.Gravatar aeb 2-3/+11 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@44 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-11-24Added libraw1394.postinst.in to list of distributed files.Gravatar aeb 3-3/+35 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@43 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-11-23Add ldconfig in deb postinst for Debian policy conformance.Gravatar aeb 2-2/+17 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@42 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-11-23Removed acconfig.h, which wasn't needed for some time.Gravatar aeb 1-13/+0 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@41 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-11-22Added ieee1394.h header.Gravatar aeb 3-1/+38 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@40 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-09-13Fix raw1394_start_iso_write() which uses wrong variable.Gravatar aeb 1-1/+1 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@39 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-09-10Work around compiler warnings for int/ptr casts.Gravatar aeb 6-10/+20 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@38 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-09-10Added control files for Debian packages.Gravatar aeb 6-8/+106 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@37 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-09-01Added missing prototypes for iso send functions.Gravatar aeb 1-0/+7 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@36 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-08-08Added raw1394_get_irm_id().Gravatar aeb 7-7/+39 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@35 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-08-06Added support for isochronous sending.Gravatar aeb 3-0/+35 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@34 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-07-05Added raw1394_reset_bus() call.Gravatar aeb 4-0/+23 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@33 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-22- Set library version info in configure.in, use in src/Makefile.am.Gravatar aeb 4-2/+16 - Enable compiler warnings. git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@32 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-15Update libtool version number.Gravatar aeb 2-2/+2 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@31 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-14Added copyright headers.Gravatar aeb 6-0/+54 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@30 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-11Added explicit AC_PROG_INSTALL call.Gravatar aeb 1-0/+1 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@29 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-09Fix size of error field.Gravatar aeb 1-2/+2 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@28 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-06-02Modified support for 32/64 bit environments, control struct fields have ↵Gravatar aeb 7-43/+28 fixed size now. git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@27 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-05-28Added support for environments with 64 bit kernel and 32 bit userland.Gravatar aeb 8-7/+45 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@26 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-04-27Fixed missing setting of ext code in raw1394_start_lock()Gravatar aeb 1-0/+1 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@25 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-04-15Fixed lock transaction to actually return response value.Gravatar aeb 3-5/+11 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@24 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-04-12Add userdata functions as news.Gravatar aeb 1-0/+4 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@23 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-04-05Add userdata functions.Gravatar aeb 3-0/+18 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@22 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-03-18Bump version number to 0.6.Gravatar aeb 3-5/+6 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@21 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-03-18Mention byte order change.Gravatar aeb 1-0/+2 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@20 53a565d1-3bb7-0310-b661-cf11e63c67ab 2000-03-18Mention SourceForge home.Gravatar aeb 1-1/+5 git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@19 53a565d1-3bb7-0310-b661-cf11e63c67ab