Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sqlite-vec-diskann.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ int diskann_quantize_vector(
case VEC0_DISKANN_QUANTIZER_INT8: {
f32 step = (1.0f - (-1.0f)) / 255.0f;
for (size_t i = 0; i < dimensions; i++) {
((i8 *)out)[i] = (i8)(((src[i] - (-1.0f)) / step) - 128.0f);
// Saturate before the cast: converting a float outside [-128, 127] to a
// signed integer type is undefined behavior in C (and silently wraps to
// garbage). Inputs are expected to be normalized to [-1, 1]; clamp so
// out-of-range values quantize to the nearest representable int8.
const f32 raw = ((src[i] - (-1.0f)) / step) - 128.0f;
const f32 q = raw < -128.0f ? -128.0f : (raw > 127.0f ? 127.0f : raw);
((i8 *)out)[i] = (i8)q;
}
return SQLITE_OK;
}
Expand Down