Skip to content

Commit cf3012b

Browse files
Enrico Steffinlongohamishun
authored andcommitted
Fixed unqualified abs and fabs (homenc#340)
1 parent 65b5817 commit cf3012b

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/Ctxt.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ void Ctxt::mulIntFactor(long e)
265265
long bal_e = balRem(e, ptxtSpace);
266266
for (auto& part : parts)
267267
part *= bal_e;
268-
noiseBound *= abs(bal_e); // because every part was scaled by bal_e
268+
noiseBound *= std::abs(bal_e); // because every part was scaled by bal_e
269269
}
270270

271271
// Ciphertext maintenance
@@ -399,9 +399,9 @@ void Ctxt::modDownToSet(const IndexSet& s)
399399
NTL::conv<double>(NTL::conv<NTL::xdouble>(delta.rep[j]) / xdiff);
400400

401401
// sanity check: |fdelta[j]| <= ptxtSpace/2
402-
if (fabs(fdelta[j]) > double(ptxtSpace) / 2.0 + 0.0001) {
402+
if (std::fabs(fdelta[j]) > double(ptxtSpace) / 2.0 + 0.0001) {
403403
std::stringstream ss;
404-
ss << "\n***Bad modSwitch: diff =" << fabs(fdelta[j])
404+
ss << "\n***Bad modSwitch: diff =" << std::fabs(fdelta[j])
405405
<< ", ptxtSpace=" << ptxtSpace;
406406
throw RuntimeError(ss.str());
407407
}
@@ -771,7 +771,7 @@ void Ctxt::addConstant(const DoubleCRT& dcrt, double size)
771771
f = balRem(f, ptxtSpace);
772772
}
773773

774-
noiseBound += size * abs(f);
774+
noiseBound += size * std::abs(f);
775775

776776
IndexSet delta = dcrt.getIndexSet() / primeSet; // set minus
777777
if (f == 1 && empty(delta)) { // just add it
@@ -829,8 +829,10 @@ void Ctxt::addConstantCKKS(const DoubleCRT& dcrt,
829829
if (factor <= 0)
830830
conv(factor, getContext().ea->getCx().encodeScalingFactor() / size);
831831

832-
NTL::xdouble ratio = floor((ratFactor / factor) + 0.5); // round to integer
833-
double inaccuracy = abs(NTL::conv<double>(ratio * factor / ratFactor) - 1.0);
832+
NTL::xdouble ratio =
833+
NTL::floor((ratFactor / factor) + 0.5); // round to integer
834+
double inaccuracy =
835+
std::abs(NTL::conv<double>(ratio * factor / ratFactor) - 1.0);
834836

835837
// Check if you need to scale up to get target accuracy of 2^{-r}
836838
if ((inaccuracy * getContext().alMod.getPPowR()) > 1.0) {
@@ -954,7 +956,7 @@ void Ctxt::addConstantCKKS(std::pair<long, long> num)
954956
NTL::xdouble xb = NTL::to_xdouble(num.second); // denominator
955957

956958
NTL::xdouble ratio = floor((ratFactor / xb) + 0.5); // round to integer
957-
double inaccuracy = abs(NTL::conv<double>(ratio * xb / ratFactor) - 1.0);
959+
double inaccuracy = std::abs(NTL::conv<double>(ratio * xb / ratFactor) - 1.0);
958960
if ((inaccuracy * getContext().alMod.getPPowR()) > 1.0) {
959961
addSomePrimes(*this); // This increases ratFactor
960962
}
@@ -1041,7 +1043,7 @@ static NTL::xdouble NoiseNorm(NTL::xdouble noise1,
10411043
long e2,
10421044
long p)
10431045
{
1044-
return noise1 * abs(balRem(e1, p)) + noise2 * abs(balRem(e2, p));
1046+
return noise1 * std::abs(balRem(e1, p)) + noise2 * std::abs(balRem(e2, p));
10451047
}
10461048

10471049
// Add/subtract another ciphertext (depending on the negative flag)
@@ -1464,7 +1466,7 @@ void Ctxt::multByConstant(const NTL::ZZ& c)
14641466
FHE_TIMER_START;
14651467

14661468
if (isCKKS()) { // multiply by dividing the scaling factor
1467-
NTL::xdouble size = fabs(NTL::to_xdouble(c));
1469+
NTL::xdouble size = NTL::fabs(NTL::to_xdouble(c));
14681470
ptxtMag *= size;
14691471
ratFactor /= size;
14701472
if (c < 0)
@@ -1494,7 +1496,7 @@ void Ctxt::multByConstant(const NTL::ZZ& c)
14941496
return;
14951497

14961498
long cc = balRem(d, ptxtSpace);
1497-
noiseBound *= abs(cc);
1499+
noiseBound *= std::abs(cc);
14981500

14991501
// multiply all the parts by this constant
15001502
NTL::ZZ c_copy(cc);
@@ -1510,7 +1512,7 @@ void Ctxt::multByConstant(const NTL::ZZ& c)
15101512
FHE_TIMER_START;
15111513
15121514
if (isCKKS()) { // multiply by dividing the scaling factor
1513-
NTL::xdouble size = fabs(NTL::to_xdouble(c));
1515+
NTL::xdouble size = NTL::fabs(NTL::to_xdouble(c));
15141516
ptxtMag *= size;
15151517
ratFactor /= size;
15161518
if (c < 0)
@@ -1519,7 +1521,7 @@ void Ctxt::multByConstant(const NTL::ZZ& c)
15191521
}
15201522
// for BGV, need to do real multiplication
15211523
long cc = balRem(rem(c, ptxtSpace), ptxtSpace); // reduce modulo ptxt space
1522-
noiseBound *= abs(cc);
1524+
noiseBound *= std::abs(cc);
15231525
15241526
// multiply all the parts by this constant
15251527
NTL::ZZ c_copy(cc);
@@ -2161,9 +2163,9 @@ double Ctxt::rawModSwitch(std::vector<NTL::ZZX>& zzParts, long q) const
21612163

21622164
// sanity check: |c*q/Q - x| <= p^r/2
21632165
NTL::xdouble diff =
2164-
fabs(NTL::conv<NTL::xdouble>(c) * NTL::conv<NTL::xdouble>(q) /
2165-
NTL::conv<NTL::xdouble>(Q) -
2166-
NTL::conv<NTL::xdouble>(x));
2166+
NTL::fabs(NTL::conv<NTL::xdouble>(c) * NTL::conv<NTL::xdouble>(q) /
2167+
NTL::conv<NTL::xdouble>(Q) -
2168+
NTL::conv<NTL::xdouble>(x));
21672169
if (diff > NTL::conv<NTL::xdouble>(p2r) / 2.0 + 0.0001) {
21682170
std::stringstream ss;
21692171
ss << "\n***BAD rawModSwitch: diff=" << diff << ", p2r=" << p2r;

src/PAlgebra.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ PAlgebra::PAlgebra(long mm,
485485
for (long i = 1, p2i = pmodm; p2i != 1; i++, p2i = NTL::MulMod(p2i, pmodm, m))
486486
p_subgp[p2i] = i;
487487
for (long j : range(tmpOrds.size())) {
488-
tmpOrds[j] = abs(tmpOrds[j]);
488+
tmpOrds[j] = std::abs(tmpOrds[j]);
489489
// for backward compatibility, a user supplied
490490
// ords value could be negative, but we ignore that here.
491491
// For testing and debugging, we may want to not ignore this...

0 commit comments

Comments
 (0)