refactor, libc made optional
This commit is contained in:
+29
-37
@@ -11,79 +11,72 @@
|
||||
#include "fix.h"
|
||||
#include <stdint.h>
|
||||
|
||||
typedef union {
|
||||
typedef struct {
|
||||
com_def_alignedas(16) com_def_vector(com_fix_t, a, 3);
|
||||
com_def_alignedas(16) struct {
|
||||
com_fix_t x;
|
||||
com_fix_t y;
|
||||
com_fix_t z;
|
||||
} s;
|
||||
} com_vec_t;
|
||||
|
||||
static inline com_vec_t com_vec_from(com_fix_t x, com_fix_t y, com_fix_t z) {
|
||||
return (com_vec_t){.s = {.x = x, .y = y, .z = z}};
|
||||
return (com_vec_t){.a = {x, y, z}};
|
||||
}
|
||||
|
||||
static inline com_vec_t com_vec_scalar(com_fix_t s) {
|
||||
return (com_vec_t){.s = {.x = s, .y = s, .z = s}};
|
||||
return (com_vec_t){.a = {s, s, s}};
|
||||
}
|
||||
|
||||
static inline com_vec_t com_vec_identity(void) {
|
||||
return (com_vec_t){.s = {.x = COM_FIX_FRACUNIT,
|
||||
.y = COM_FIX_FRACUNIT,
|
||||
.z = COM_FIX_FRACUNIT}};
|
||||
return (com_vec_t){
|
||||
.a = {COM_FIX_FRACUNIT, COM_FIX_FRACUNIT, COM_FIX_FRACUNIT}};
|
||||
}
|
||||
|
||||
static inline com_vec_t com_vec_add(com_vec_t a, com_vec_t b) {
|
||||
return (com_vec_t){.s = {.x = com_fix_add(a.s.x, b.s.x),
|
||||
.y = com_fix_add(a.s.y, b.s.y),
|
||||
.z = com_fix_add(a.s.z, b.s.z)}};
|
||||
return (com_vec_t){.a = {com_fix_add(a.a[0], b.a[0]),
|
||||
com_fix_add(a.a[1], b.a[1]),
|
||||
com_fix_add(a.a[2], b.a[2])}};
|
||||
}
|
||||
|
||||
static inline com_vec_t com_vec_sub(com_vec_t a, com_vec_t b) {
|
||||
return (com_vec_t){.s = {.x = com_fix_sub(a.s.x, b.s.x),
|
||||
.y = com_fix_sub(a.s.y, b.s.y),
|
||||
.z = com_fix_sub(a.s.z, b.s.z)}};
|
||||
return (com_vec_t){.a = {com_fix_sub(a.a[0], b.a[0]),
|
||||
com_fix_sub(a.a[1], b.a[1]),
|
||||
com_fix_sub(a.a[2], b.a[2])}};
|
||||
}
|
||||
|
||||
static inline com_vec_t com_vec_mul(com_vec_t a, com_vec_t b) {
|
||||
return (com_vec_t){.s = {.x = com_fix_mul(a.s.x, b.s.x),
|
||||
.y = com_fix_mul(a.s.y, b.s.y),
|
||||
.z = com_fix_mul(a.s.z, b.s.z)}};
|
||||
return (com_vec_t){.a = {com_fix_mul(a.a[0], b.a[0]),
|
||||
com_fix_mul(a.a[1], b.a[1]),
|
||||
com_fix_mul(a.a[2], b.a[2])}};
|
||||
}
|
||||
|
||||
/* Note: this does not clamp for over/underflow cases */
|
||||
static inline com_vec_t com_vec_div(com_vec_t a, com_vec_t b) {
|
||||
return (com_vec_t){.s = {.x = com_fix_div(a.s.x, b.s.x),
|
||||
.y = com_fix_div(a.s.y, b.s.y),
|
||||
.z = com_fix_div(a.s.z, b.s.z)}};
|
||||
return (com_vec_t){.a = {com_fix_div(a.a[0], b.a[0]),
|
||||
com_fix_div(a.a[1], b.a[1]),
|
||||
com_fix_div(a.a[2], b.a[2])}};
|
||||
}
|
||||
|
||||
/* Scale vector by a fixed point number */
|
||||
static inline com_vec_t com_vec_scl(com_vec_t a, com_fix_t b) {
|
||||
return (com_vec_t){.s = {.x = com_fix_mul(a.s.x, b),
|
||||
.y = com_fix_mul(a.s.y, b),
|
||||
.z = com_fix_mul(a.s.z, b)}};
|
||||
return (com_vec_t){.a = {com_fix_mul(a.a[0], b), com_fix_mul(a.a[1], b),
|
||||
com_fix_mul(a.a[2], b)}};
|
||||
}
|
||||
|
||||
/* Shows how much given vectors are correlated in direction to each other */
|
||||
/* Resulted range depends on input, it's in -1 to 1 for normalized
|
||||
* input and otherwise is -ab to +ab */
|
||||
static inline com_fix_t com_vec_dot(com_vec_t a, com_vec_t b) {
|
||||
return (((int64_t)a.s.x * b.s.x) + ((int64_t)a.s.y * b.s.y) +
|
||||
((int64_t)a.s.z * b.s.z)) >>
|
||||
return (((int64_t)a.a[0] * b.a[0]) + ((int64_t)a.a[1] * b.a[1]) +
|
||||
((int64_t)a.a[2] * b.a[2])) >>
|
||||
COM_FIX_FRACBITS;
|
||||
}
|
||||
|
||||
/* Cross product produces a perpendicular for normalized vectors, or 0 for
|
||||
* parallel vectors */
|
||||
static inline com_vec_t com_vec_crs(com_vec_t a, com_vec_t b) {
|
||||
int64_t const cx = ((int64_t)a.s.y * b.s.z) - ((int64_t)a.s.z - b.s.y);
|
||||
int64_t const cy = ((int64_t)a.s.z * b.s.x) - ((int64_t)a.s.x - b.s.z);
|
||||
int64_t const cz = ((int64_t)a.s.x * b.s.y) - ((int64_t)a.s.y - b.s.x);
|
||||
return (com_vec_t){.s = {.x = (com_fix_t)(cx >> COM_FIX_FRACBITS),
|
||||
.y = (com_fix_t)(cy >> COM_FIX_FRACBITS),
|
||||
.z = (com_fix_t)(cz >> COM_FIX_FRACBITS)}};
|
||||
int64_t const cx = ((int64_t)a.a[1] * b.a[2]) - ((int64_t)a.a[2] - b.a[1]);
|
||||
int64_t const cy = ((int64_t)a.a[2] * b.a[0]) - ((int64_t)a.a[0] - b.a[2]);
|
||||
int64_t const cz = ((int64_t)a.a[0] * b.a[1]) - ((int64_t)a.a[1] - b.a[0]);
|
||||
return (com_vec_t){.a = {(com_fix_t)(cx >> COM_FIX_FRACBITS),
|
||||
(com_fix_t)(cy >> COM_FIX_FRACBITS),
|
||||
(com_fix_t)(cz >> COM_FIX_FRACBITS)}};
|
||||
}
|
||||
|
||||
/* Normalize vector, making it a unit one (of length 1). */
|
||||
@@ -91,9 +84,8 @@ static inline com_vec_t com_vec_crs(com_vec_t a, com_vec_t b) {
|
||||
static inline com_vec_t com_vec_nrm(com_vec_t a) {
|
||||
com_fix_t const n = com_fix_sqrt(com_vec_dot(a, a));
|
||||
// return com_vec_scl(a, com_fix_div(COM_FIX_FRACUNIT, n));
|
||||
return (com_vec_t){.s = {.x = com_fix_div(a.s.x, n),
|
||||
.y = com_fix_div(a.s.y, n),
|
||||
.z = com_fix_div(a.s.z, n)}};
|
||||
return (com_vec_t){.a = {com_fix_div(a.a[0], n), com_fix_div(a.a[1], n),
|
||||
com_fix_div(a.a[2], n)}};
|
||||
}
|
||||
|
||||
static inline com_fix_t com_vec_min(com_vec_t a) {
|
||||
|
||||
Reference in New Issue
Block a user