Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vector4i_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 <stdlib.h>
6#include <vecmat.h>
7
15void vec4i_add_ptr(vector4i *res, const vector4i *a, const vector4i *b)
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
31void vec4i_sub_ptr(vector4i *res, const vector4i *a, const vector4i *b)
32{
33 res->x = a->x - b->x;
34 res->y = a->y - b->y;
35 res->z = a->z - b->z;
36 res->w = a->w - b->w;
37}
38
47void vec4i_mul_scalar_ptr(vector4i *res, const vector4i *v, const vm_int_t s)
48{
49 res->x = v->x * s;
50 res->y = v->y * s;
51 res->z = v->z * s;
52 res->w = v->w * s;
53}
54
65void vec4i_div_scalar_ptr(vector4i *res, const vector4i *v, const vm_int_t s)
66{
67 if (s == 0) {
68 res->x = 0;
69 res->y = 0;
70 res->z = 0;
71 res->w = 0;
72 return;
73 }
74 res->x = v->x / s;
75 res->y = v->y / s;
76 res->z = v->z / s;
77 res->w = v->w / s;
78}
79
87void vec4i_mul_ptr(vector4i *res, const vector4i *a, const vector4i *b)
88{
89 res->x = a->x * b->x;
90 res->y = a->y * b->y;
91 res->z = a->z * b->z;
92 res->w = a->w * b->w;
93}
94
101void vec4i_neg_ptr(vector4i *res, const vector4i *v)
102{
103 res->x = -v->x;
104 res->y = -v->y;
105 res->z = -v->z;
106 res->w = -v->w;
107}
108
116void vec4i_abs_ptr(vector4i *res, const vector4i *v)
117{
118 res->x = abs(v->x);
119 res->y = abs(v->y);
120 res->z = abs(v->z);
121 res->w = abs(v->w);
122}
123
134{
135 const vm_float_t len = vec4i_length(*v);
136 if (len == 0.0f) {
137 *res = *v;
138 return;
139 }
140
141 res->x = (vm_int_t)(v->x / len);
142 res->y = (vm_int_t)(v->y / len);
143 res->z = (vm_int_t)(v->z / len);
144 res->w = (vm_int_t)(v->w / len);
145}
146
155void vec4i_min_ptr(vector4i *res, const vector4i *a, const vector4i *b)
156{
157 res->x = a->x < b->x ? a->x : b->x;
158 res->y = a->y < b->y ? a->y : b->y;
159 res->z = a->z < b->z ? a->z : b->z;
160 res->w = a->w < b->w ? a->w : b->w;
161}
162
171void vec4i_max_ptr(vector4i *res, const vector4i *a, const vector4i *b)
172{
173 res->x = a->x > b->x ? a->x : b->x;
174 res->y = a->y > b->y ? a->y : b->y;
175 res->z = a->z > b->z ? a->z : b->z;
176 res->w = a->w > b->w ? a->w : b->w;
177}
178
186void vec4i_sign_ptr(vector4i *res, const vector4i *v)
187{
188 res->x = v->x > 0 ? 1 : v->x < 0 ? -1 : 0;
189 res->y = v->y > 0 ? 1 : v->y < 0 ? -1 : 0;
190 res->z = v->z > 0 ? 1 : v->z < 0 ? -1 : 0;
191 res->w = v->w > 0 ? 1 : v->w < 0 ? -1 : 0;
192}
193
203void vec4i_lerp_ptr(vector4i *res, const vector4i *a, const vector4i *b, const vm_float_t t)
204{
205 const vm_float_t clamped_t = (t < 0.0f) ? 0.0f : (t > 1.0f) ? 1.0f : t;
206
207 res->x = (vm_int_t)(a->x + (b->x - a->x) * clamped_t);
208 res->y = (vm_int_t)(a->y + (b->y - a->y) * clamped_t);
209 res->z = (vm_int_t)(a->z + (b->z - a->z) * clamped_t);
210 res->w = (vm_int_t)(a->w + (b->w - a->w) * clamped_t);
211}
212
222void vec4i_clamp_ptr(vector4i *res, const vector4i *v, const vector4i *min, const vector4i *max)
223{
224 res->x = v->x < min->x ? min->x : (v->x > max->x ? max->x : v->x);
225 res->y = v->y < min->y ? min->y : (v->y > max->y ? max->y : v->y);
226 res->z = v->z < min->z ? min->z : (v->z > max->z ? max->z : v->z);
227 res->w = v->w < min->w ? min->w : (v->w > max->w ? max->w : v->w);
228}
229
238{
239 res->x = v->x;
240 res->y = v->y;
241 res->z = v->z;
242}
243
244// Floor division: toward -inf; returns 0 if b == 0.
245static vm_int_t vm_div_floor4(const vm_int_t a, const vm_int_t b)
246{
247 if (b == 0) {
248 return 0;
249 }
250 const vm_int_t q = a / b;
251 const vm_int_t r = a % b;
252 if (r != 0 && ((a < 0) != (b < 0))) {
253 return q - 1;
254 }
255 return q;
256}
257
258// Floor modulo: result in [0, b) for b > 0 (or (b, 0] for b < 0); 0 if b == 0.
259static vm_int_t vm_mod_floor4(const vm_int_t a, const vm_int_t b)
260{
261 if (b == 0) {
262 return 0;
263 }
264 const vm_int_t r = a % b;
265 if (r != 0 && ((a < 0) != (b < 0))) {
266 return r + b;
267 }
268 return r;
269}
270
278void vec4i_div_ptr(vector4i *res, const vector4i *a, const vector4i *b)
279{
280 res->x = (b->x == 0) ? 0 : a->x / b->x;
281 res->y = (b->y == 0) ? 0 : a->y / b->y;
282 res->z = (b->z == 0) ? 0 : a->z / b->z;
283 res->w = (b->w == 0) ? 0 : a->w / b->w;
284}
285
293void vec4i_add_scalar_ptr(vector4i *res, const vector4i *v, const vm_int_t s)
294{
295 res->x = v->x + s;
296 res->y = v->y + s;
297 res->z = v->z + s;
298 res->w = v->w + s;
299}
300
308void vec4i_sub_scalar_ptr(vector4i *res, const vector4i *v, const vm_int_t s)
309{
310 res->x = v->x - s;
311 res->y = v->y - s;
312 res->z = v->z - s;
313 res->w = v->w - s;
314}
315
323void vec4i_mod_ptr(vector4i *res, const vector4i *a, const vector4i *b)
324{
325 res->x = vm_mod_floor4(a->x, b->x);
326 res->y = vm_mod_floor4(a->y, b->y);
327 res->z = vm_mod_floor4(a->z, b->z);
328 res->w = vm_mod_floor4(a->w, b->w);
329}
330
338void vec4i_div_floor_ptr(vector4i *res, const vector4i *a, const vector4i *b)
339{
340 res->x = vm_div_floor4(a->x, b->x);
341 res->y = vm_div_floor4(a->y, b->y);
342 res->z = vm_div_floor4(a->z, b->z);
343 res->w = vm_div_floor4(a->w, b->w);
344}
345
353void vec4i_wrap_ptr(vector4i *res, const vector4i *v, const vector4i *period)
354{
355 res->x = vm_mod_floor4(v->x, period->x);
356 res->y = vm_mod_floor4(v->y, period->y);
357 res->z = vm_mod_floor4(v->z, period->z);
358 res->w = vm_mod_floor4(v->w, period->w);
359}
vm_int_t x
Definition vecmat.h:174
vm_int_t y
Definition vecmat.h:175
vm_int_t z
Definition vecmat.h:176
vm_int_t z
Definition vecmat.h:187
vm_int_t y
Definition vecmat.h:186
vm_int_t w
Definition vecmat.h:188
vm_int_t x
Definition vecmat.h:185
vm_float_t vec4i_length(vector4i v)
Computes the length (magnitude) of a vector4i.
Definition vector4i.c:339
double vm_float_t
Definition vecmat.h:79
int8_t vm_int_t
Definition vecmat.h:103
void vec4i_add_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Adds the components of two vector4i and stores the result in res.
void vec4i_to_vec3i_ptr(vector3i *res, const vector4i *v)
Copies the x, y, z components of a vector4i and stores the result in a vector3i.
void vec4i_div_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Divides two vectors component-wise.
void vec4i_mod_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Component-wise floor modulo of a by b.
void vec4i_abs_ptr(vector4i *res, const vector4i *v)
Computes the absolute value of each component of a vector4i and stores the result in res.
void vec4i_clamp_ptr(vector4i *res, const vector4i *v, const vector4i *min, const vector4i *max)
Clamps the components of a vector4i to the range [min, max] and stores the result in res.
void vec4i_max_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Computes the component-wise maximum of two vector4i and stores the result in res.
void vec4i_div_scalar_ptr(vector4i *res, const vector4i *v, const vm_int_t s)
Divides the components of a vector4i by a scalar and stores the result in res.
void vec4i_normalize_ptr(vector4i *res, const vector4i *v)
Normalizes a vector4i by dividing its components by the vector length and stores the result in res.
void vec4i_mul_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Multiplies the components of two vector4i and stores the result in res.
void vec4i_mul_scalar_ptr(vector4i *res, const vector4i *v, const vm_int_t s)
Multiplies the components of a vector4i by a scalar and stores the result in res.
void vec4i_neg_ptr(vector4i *res, const vector4i *v)
Negates the components of a vector4i and stores the result in res.
void vec4i_min_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Takes the component-wise minimum of two vector4i and stores the result in res.
void vec4i_sub_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Subtracts the components of the second vector from the first vector4i and stores the result in res.
void vec4i_lerp_ptr(vector4i *res, const vector4i *a, const vector4i *b, const vm_float_t t)
Linearly interpolates between two vector4i using an interpolation factor t (clamped to [0,...
void vec4i_add_scalar_ptr(vector4i *res, const vector4i *v, const vm_int_t s)
Adds a scalar to each component.
void vec4i_div_floor_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Component-wise floored division of a by b.
static vm_int_t vm_div_floor4(const vm_int_t a, const vm_int_t b)
void vec4i_sub_scalar_ptr(vector4i *res, const vector4i *v, const vm_int_t s)
Subtracts a scalar from each component.
void vec4i_sign_ptr(vector4i *res, const vector4i *v)
Computes the sign (-1, 0, or 1) of each component of a vector4i and stores the result in res.
static vm_int_t vm_mod_floor4(const vm_int_t a, const vm_int_t b)
void vec4i_wrap_ptr(vector4i *res, const vector4i *v, const vector4i *period)
Wraps each component of v into [0, period).