MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_rsq_encode

Function cbm_rsq_encode

src/semantic/rotsq.c:60–112  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

58/* ── Encode ─────────────────────────────────────────────────────────── */
59
60void cbm_rsq_encode(const float *v, cbm_rsq_code_t *out) {
61 rsq_init_diag();
62
63 float rot[CBM_RSQ_DIM];
64 for (int d = 0; d < CBM_RSQ_IN_DIM; d++) {
65 rot[d] = v[d] * g_rsq_diag[d];
66 }
67 for (int d = CBM_RSQ_IN_DIM; d < CBM_RSQ_DIM; d++) {
68 rot[d] = 0.0F;
69 }
70 rsq_fwht(rot);
71 /* Normalize the transform so the rotation is orthonormal (H/√D): keeps
72 * the coordinates in a data-independent range and makes the estimated IP
73 * directly comparable to the pre-rotation IP. */
74 const float inv_sqrt_d = 1.0F / 32.0F; /* 1/√1024 */
75 float lo = rot[0] * inv_sqrt_d;
76 float hi = lo;
77 for (int d = 0; d < CBM_RSQ_DIM; d++) {
78 rot[d] *= inv_sqrt_d;
79 if (rot[d] < lo) {
80 lo = rot[d];
81 }
82 if (rot[d] > hi) {
83 hi = rot[d];
84 }
85 }
86
87 /* Per-vector scalar quantization over [lo, hi] at CBM_RSQ_BITS. */
88 float range = hi - lo;
89 float step = range > 0.0F ? range / (float)CBM_RSQ_LEVELS : 1.0F;
90 out->offset = lo;
91 out->scale = step;
92
93 int32_t sum = 0;
94 memset(out->codes, 0, sizeof(out->codes));
95 for (int d = 0; d < CBM_RSQ_DIM; d++) {
96 float q = (rot[d] - lo) / step;
97 int32_t c = (int32_t)(q + 0.5F);
98 if (c < 0) {
99 c = 0;
100 }
101 if (c > CBM_RSQ_LEVELS) {
102 c = CBM_RSQ_LEVELS;
103 }
104 sum += c;
105 if (d & 1) {
106 out->codes[d >> 1] |= (uint8_t)(c << 4);
107 } else {
108 out->codes[d >> 1] |= (uint8_t)c;
109 }
110 }
111 out->code_sum = sum;
112}
113
114/* ── Estimated inner product from two codes ─────────────────────────── */
115

Callers 4

vec_build_workerFunction · 0.85
collect_workerFunction · 0.85
test_semantic.cFile · 0.85

Calls 2

rsq_init_diagFunction · 0.85
rsq_fwhtFunction · 0.85

Tested by 1