Skip to content

Commit ce8c042

Browse files
junjieqifacebook-github-bot
authored andcommitted
Use std:: qualified math functions to avoid float-to-double promotion (#5088)
Summary: 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`. Differential Revision: D100576274
1 parent d3747e2 commit ce8c042

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

faiss/IndexIVFSpectralHash.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <faiss/IndexIVFSpectralHash.h>
1111

1212
#include <algorithm>
13+
#include <cmath>
1314
#include <cstdint>
1415
#include <memory>
1516

@@ -159,7 +160,7 @@ void binarize_with_freq(
159160
memset(codes, 0, (nbit + 7) / 8);
160161
for (size_t i = 0; i < nbit; i++) {
161162
float xf = (x[i] - c[i]);
162-
int64_t xi = int64_t(floor(xf * freq));
163+
int64_t xi = int64_t(std::floor(xf * freq));
163164
int64_t bit = xi & 1;
164165
codes[i >> 3] |= bit << (i & 7);
165166
}

faiss/VectorTransform.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ void LinearTransform::set_is_orthonormal() {
279279
float v = ATA[i + j * d_out];
280280
if (i == j)
281281
v -= 1;
282-
if (fabs(v) > eps) {
282+
if (std::fabs(v) > eps) {
283283
is_orthonormal = false;
284284
}
285285
}
@@ -773,7 +773,7 @@ void PCAMatrix::prepare_Ab() {
773773
if (eigen_power != 0) {
774774
float* ai = A.data();
775775
for (int i = 0; i < d_out; i++) {
776-
float factor = pow(eigenvalues[i] + epsilon, eigen_power);
776+
float factor = std::pow(eigenvalues[i] + epsilon, eigen_power);
777777
for (int j = 0; j < d_in; j++)
778778
*ai++ *= factor;
779779
}

0 commit comments

Comments
 (0)