Use std:: qualified math functions to avoid float-to-double promotion (#5088)

Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/5088

Fix 4 `performance-type-promotion-in-math-fn` lint warnings by using `std::floor`, `std::fabs`, and `std::pow` instead of the unqualified C versions. The unqualified versions promote `float` arguments to `double`, causing unnecessary precision loss and performance overhead. Also added missing `#include <cmath>` in `IndexIVFSpectralHash.cpp`.

Reviewed By: mnorris11

Differential Revision: D100576274

fbshipit-source-id: f491b1e9432a42f56a5660002f5b10ed0f594f4c
This commit is contained in:
Junjie Qi
2026-04-13 13:02:00 -07:00
committed by meta-codesync[bot]
parent 7cc63422d7
commit 3c19c52ca0
2 changed files with 4 additions and 3 deletions
+2 -1
View File
@@ -10,6 +10,7 @@
#include <faiss/IndexIVFSpectralHash.h>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <memory>
@@ -159,7 +160,7 @@ void binarize_with_freq(
memset(codes, 0, (nbit + 7) / 8);
for (size_t i = 0; i < nbit; i++) {
float xf = (x[i] - c[i]);
int64_t xi = int64_t(floor(xf * freq));
int64_t xi = int64_t(std::floor(xf * freq));
int64_t bit = xi & 1;
codes[i >> 3] |= bit << (i & 7);
}
+2 -2
View File
@@ -279,7 +279,7 @@ void LinearTransform::set_is_orthonormal() {
float v = ATA[i + j * d_out];
if (i == j)
v -= 1;
if (fabs(v) > eps) {
if (std::fabs(v) > eps) {
is_orthonormal = false;
}
}
@@ -773,7 +773,7 @@ void PCAMatrix::prepare_Ab() {
if (eigen_power != 0) {
float* ai = A.data();
for (int i = 0; i < d_out; i++) {
float factor = pow(eigenvalues[i] + epsilon, eigen_power);
float factor = std::pow(eigenvalues[i] + epsilon, eigen_power);
for (int j = 0; j < d_in; j++)
*ai++ *= factor;
}