Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vector4_ptr.c
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025-2026 Igal Alkon
2// SPDX-FileCopyrightText: 2026 ALKONTEK <git@alkontek.com>
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <vecmat.h>
6#include "features/cpu.h"
7
16{
17 res->x = a->x + b->x;
18 res->y = a->y + b->y;
19 res->z = a->z + b->z;
20 res->w = a->w + b->w;
21}
22
31{
32 res->x = a->x - b->x;
33 res->y = a->y - b->y;
34 res->z = a->z - b->z;
35 res->w = a->w - b->w;
36}
37
46{
47 res->x = v->x * s;
48 res->y = v->y * s;
49 res->z = v->z * s;
50 res->w = v->w * s;
51}
52
63{
64 if (s == 0.0f) {
65 res->x = res->y = res->z = res->w = 0.0f;
66 return;
67 }
68 res->x = v->x / s;
69 res->y = v->y / s;
70 res->z = v->z / s;
71 res->w = v->w / s;
72}
73
82{
83 res->x = a->x * b->x;
84 res->y = a->y * b->y;
85 res->z = a->z * b->z;
86 res->w = a->w * b->w;
87}
88
96{
97 res->x = -v->x;
98 res->y = -v->y;
99 res->z = -v->z;
100 res->w = -v->w;
101}
102
110{
111 res->x = VECMAT_FABS(v->x);
112 res->y = VECMAT_FABS(v->y);
113 res->z = VECMAT_FABS(v->z);
114 res->w = VECMAT_FABS(v->w);
115}
116
124{
125 const vm_float_t len = vec4_length(*v);
126 if (len == 0.0f) {
127 *res = *v;
128 return;
129 }
130 const vm_float_t inv_len = 1.0f / len;
131 res->x = v->x * inv_len;
132 res->y = v->y * inv_len;
133 res->z = v->z * inv_len;
134 res->w = v->w * inv_len;
135}
136
145{
146 res->x = VECMAT_FMIN(a->x, b->x);
147 res->y = VECMAT_FMIN(a->y, b->y);
148 res->z = VECMAT_FMIN(a->z, b->z);
149 res->w = VECMAT_FMIN(a->w, b->w);
150}
151
160{
161 res->x = VECMAT_FMAX(a->x, b->x);
162 res->y = VECMAT_FMAX(a->y, b->y);
163 res->z = VECMAT_FMAX(a->z, b->z);
164 res->w = VECMAT_FMAX(a->w, b->w);
165}
166
174{
175 res->x = v->x > 0.0f ? 1.0f : v->x < 0.0f ? -1.0f : 0.0f;
176 res->y = v->y > 0.0f ? 1.0f : v->y < 0.0f ? -1.0f : 0.0f;
177 res->z = v->z > 0.0f ? 1.0f : v->z < 0.0f ? -1.0f : 0.0f;
178 res->w = v->w > 0.0f ? 1.0f : v->w < 0.0f ? -1.0f : 0.0f;
179}
180
188{
189 res->x = VECMAT_FLOOR(v->x);
190 res->y = VECMAT_FLOOR(v->y);
191 res->z = VECMAT_FLOOR(v->z);
192 res->w = VECMAT_FLOOR(v->w);
193}
194
202{
203 res->x = VECMAT_CEIL(v->x);
204 res->y = VECMAT_CEIL(v->y);
205 res->z = VECMAT_CEIL(v->z);
206 res->w = VECMAT_CEIL(v->w);
207}
208
216{
217 res->x = VECMAT_ROUND(v->x);
218 res->y = VECMAT_ROUND(v->y);
219 res->z = VECMAT_ROUND(v->z);
220 res->w = VECMAT_ROUND(v->w);
221}
222
233{
234 res->x = a->x + t * (b->x - a->x);
235 res->y = a->y + t * (b->y - a->y);
236 res->z = a->z + t * (b->z - a->z);
237 res->w = a->w + t * (b->w - a->w);
238}
239
249VECMAT_SCALAR_API void vec4_clamp_ptr_scalar(vector4 *res, const vector4 *v, const vector4 *min, const vector4 *max)
250{
251 res->x = VECMAT_FMAX(min->x, VECMAT_FMIN(max->x, v->x));
252 res->y = VECMAT_FMAX(min->y, VECMAT_FMIN(max->y, v->y));
253 res->z = VECMAT_FMAX(min->z, VECMAT_FMIN(max->z, v->z));
254 res->w = VECMAT_FMAX(min->w, VECMAT_FMIN(max->w, v->w));
255}
256
267{
268 if (VECMAT_FABS(v->w) > VECMAT_EPSILON) {
269 const vm_float_t inv_w = 1.0f / v->w;
270 res->x = v->x * inv_w;
271 res->y = v->y * inv_w;
272 res->z = v->z * inv_w;
273 res->w = 1.0f;
274 } else {
275 res->x = res->y = res->z = res->w = 0.0f;
276 }
277}
278
285void vec4_to_vec3_ptr(vector3 *res, const vector4 *v)
286{
287 res->x = v->x;
288 res->y = v->y;
289 res->z = v->z;
290}
291
300{
301 res->x = (b->x == 0.0f) ? 0.0f : a->x / b->x;
302 res->y = (b->y == 0.0f) ? 0.0f : a->y / b->y;
303 res->z = (b->z == 0.0f) ? 0.0f : a->z / b->z;
304 res->w = (b->w == 0.0f) ? 0.0f : a->w / b->w;
305}
306
315{
316 res->x = v->x + s;
317 res->y = v->y + s;
318 res->z = v->z + s;
319 res->w = v->w + s;
320}
321
330{
331 res->x = v->x - s;
332 res->y = v->y - s;
333 res->z = v->z - s;
334 res->w = v->w - s;
335}
336
346{
347 res->x = VECMAT_FMIN(VECMAT_FMAX(v->x, min), max);
348 res->y = VECMAT_FMIN(VECMAT_FMAX(v->y, min), max);
349 res->z = VECMAT_FMIN(VECMAT_FMAX(v->z, min), max);
350 res->w = VECMAT_FMIN(VECMAT_FMAX(v->w, min), max);
351}
352
360{
361 vec4_clamp_scalar_ptr_scalar(res, v, 0.0f, 1.0f);
362}
363
371{
372 res->x = v->x - VECMAT_FLOOR(v->x);
373 res->y = v->y - VECMAT_FLOOR(v->y);
374 res->z = v->z - VECMAT_FLOOR(v->z);
375 res->w = v->w - VECMAT_FLOOR(v->w);
376}
377
385void vec4_project_ptr(vector4 *res, const vector4 *a, const vector4 *b)
386{
387 const vm_float_t denom = vec4_dot(*b, *b);
388 if (denom == 0.0f) {
389 res->x = res->y = res->z = res->w = 0.0f;
390 return;
391 }
392 const vm_float_t scale = vec4_dot(*a, *b) / denom;
393 res->x = b->x * scale;
394 res->y = b->y * scale;
395 res->z = b->z * scale;
396 res->w = b->w * scale;
397}
398
406void vec4_slide_ptr(vector4 *res, const vector4 *v, const vector4 *normal)
407{
408 const vm_float_t d = vec4_dot(*v, *normal);
409 res->x = v->x - normal->x * d;
410 res->y = v->y - normal->y * d;
411 res->z = v->z - normal->z * d;
412 res->w = v->w - normal->w * d;
413}
414
422void vec4_reject_ptr(vector4 *res, const vector4 *a, const vector4 *b)
423{
424 vector4 projected;
425 vec4_project_ptr(&projected, a, b);
426 res->x = a->x - projected.x;
427 res->y = a->y - projected.y;
428 res->z = a->z - projected.z;
429 res->w = a->w - projected.w;
430}
431
432void vec4_add_ptr(vector4 *res, const vector4 *a, const vector4 *b)
433{
434#ifdef VECMAT_RUNTIME_DISPATCH
435 vm_cpu_init();
436 vec4_add_ptr_(res, a, b);
437#else
438 vec4_add_ptr_scalar(res, a, b);
439#endif
440}
441
442void vec4_sub_ptr(vector4 *res, const vector4 *a, const vector4 *b)
443{
444#ifdef VECMAT_RUNTIME_DISPATCH
445 vm_cpu_init();
446 vec4_sub_ptr_(res, a, b);
447#else
448 vec4_sub_ptr_scalar(res, a, b);
449#endif
450}
451
452void vec4_mul_ptr(vector4 *res, const vector4 *a, const vector4 *b)
453{
454#ifdef VECMAT_RUNTIME_DISPATCH
455 vm_cpu_init();
456 vec4_mul_ptr_(res, a, b);
457#else
458 vec4_mul_ptr_scalar(res, a, b);
459#endif
460}
461
462void vec4_mul_scalar_ptr(vector4 *res, const vector4 *v, const vm_float_t s)
463{
464#ifdef VECMAT_RUNTIME_DISPATCH
465 vm_cpu_init();
466 vec4_mul_scalar_ptr_(res, v, s);
467#else
469#endif
470}
471
472void vec4_div_scalar_ptr(vector4 *res, const vector4 *v, const vm_float_t s)
473{
474#ifdef VECMAT_RUNTIME_DISPATCH
475 vm_cpu_init();
476 vec4_div_scalar_ptr_(res, v, s);
477#else
479#endif
480}
481
482void vec4_neg_ptr(vector4 *res, const vector4 *v)
483{
484#ifdef VECMAT_RUNTIME_DISPATCH
485 vm_cpu_init();
486 vec4_neg_ptr_(res, v);
487#else
488 vec4_neg_ptr_scalar(res, v);
489#endif
490}
491
492void vec4_abs_ptr(vector4 *res, const vector4 *v)
493{
494#ifdef VECMAT_RUNTIME_DISPATCH
495 vm_cpu_init();
496 vec4_abs_ptr_(res, v);
497#else
498 vec4_abs_ptr_scalar(res, v);
499#endif
500}
501
503{
504#ifdef VECMAT_RUNTIME_DISPATCH
505 vm_cpu_init();
506 vec4_normalize_ptr_(res, v);
507#else
509#endif
510}
511
512void vec4_min_ptr(vector4 *res, const vector4 *a, const vector4 *b)
513{
514#ifdef VECMAT_RUNTIME_DISPATCH
515 vm_cpu_init();
516 vec4_min_ptr_(res, a, b);
517#else
518 vec4_min_ptr_scalar(res, a, b);
519#endif
520}
521
522void vec4_max_ptr(vector4 *res, const vector4 *a, const vector4 *b)
523{
524#ifdef VECMAT_RUNTIME_DISPATCH
525 vm_cpu_init();
526 vec4_max_ptr_(res, a, b);
527#else
528 vec4_max_ptr_scalar(res, a, b);
529#endif
530}
531
532void vec4_lerp_ptr(vector4 *res, const vector4 *a, const vector4 *b, const vm_float_t t)
533{
534#ifdef VECMAT_RUNTIME_DISPATCH
535 vm_cpu_init();
536 vec4_lerp_ptr_(res, a, b, t);
537#else
538 vec4_lerp_ptr_scalar(res, a, b, t);
539#endif
540}
541
542void vec4_clamp_ptr(vector4 *res, const vector4 *v, const vector4 *min, const vector4 *max)
543{
544#ifdef VECMAT_RUNTIME_DISPATCH
545 vm_cpu_init();
546 vec4_clamp_ptr_(res, v, min, max);
547#else
548 vec4_clamp_ptr_scalar(res, v, min, max);
549#endif
550}
551
552void vec4_div_ptr(vector4 *res, const vector4 *a, const vector4 *b)
553{
554#ifdef VECMAT_RUNTIME_DISPATCH
555 vm_cpu_init();
556 vec4_div_ptr_(res, a, b);
557#else
558 vec4_div_ptr_scalar(res, a, b);
559#endif
560}
561
562void vec4_add_scalar_ptr(vector4 *res, const vector4 *v, const vm_float_t s)
563{
564#ifdef VECMAT_RUNTIME_DISPATCH
565 vm_cpu_init();
566 vec4_add_scalar_ptr_(res, v, s);
567#else
569#endif
570}
571
572void vec4_sub_scalar_ptr(vector4 *res, const vector4 *v, const vm_float_t s)
573{
574#ifdef VECMAT_RUNTIME_DISPATCH
575 vm_cpu_init();
576 vec4_sub_scalar_ptr_(res, v, s);
577#else
579#endif
580}
581
582void vec4_clamp_scalar_ptr(vector4 *res, const vector4 *v, const vm_float_t min, const vm_float_t max)
583{
584#ifdef VECMAT_RUNTIME_DISPATCH
585 vm_cpu_init();
586 vec4_clamp_scalar_ptr_(res, v, min, max);
587#else
588 vec4_clamp_scalar_ptr_scalar(res, v, min, max);
589#endif
590}
591
593{
594#ifdef VECMAT_RUNTIME_DISPATCH
595 vm_cpu_init();
596 vec4_saturate_ptr_(res, v);
597#else
599#endif
600}
601
602void vec4_sign_ptr(vector4 *res, const vector4 *v)
603{
604#ifdef VECMAT_RUNTIME_DISPATCH
605 vm_cpu_init();
606 vec4_sign_ptr_(res, v);
607#else
608 vec4_sign_ptr_scalar(res, v);
609#endif
610}
611
612void vec4_floor_ptr(vector4 *res, const vector4 *v)
613{
614#ifdef VECMAT_RUNTIME_DISPATCH
615 vm_cpu_init();
616 vec4_floor_ptr_(res, v);
617#else
618 vec4_floor_ptr_scalar(res, v);
619#endif
620}
621
622void vec4_ceil_ptr(vector4 *res, const vector4 *v)
623{
624#ifdef VECMAT_RUNTIME_DISPATCH
625 vm_cpu_init();
626 vec4_ceil_ptr_(res, v);
627#else
628 vec4_ceil_ptr_scalar(res, v);
629#endif
630}
631
632void vec4_round_ptr(vector4 *res, const vector4 *v)
633{
634#ifdef VECMAT_RUNTIME_DISPATCH
635 vm_cpu_init();
636 vec4_round_ptr_(res, v);
637#else
638 vec4_round_ptr_scalar(res, v);
639#endif
640}
641
642void vec4_fract_ptr(vector4 *res, const vector4 *v)
643{
644#ifdef VECMAT_RUNTIME_DISPATCH
645 vm_cpu_init();
646 vec4_fract_ptr_(res, v);
647#else
648 vec4_fract_ptr_scalar(res, v);
649#endif
650}
651
653{
654#ifdef VECMAT_RUNTIME_DISPATCH
655 vm_cpu_init();
656 vec4_homogenize_ptr_(res, v);
657#else
659#endif
660}
#define VECMAT_SCALAR_API
Definition cpu.h:17
vm_float_t z
Definition vecmat.h:139
vm_float_t y
Definition vecmat.h:138
vm_float_t x
Definition vecmat.h:137
vm_float_t z
Definition vecmat.h:150
vm_float_t x
Definition vecmat.h:148
vm_float_t y
Definition vecmat.h:149
vm_float_t w
Definition vecmat.h:151
#define VECMAT_FMAX(a, b)
Definition vecmat.h:423
#define VECMAT_EPSILON
Definition vecmat.h:377
vm_float_t vec4_dot(vector4 a, vector4 b)
Computes the dot product of two vector4.
Definition vector4.c:388
#define VECMAT_FLOOR(x)
Definition vecmat.h:424
#define VECMAT_FMIN(a, b)
Definition vecmat.h:422
#define VECMAT_ROUND(x)
Definition vecmat.h:426
#define VECMAT_CEIL(x)
Definition vecmat.h:425
vm_float_t vec4_length(vector4 v)
Computes the length (magnitude) of a vector4.
Definition vector4.c:399
double vm_float_t
Definition vecmat.h:79
#define VECMAT_FABS(x)
Definition vecmat.h:413
void vm_cpu_init(void)
Definition dispatch.c:77
VECMAT_SCALAR_API void vec4_sub_ptr_scalar(vector4 *res, const vector4 *a, const vector4 *b)
Performs component-wise subtraction of two vector4.
Definition vector4_ptr.c:30
void vec4_to_vec3_ptr(vector3 *res, const vector4 *v)
Copies the x, y, z components from a vector4 to a vector3.
void vec4_div_ptr(vector4 *res, const vector4 *a, const vector4 *b)
void vec4_clamp_scalar_ptr(vector4 *res, const vector4 *v, const vm_float_t min, const vm_float_t max)
void vec4_add_scalar_ptr(vector4 *res, const vector4 *v, const vm_float_t s)
VECMAT_SCALAR_API void vec4_clamp_scalar_ptr_scalar(vector4 *res, const vector4 *v, const vm_float_t min, const vm_float_t max)
Clamps each component to the scalar range [min, max].
VECMAT_SCALAR_API void vec4_max_ptr_scalar(vector4 *res, const vector4 *a, const vector4 *b)
Performs component-wise maximum of two vector4.
void vec4_min_ptr(vector4 *res, const vector4 *a, const vector4 *b)
VECMAT_SCALAR_API void vec4_neg_ptr_scalar(vector4 *res, const vector4 *v)
Performs component-wise negation of a vector4.
Definition vector4_ptr.c:95
void vec4_floor_ptr(vector4 *res, const vector4 *v)
VECMAT_SCALAR_API void vec4_round_ptr_scalar(vector4 *res, const vector4 *v)
Performs component-wise rounding of a vector4 to the nearest integer.
void vec4_sign_ptr(vector4 *res, const vector4 *v)
VECMAT_SCALAR_API void vec4_saturate_ptr_scalar(vector4 *res, const vector4 *v)
Clamps each component to the range [0, 1].
void vec4_mul_scalar_ptr(vector4 *res, const vector4 *v, const vm_float_t s)
void vec4_homogenize_ptr(vector4 *res, const vector4 *v)
VECMAT_SCALAR_API void vec4_div_scalar_ptr_scalar(vector4 *res, const vector4 *v, const vm_float_t s)
Performs component-wise division of a vector4 by a scalar.
Definition vector4_ptr.c:62
void vec4_add_ptr(vector4 *res, const vector4 *a, const vector4 *b)
void vec4_max_ptr(vector4 *res, const vector4 *a, const vector4 *b)
VECMAT_SCALAR_API void vec4_homogenize_ptr_scalar(vector4 *res, const vector4 *v)
Homogenizes a vector4 by dividing its x, y, z components by w.
VECMAT_SCALAR_API void vec4_add_ptr_scalar(vector4 *res, const vector4 *a, const vector4 *b)
Performs component-wise addition of two vector4.
Definition vector4_ptr.c:15
void vec4_fract_ptr(vector4 *res, const vector4 *v)
VECMAT_SCALAR_API void vec4_mul_ptr_scalar(vector4 *res, const vector4 *a, const vector4 *b)
Performs component-wise multiplication of two vector4.
Definition vector4_ptr.c:81
VECMAT_SCALAR_API void vec4_abs_ptr_scalar(vector4 *res, const vector4 *v)
Performs component-wise absolute value on a vector4.
void vec4_abs_ptr(vector4 *res, const vector4 *v)
void vec4_saturate_ptr(vector4 *res, const vector4 *v)
VECMAT_SCALAR_API void vec4_sign_ptr_scalar(vector4 *res, const vector4 *v)
Computes the component-wise sign of a vector4.
void vec4_lerp_ptr(vector4 *res, const vector4 *a, const vector4 *b, const vm_float_t t)
VECMAT_SCALAR_API void vec4_normalize_ptr_scalar(vector4 *res, const vector4 *v)
Normalizes a vector4 by scaling its components to unit length.
void vec4_div_scalar_ptr(vector4 *res, const vector4 *v, const vm_float_t s)
VECMAT_SCALAR_API void vec4_floor_ptr_scalar(vector4 *res, const vector4 *v)
Performs component-wise floor operation on a vector4.
void vec4_project_ptr(vector4 *res, const vector4 *a, const vector4 *b)
Projects a onto b.
void vec4_ceil_ptr(vector4 *res, const vector4 *v)
void vec4_reject_ptr(vector4 *res, const vector4 *a, const vector4 *b)
Returns the component of a orthogonal to b.
void vec4_round_ptr(vector4 *res, const vector4 *v)
void vec4_neg_ptr(vector4 *res, const vector4 *v)
VECMAT_SCALAR_API void vec4_div_ptr_scalar(vector4 *res, const vector4 *a, const vector4 *b)
Divides two vectors component-wise.
VECMAT_SCALAR_API void vec4_clamp_ptr_scalar(vector4 *res, const vector4 *v, const vector4 *min, const vector4 *max)
Performs component-wise clamping of a vector4 to the specified min and max bounds.
void vec4_normalize_ptr(vector4 *res, const vector4 *v)
void vec4_sub_scalar_ptr(vector4 *res, const vector4 *v, const vm_float_t s)
VECMAT_SCALAR_API void vec4_mul_scalar_ptr_scalar(vector4 *res, const vector4 *v, const vm_float_t s)
Performs component-wise multiplication of a vector4 by a scalar.
Definition vector4_ptr.c:45
VECMAT_SCALAR_API void vec4_sub_scalar_ptr_scalar(vector4 *res, const vector4 *v, const vm_float_t s)
Subtracts a scalar from each component.
void vec4_slide_ptr(vector4 *res, const vector4 *v, const vector4 *normal)
Removes the component of v along normal.
VECMAT_SCALAR_API void vec4_min_ptr_scalar(vector4 *res, const vector4 *a, const vector4 *b)
Performs component-wise minimum of two vector4.
void vec4_mul_ptr(vector4 *res, const vector4 *a, const vector4 *b)
VECMAT_SCALAR_API void vec4_ceil_ptr_scalar(vector4 *res, const vector4 *v)
Performs component-wise ceiling of a vector4.
VECMAT_SCALAR_API void vec4_fract_ptr_scalar(vector4 *res, const vector4 *v)
Returns the fractional part of each component.
void vec4_sub_ptr(vector4 *res, const vector4 *a, const vector4 *b)
VECMAT_SCALAR_API void vec4_add_scalar_ptr_scalar(vector4 *res, const vector4 *v, const vm_float_t s)
Adds a scalar to each component.
VECMAT_SCALAR_API void vec4_lerp_ptr_scalar(vector4 *res, const vector4 *a, const vector4 *b, const vm_float_t t)
Performs linear interpolation between two vector4 with interpolation factor t.
void vec4_clamp_ptr(vector4 *res, const vector4 *v, const vector4 *min, const vector4 *max)