| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2026 Tiger Data, Inc. | ||
| 3 | * Licensed under the PostgreSQL License. See LICENSE for details. | ||
| 4 | * | ||
| 5 | * vec32.c - Vector operations | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <math.h> | ||
| 9 | #include <string.h> | ||
| 10 | |||
| 11 | #include "core/memory.h" | ||
| 12 | #include "types/vec32.h" | ||
| 13 | |||
| 14 | /* (No extern vtable — inline vtable in vec32.h, dispatch via VecType) | ||
| 15 | */ | ||
| 16 | |||
| 17 | /* ---------------------------------------------------------------- | ||
| 18 | * Vec32 lifecycle | ||
| 19 | * ---------------------------------------------------------------- */ | ||
| 20 | |||
| 21 | Vec32 * | ||
| 22 | 30 | vec32_create(Dimension dim) | |
| 23 | { | ||
| 24 |
4/4✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 26 times.
|
30 | if (dim == 0 || dim > VEC32_MAX_DIM) |
| 25 | 4 | return NULL; | |
| 26 | |||
| 27 | 26 | size_t size = VEC32_SIZE(dim); | |
| 28 | 26 | Vec32 *v = vs_alloc(size); | |
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (v == NULL) |
| 30 | ✗ | return NULL; | |
| 31 | |||
| 32 | 26 | VS_SET_VARSIZE(v, size); | |
| 33 | 26 | v->dim = (int16_t)dim; | |
| 34 | 26 | v->unused = 0; | |
| 35 | |||
| 36 | 26 | return v; | |
| 37 | } | ||
| 38 | |||
| 39 | Vec32 * | ||
| 40 | 4 | vec32_copy(const Vec32 *src) | |
| 41 | { | ||
| 42 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (src == NULL) |
| 43 | 2 | return NULL; | |
| 44 | |||
| 45 | 2 | Vec32 *dst = vec32_create((Dimension)src->dim); | |
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (dst == NULL) |
| 47 | ✗ | return NULL; | |
| 48 | |||
| 49 | 2 | memcpy(dst->x, src->x, (size_t)src->dim * sizeof(float)); | |
| 50 | 2 | return dst; | |
| 51 | } | ||
| 52 | |||
| 53 | void | ||
| 54 | 26 | vec32_free(Vec32 *v) | |
| 55 | { | ||
| 56 | 26 | vs_free(v); | |
| 57 | 26 | } | |
| 58 | |||
| 59 | void | ||
| 60 | 16 | vec32_set(Vec32 *v, const float *values) | |
| 61 | { | ||
| 62 | 16 | memcpy(v->x, values, (size_t)v->dim * sizeof(float)); | |
| 63 | 16 | } | |
| 64 | |||
| 65 | void | ||
| 66 | 4 | vec32_zero(Vec32 *v) | |
| 67 | { | ||
| 68 | 4 | memset(v->x, 0, (size_t)v->dim * sizeof(float)); | |
| 69 | 4 | } | |
| 70 | |||
| 71 | void | ||
| 72 | 2 | vec32_fill(Vec32 *v, float value) | |
| 73 | { | ||
| 74 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
|
8 | for (int16_t i = 0; i < v->dim; i++) |
| 75 | 6 | v->x[i] = value; | |
| 76 | 2 | } | |
| 77 | |||
| 78 | float | ||
| 79 | 10 | vec32_dot(const Vec32 *a, const Vec32 *b) | |
| 80 | { | ||
| 81 | 10 | float sum = 0.0f; | |
| 82 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
|
40 | for (int16_t i = 0; i < a->dim; i++) |
| 83 | 30 | sum += a->x[i] * b->x[i]; | |
| 84 | 10 | return sum; | |
| 85 | } | ||
| 86 | |||
| 87 | float | ||
| 88 | 8 | vec32_norm(const Vec32 *v) | |
| 89 | { | ||
| 90 | 8 | return sqrtf(vec32_dot(v, v)); | |
| 91 | } | ||
| 92 | |||
| 93 | void | ||
| 94 | 4 | vec32_normalize(Vec32 *v) | |
| 95 | { | ||
| 96 | 4 | float norm = vec32_norm(v); | |
| 97 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (norm > 0.0f) |
| 98 | { | ||
| 99 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
|
8 | for (int16_t i = 0; i < v->dim; i++) |
| 100 | 6 | v->x[i] /= norm; | |
| 101 | } | ||
| 102 | 4 | } | |
| 103 |