| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2026 Tiger Data, Inc. | ||
| 3 | * Licensed under the PostgreSQL License. See LICENSE for details. | ||
| 4 | * | ||
| 5 | * vecops.h - General vector operations with SIMD dispatch | ||
| 6 | * | ||
| 7 | * Provides optimized implementations of common vector operations used | ||
| 8 | * throughout the codebase (quantization, clustering, distance computation). | ||
| 9 | */ | ||
| 10 | |||
| 11 | #ifndef VS_VECOPS_H | ||
| 12 | #define VS_VECOPS_H | ||
| 13 | |||
| 14 | #include "core/types.h" | ||
| 15 | |||
| 16 | /* | ||
| 17 | * Dot product: sum(a[i] * b[i]) | ||
| 18 | * | ||
| 19 | * Returns the inner product of two vectors. | ||
| 20 | */ | ||
| 21 | float vs_dot_product(const float *a, const float *b, Dimension dim); | ||
| 22 | |||
| 23 | /* | ||
| 24 | * L2 norm squared: sum(v[i]^2) | ||
| 25 | * | ||
| 26 | * Returns ||v||^2. Use vs_l2_norm() if you need the actual norm. | ||
| 27 | */ | ||
| 28 | float vs_l2_norm_squared(const float *v, Dimension dim); | ||
| 29 | |||
| 30 | /* | ||
| 31 | * L2 norm: sqrt(sum(v[i]^2)) | ||
| 32 | * | ||
| 33 | * Returns ||v||. Equivalent to sqrt(vs_l2_norm_squared(v, dim)). | ||
| 34 | */ | ||
| 35 | float vs_l2_norm(const float *v, Dimension dim); | ||
| 36 | |||
| 37 | /* | ||
| 38 | * Vector sum: sum(v[i]) | ||
| 39 | * | ||
| 40 | * Returns the sum of all elements. | ||
| 41 | */ | ||
| 42 | float vec32_sum(const float *v, Dimension dim); | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Vector subtraction: out[i] = a[i] - b[i] | ||
| 46 | * | ||
| 47 | * Computes element-wise difference. Output may alias input. | ||
| 48 | */ | ||
| 49 | void vec32_sub(const float *a, const float *b, float *out, Dimension dim); | ||
| 50 | |||
| 51 | /* | ||
| 52 | * Vector addition: out[i] = a[i] + b[i] | ||
| 53 | * | ||
| 54 | * Computes element-wise sum. Output may alias input. | ||
| 55 | */ | ||
| 56 | void vec32_add(const float *a, const float *b, float *out, Dimension dim); | ||
| 57 | |||
| 58 | /* | ||
| 59 | * Vector scale: out[i] = v[i] * scalar | ||
| 60 | * | ||
| 61 | * Multiplies each element by a scalar. Output may alias input. | ||
| 62 | */ | ||
| 63 | void vec32_scale(const float *v, float scalar, float *out, Dimension dim); | ||
| 64 | |||
| 65 | /* | ||
| 66 | * Normalize a raw float vector to unit L2 length, in place. No-op for a | ||
| 67 | * zero vector. (vec32_normalize in vec32.h is the Vec32 form.) | ||
| 68 | */ | ||
| 69 | static inline void | ||
| 70 | 2021 | vs_l2_normalize(float *v, Dimension dim) | |
| 71 | { | ||
| 72 | 2021 | float norm = vs_l2_norm(v, dim); | |
| 73 |
1/2✓ Branch 0 taken 2021 times.
✗ Branch 1 not taken.
|
2021 | if (norm > 0.0f) |
| 74 | 2021 | vec32_scale(v, 1.0f / norm, v, dim); | |
| 75 | 2021 | } | |
| 76 | |||
| 77 | /* | ||
| 78 | * Vector mean: out[d] = mean of vectors[i][d] for all i | ||
| 79 | * | ||
| 80 | * Computes the element-wise mean of nvecs row-major vectors. | ||
| 81 | * Output must be preallocated with at least dim floats. | ||
| 82 | */ | ||
| 83 | void | ||
| 84 | vec32_mean(const float *vectors, uint32_t nvecs, Dimension dim, float *out); | ||
| 85 | |||
| 86 | /* | ||
| 87 | * Global mean for index metadata: the element-wise mean of the (leaf) | ||
| 88 | * centroids, L2-normalized when the metric is cosine so it lives on the | ||
| 89 | * unit sphere with the vectors it recenters. Every writer of the | ||
| 90 | * metapage's global mean must use this so the cosine normalization | ||
| 91 | * cannot be missed at one site. | ||
| 92 | */ | ||
| 93 | void vs_global_mean( | ||
| 94 | const float *centroids, | ||
| 95 | uint32_t ncentroids, | ||
| 96 | Dimension dim, | ||
| 97 | DistanceMetric metric, | ||
| 98 | float *out); | ||
| 99 | |||
| 100 | /* | ||
| 101 | * L2 distance squared: sum((a[i] - b[i])^2) | ||
| 102 | * | ||
| 103 | * Returns ||a - b||^2. Equivalent to vs_l2_norm_squared of the difference, | ||
| 104 | * but without needing a temporary buffer. | ||
| 105 | */ | ||
| 106 | float vs_l2_distance_squared(const float *a, const float *b, Dimension dim); | ||
| 107 | |||
| 108 | /* | ||
| 109 | * Initialize SIMD dispatch for vector operations. | ||
| 110 | * Called automatically on first use, but can be called explicitly | ||
| 111 | * for deterministic initialization timing. | ||
| 112 | */ | ||
| 113 | int vs_vecops_init(void); | ||
| 114 | |||
| 115 | /* | ||
| 116 | * Get name of the active SIMD implementation. | ||
| 117 | * Returns one of: "avx512", "avx2", "neon", "compiler" | ||
| 118 | */ | ||
| 119 | const char *vs_vecops_impl_name(void); | ||
| 120 | |||
| 121 | /* | ||
| 122 | * Force re-initialization of SIMD dispatch (for testing). | ||
| 123 | */ | ||
| 124 | void vs_vecops_force_reinit(void); | ||
| 125 | |||
| 126 | #endif /* VS_VECOPS_H */ | ||
| 127 |