Permalink
Browse files

migrate math fns to ct-head/math.h, update call sites

  • Loading branch information...
postspectacular committed Aug 13, 2016
1 parent d40d2f5 commit 19e3315bb1c85f2166d61b142944c27f70b20a45
View
@@ -1,4 +1,4 @@
[submodule "ext/ct-head"]
path = ext/ct-head
url = https://github.com/thi-ng/ct-head
branch = master
branch = master
Submodule ct-head updated 5 files
+12 −0 .clang-format
+25 −0 README.org
+31 −31 log.h
+214 −0 math.h
+10 −10 vargs.h
View
@@ -131,7 +131,7 @@ void *ct_array_reverse8_imm(void *ptr, size_t num) {
__m64 *iptr = (__m64*)ptr;
for (size_t i = 0, n2 = num >> 1, end = num - 1; i < n2; i++, end--) {
//CT_INFO("swap: %p, %p", &iptr[i], &iptr[end]);
SWAP(__m64, iptr[i], iptr[end]);
CT_SWAP(__m64, iptr[i], iptr[end]);
}
return ptr;
}
@@ -141,8 +141,8 @@ void *ct_array_reverse8_imm(void *ptr, size_t num) {
void *ct_array_reverse8_imm(void *ptr, size_t num) {
uint32_t *iptr = (uint32_t *)ptr;
for (size_t i = 0, end = num * 2 - 2; i < num; i += 2, end -= 2) {
SWAP(uint32_t, iptr[i], iptr[end]);
SWAP(uint32_t, iptr[i + 1], iptr[end + 1]);
CT_SWAP(uint32_t, iptr[i], iptr[end]);
CT_SWAP(uint32_t, iptr[i + 1], iptr[end + 1]);
}
return ptr;
}
View
@@ -31,7 +31,7 @@ static int path_for_point(CT_OTNode *node,
*path++ = node;
i++;
}
return (node->type == CT_TREE_LEAF && ct_deltaeq3fv(node->point, p, EPS))
return (node->type == CT_TREE_LEAF && ct_deltaeq3fv(node->point, p, CT_EPS))
? i
: -1;
}
@@ -114,7 +114,7 @@ static int insert_node(CT_OTNode *node,
node->type = CT_TREE_LEAF;
return 0;
case CT_TREE_LEAF:
if (ct_deltaeq3fv(node->point, p, EPS)) {
if (ct_deltaeq3fv(node->point, p, CT_EPS)) {
node->point = (CT_Vec3f *)p;
node->data = (void *)data;
return 0;
@@ -201,7 +201,7 @@ ct_export CT_OTNode *ct_octree_find_leaf(const CT_Octree *t,
}
node = c;
}
return ((node->type == CT_TREE_LEAF && ct_deltaeq3fv(node->point, p, EPS))
return ((node->type == CT_TREE_LEAF && ct_deltaeq3fv(node->point, p, CT_EPS))
? node
: NULL);
fail:
View
@@ -28,7 +28,7 @@ static int path_for_point(CT_QTNode *node,
*path++ = node;
i++;
}
return (node->type == CT_TREE_LEAF && ct_deltaeq2fv(node->point, p, EPS))
return (node->type == CT_TREE_LEAF && ct_deltaeq2fv(node->point, p, CT_EPS))
? i
: -1;
}
@@ -105,7 +105,7 @@ static int insert_node(CT_QTNode *node,
node->type = CT_TREE_LEAF;
return 0;
case CT_TREE_LEAF:
if (ct_deltaeq2fv(node->point, p, EPS)) {
if (ct_deltaeq2fv(node->point, p, CT_EPS)) {
node->point = (CT_Vec2f *)p;
node->data = (void *)data;
return 0;
@@ -190,7 +190,7 @@ ct_export CT_QTNode *ct_qtree_find_leaf(const CT_Quadtree *t,
}
node = c;
}
return (node->type == CT_TREE_LEAF && ct_deltaeq2fv(node->point, p, EPS)
return (node->type == CT_TREE_LEAF && ct_deltaeq2fv(node->point, p, CT_EPS)
? node
: NULL);
fail:
View
@@ -44,7 +44,7 @@ ct_export float ct_circle2f_circumference(CT_Circle2f *c) {
}
ct_export int ct_circle2f_classify_point(CT_Circle2f *c, CT_Vec2f *p) {
return ct_signumf(c->r - ct_dist2fv(&c->pos, p), EPS);
return ct_signumf(c->r - ct_dist2fv(&c->pos, p), CT_EPS);
}
ct_export CT_Vec2f *ct_circle2f_vertices(CT_Circle2f *c, CT_Vec2f *verts,
@@ -3,7 +3,7 @@
#define CLIP_LB(pp, qq) \
p = (pp); \
q = (qq); \
if (q < 0 && fabs(p) < EPS) { \
if (q < 0 && fabs(p) < CT_EPS) { \
return 0; \
} \
r = q / p; \
View
@@ -63,18 +63,18 @@ ct_export CT_Vec2f *ct_triangle2f_point_opposite_ptr(CT_Triangle2f *t,
ct_export CT_Vec2f *ct_triangle2f_point_opposite(CT_Triangle2f *t,
const CT_Vec2f *a,
const CT_Vec2f *b) {
if (ct_deltaeq2fv(a, &t->a, EPS)) {
return (ct_deltaeq2fv(b, &t->b, EPS)
if (ct_deltaeq2fv(a, &t->a, CT_EPS)) {
return (ct_deltaeq2fv(b, &t->b, CT_EPS)
? &t->c
: (ct_deltaeq2fv(b, &t->c, EPS) ? &t->b : NULL));
} else if (ct_deltaeq2fv(a, &t->b, EPS)) {
return (ct_deltaeq2fv(b, &t->a, EPS)
: (ct_deltaeq2fv(b, &t->c, CT_EPS) ? &t->b : NULL));
} else if (ct_deltaeq2fv(a, &t->b, CT_EPS)) {
return (ct_deltaeq2fv(b, &t->a, CT_EPS)
? &t->c
: (ct_deltaeq2fv(b, &t->c, EPS) ? &t->a : NULL));
} else if (ct_deltaeq2fv(a, &t->c, EPS)) {
return (ct_deltaeq2fv(b, &t->b, EPS)
: (ct_deltaeq2fv(b, &t->c, CT_EPS) ? &t->a : NULL));
} else if (ct_deltaeq2fv(a, &t->c, CT_EPS)) {
return (ct_deltaeq2fv(b, &t->b, CT_EPS)
? &t->a
: (ct_deltaeq2fv(b, &t->a, EPS) ? &t->b : NULL));
: (ct_deltaeq2fv(b, &t->a, CT_EPS) ? &t->b : NULL));
}
return NULL;
}
@@ -158,18 +158,18 @@ ct_export CT_Vec3f *ct_triangle3f_point_opposite_ptr(CT_Triangle3f *t,
ct_export CT_Vec3f *ct_triangle3f_point_opposite(CT_Triangle3f *t,
const CT_Vec3f *a,
const CT_Vec3f *b) {
if (ct_deltaeq3fv(a, &t->a, EPS)) {
return (ct_deltaeq3fv(b, &t->b, EPS)
if (ct_deltaeq3fv(a, &t->a, CT_EPS)) {
return (ct_deltaeq3fv(b, &t->b, CT_EPS)
? &t->c
: (ct_deltaeq3fv(b, &t->c, EPS) ? &t->b : NULL));
} else if (ct_deltaeq3fv(a, &t->b, EPS)) {
return (ct_deltaeq3fv(b, &t->a, EPS)
: (ct_deltaeq3fv(b, &t->c, CT_EPS) ? &t->b : NULL));
} else if (ct_deltaeq3fv(a, &t->b, CT_EPS)) {
return (ct_deltaeq3fv(b, &t->a, CT_EPS)
? &t->c
: (ct_deltaeq3fv(b, &t->c, EPS) ? &t->a : NULL));
} else if (ct_deltaeq3fv(a, &t->c, EPS)) {
return (ct_deltaeq3fv(b, &t->b, EPS)
: (ct_deltaeq3fv(b, &t->c, CT_EPS) ? &t->a : NULL));
} else if (ct_deltaeq3fv(a, &t->c, CT_EPS)) {
return (ct_deltaeq3fv(b, &t->b, CT_EPS)
? &t->a
: (ct_deltaeq3fv(b, &t->a, EPS) ? &t->b : NULL));
: (ct_deltaeq3fv(b, &t->a, CT_EPS) ? &t->b : NULL));
}
return NULL;
}
View
@@ -2,28 +2,6 @@
#include "math/math.h"
ct_export uint32_t ct_ceil_pow2(uint32_t x) {
if (x) {
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
}
return 1;
}
ct_export uint32_t ct_floor_pow2(uint32_t x) {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x - (x >> 1);
}
ct_export float ct_parse_float(char *src, float err) {
float x;
return sscanf(src, "%f", &x) ? x : err;
View
@@ -2,143 +2,12 @@
#include <math.h>
#include "cthing.h"
#ifndef EPS
#define EPS 1e-6f
#endif
#define CT_LN2 0.69314718055994530942f
#define CT_SQRT2 1.414213562373095f
#define CT_PI 3.1415926535897932384626433832795029f
#define CT_TAU (2.0f * CT_PI)
#define CT_HALF_PI (0.5f * CT_PI)
#define CT_QUARTER_PI (0.25f * CT_PI)
#define CT_INV_TAU (1.0f / CT_TAU)
#define CT_INV_PI (1.0f / CT_PI)
#define CT_INV_HALF_PI (1.0f / CT_HALF_PI)
#define CT_DEGREES (180.0 / CT_PI)
#define CT_RADIANS (CT_PI / 180.0)
#define CT_INV_RAND_MAX (float)(1.0 / RAND_MAX)
#define CT_INV_RAND_MAX2 (float)(2.0 / RAND_MAX)
#define CT_MIN(a, b) ((a) < (b) ? (a) : (b))
#define CT_MAX(a, b) ((a) > (b) ? (a) : (b))
#define CLAMP(a, b, c) CT_MIN(CT_MAX(a, b), c)
#define IN_RANGE(x, a, b) ((x) >= (a) && (x) <= (b))
#define SIGNUM(x, eps) (int)((x) < 0 ? -1 : ((x) > 0) ? 1 : 0)
#define COMPARE(a, b) (int)((a) < (b) ? -1 : ((a) > (b) ? 1 : 0))
#define SWAP(type, a, b) \
do { \
type tmp; \
tmp = (a); \
(a) = (b); \
(b) = tmp; \
} while (0)
#include "ct-head/math.h"
CT_BEGIN_DECLS
uint32_t ct_ceil_pow2(uint32_t x);
uint32_t ct_floor_pow2(uint32_t x);
float ct_parse_float(char *src, float err);
int ct_parse_int(char *src, int err);
uint32_t ct_parse_hex32(char *src, uint32_t err);
// x must be 0..1
// bias: 1 = unbiased, <1 = bias towards high, >1 = bias towards low
ct_export ct_inline float ct_biasf(float x, float low, float high, float bias) {
return low + (high - low) * powf(x, bias);
}
ct_export ct_inline int ct_clampi(const int x, const int a, const int b) {
return CLAMP(x, a, b);
}
ct_export ct_inline float ct_clampf(const float x,
const float a,
const float b) {
return CLAMP(x, a, b);
}
ct_export ct_inline size_t ct_clz8(uint8_t x) {
#if defined(__GCC__) || defined(__clang__) || defined(__EMSCRIPTEN__)
return x == 0 ? 8 : __builtin_clz(x) - 24;
#else
return 0; // FIXME
#endif
}
ct_export ct_inline int ct_deltaeqf(const float a,
const float b,
const float eps) {
return fabs(a - b) <= eps;
}
ct_inline float ct_maddf(const float a, const float b, const float c) {
return a * b + c;
}
ct_inline int ct_maxi(const int a, const int b) {
return CT_MAX(a, b);
}
ct_inline float ct_maxf(const float a, const float b) {
return CT_MAX(a, b);
}
ct_inline int ct_mini(const int a, const int b) {
return CT_MIN(a, b);
}
ct_inline float ct_minf(const float a, const float b) {
return CT_MIN(a, b);
}
ct_inline float ct_mixf(const float a, const float b, const float t) {
return a + (b - a) * t;
}
ct_inline size_t ct_ceil_multiple_pow2(const size_t x, const size_t y) {
return (x + (y - 1)) & ~(y - 1);
}
ct_inline float ct_rand_norm() {
return rand() * CT_INV_RAND_MAX2 - 1.0f;
}
ct_inline float ct_rand_normpos() {
return rand() * CT_INV_RAND_MAX;
}
ct_inline float ct_rand_minmax(const float min, const float max) {
return ct_rand_normpos() * (max - min) + min;
}
ct_inline int ct_signumf(const float x, const float eps) {
if (ct_deltaeqf(x, 0.f, eps)) {
return 0;
}
return (x < 0.f ? -1 : 1);
}
ct_inline float ct_stepf(const float x,
const float edge,
const float y1,
const float y2) {
return (x < edge ? y1 : y2);
}
ct_inline float ct_wrapf(float x, float domain) {
return ((x < 0.f) ? (domain + x) : (x >= domain ? (x - domain) : x));
}
ct_inline uint32_t ct_rotl32(uint32_t x, uint8_t r) {
return (x << r) | (x >> (32 - r));
}
ct_inline uint64_t ct_rotl64(uint64_t x, uint8_t r) {
return (x << r) | (x >> (64 - r));
}
CT_END_DECLS
View
@@ -140,12 +140,12 @@ ct_export CT_Mat4f *ct_mat4f_set_identity(CT_Mat4f *m) {
}
ct_export CT_Mat4f *ct_mat4f_transpose_imm(CT_Mat4f *m) {
SWAP(float, m->m01, m->m10);
SWAP(float, m->m02, m->m20);
SWAP(float, m->m03, m->m30);
SWAP(float, m->m12, m->m21);
SWAP(float, m->m13, m->m31);
SWAP(float, m->m23, m->m32);
CT_SWAP(float, m->m01, m->m10);
CT_SWAP(float, m->m02, m->m20);
CT_SWAP(float, m->m03, m->m30);
CT_SWAP(float, m->m12, m->m21);
CT_SWAP(float, m->m13, m->m31);
CT_SWAP(float, m->m23, m->m32);
return m;
}
@@ -459,7 +459,7 @@ ct_export int ct_mat4f_set_lookat(CT_Mat4f *m,
const CT_Vec3f *up) {
CT_Vec3f x, y, z;
ct_sub3fv(eye, target, &z);
if (ct_magsq3f(&z) < EPS) {
if (ct_magsq3f(&z) < CT_EPS) {
return 1;
}
ct_normalize3f_imm(&z, 1);
View
@@ -266,7 +266,7 @@ ct_export ct_inline CT_Vec2f *ct_normalize2f(const CT_Vec2f *v,
}
ct_export ct_inline size_t ct_is_normalized2f(const CT_Vec2f *v) {
return ct_deltaeqf(ct_mag2f(v), 1.f, EPS);
return ct_deltaeqf(ct_mag2f(v), 1.f, CT_EPS);
}
ct_export ct_inline CT_Vec2f *ct_limit2f_imm(CT_Vec2f *v, float len) {
View
@@ -14,11 +14,11 @@ CT_Vec3f *ct_vec3fn(float n, CT_MPool *mpool);
ct_export ct_inline int ct_compare3fv(const void *a, const void *b) {
CT_Vec3f *va = (CT_Vec3f *)a;
CT_Vec3f *vb = (CT_Vec3f *)b;
int c = COMPARE(va->x, vb->x);
int c = CT_COMPARE(va->x, vb->x);
if (!c) {
c = COMPARE(va->y, vb->y);
c = CT_COMPARE(va->y, vb->y);
if (!c) {
return COMPARE(va->z, vb->z);
return CT_COMPARE(va->z, vb->z);
}
}
return c;
@@ -193,7 +193,7 @@ ct_export ct_inline CT_Vec3f *ct_normalize3f(const CT_Vec3f *v,
}
ct_export ct_inline size_t ct_is_normalized3f(const CT_Vec3f *v) {
return ct_deltaeqf(ct_mag3f(v), 1.f, EPS);
return ct_deltaeqf(ct_mag3f(v), 1.f, CT_EPS);
}
ct_export ct_inline CT_Vec3f *ct_limit3f_imm(CT_Vec3f *v, float len) {
Oops, something went wrong.

0 comments on commit 19e3315

Please sign in to comment.