Skip to content

Commit 66c044e

Browse files
committed
replace remaining ternary operations with conditional_select calls
1 parent d1d7add commit 66c044e

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/MontyFullRange.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "hurchalla/modular_arithmetic/absolute_value_difference.h"
1919
#include "hurchalla/util/traits/ut_numeric_limits.h"
2020
#include "hurchalla/util/unsigned_multiply_to_hilo_product.h"
21+
#include "hurchalla/util/conditional_select.h"
2122
#include "hurchalla/util/compiler_macros.h"
2223
#include "hurchalla/modular_arithmetic/detail/clockwork_programming_by_contract.h"
2324
#include <type_traits>
@@ -233,7 +234,8 @@ class MontyFullRange final :
233234
T minu, subt;
234235
hc::REDC_incomplete(minu, subt, u_hi, u_lo, n_, BC::inv_n_, PTAG());
235236
T res = static_cast<T>(minu - subt);
236-
T subtrahend = (minu < subt) ? res : static_cast<T>(0);
237+
// T subtrahend = (minu < subt) ? res : static_cast<T>(0);
238+
T subtrahend = hc::conditional_select((minu < subt), res, static_cast<T>(0));
237239
SV result(res, subtrahend);
238240
return result;
239241
}
@@ -261,7 +263,9 @@ class MontyFullRange final :
261263
// via squareToMontgomeryValue
262264
HURCHALLA_FORCE_INLINE V getMontgomeryValue(SV sv) const
263265
{
264-
T nonneg_value = sv.get_subtrahend() != 0 ? sv.get() + n_ : sv.get();
266+
//T nonneg_value = sv.get_subtrahend() != 0 ? sv.get() + n_ : sv.get();
267+
T nonneg_value = ::hurchalla::conditional_select(
268+
(sv.get_subtrahend() != 0), sv.get() + n_, sv.get());
265269
return V(nonneg_value);
266270
}
267271

montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_pow_2kary/experimental_montgomery_pow_2kary.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,12 +1059,14 @@ if HURCHALLA_CPP17_CONSTEXPR (CODE_SECTION == 0) {
10591059
index = static_cast<size_t>(branchless_shift_right(n, shift)) & MASK;
10601060

10611061
HURCHALLA_REQUEST_UNROLL_LOOP for (size_t j=0; j<ARRAY_SIZE; ++j) {
1062-
V tmp = (index % 2 == 0) ? table[index/2][j] : result[j];
1062+
V tmp = result[j];
1063+
tmp.cmov((index % 2 == 0), table[index/2][j]);
10631064
result[j] = mf.template multiply<LowuopsTag>(tmp, result[j]);
10641065
}
10651066

10661067
HURCHALLA_REQUEST_UNROLL_LOOP for (size_t j=0; j<ARRAY_SIZE; ++j) {
1067-
V tmp = (index % 2 == 0) ? result[j] : table[index][j];
1068+
V tmp = table[index][j];
1069+
tmp.cmov((index % 2 == 0), result[j]);
10681070
result[j] = mf.template multiply<LowuopsTag>(tmp, result[j]);
10691071
}
10701072
}

montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_two_pow/experimental_montgomery_two_pow.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,8 +2465,7 @@ if HURCHALLA_CPP17_CONSTEXPR (CODE_SECTION == 0) {
24652465

24662466
U n_max = n[0];
24672467
HURCHALLA_REQUEST_UNROLL_LOOP for (size_t j=1; j<ARRAY_SIZE; ++j) {
2468-
if (n_max < n[j])
2469-
n_max = n[j];
2468+
n_max = (n_max < n[j]) ? n[j] : n_max;
24702469
}
24712470

24722471

montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/platform_specific/montgomery_pow.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ struct montgomery_pow {
4141
// Applied Handbook of Cryptography- http://cacr.uwaterloo.ca/hac/
4242
// See also: hurchalla/modular_arithmetic/detail/impl_modular_pow.h
4343
V mont_one = mf.getUnityValue();
44-
V result = (exponent & static_cast<T>(1)) ? base : mont_one;
44+
V result = mont_one;
45+
result.cmov((exponent & static_cast<T>(1)), base);
4546
while (exponent > static_cast<T>(1)) {
4647
exponent = static_cast<T>(exponent >> static_cast<T>(1));
4748
base = mf.template square<LowlatencyTag>(base);

montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/low_level_api/detail/platform_specific/ImplRedc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ struct RedcIncomplete {
289289
T v_mid = u_mid - mn_mid;
290290
TH u_hi_hi = static_cast<TH>(u_hi >> HALF_BITS);
291291
TH v_hi_hi = u_hi_hi - (u_mid < mn_mid);
292-
T moz = (u_hi_hi < (u_mid < mn_mid)) ? n : 0;
292+
// T moz = (u_hi_hi < (u_mid < mn_mid)) ? n : 0;
293+
T moz = ::hurchalla::conditional_select((u_hi_hi < (u_mid < mn_mid)), n, 0);
293294

294295
T v_hi = (static_cast<T>(v_hi_hi) << HALF_BITS) | (v_mid >> HALF_BITS);
295296
v_hi = v_hi + moz;
@@ -807,7 +808,8 @@ struct RedcStandard
807808
// By RedcIncomplete::call()'s Postcondition #1, we would have:
808809
T difference = static_cast<T>(minuend - subtrahend);
809810
bool ovf = (minuend < subtrahend);
810-
T result = (ovf) ? static_cast<T>(difference + n) : difference;
811+
// T result = (ovf) ? static_cast<T>(difference + n) : difference;
812+
T result = hc::conditional_select((ovf), static_cast<T>(difference + n), difference);
811813
#else
812814
// The #if section above is just a modular subtraction...
813815
// The most efficient way to compute it is to call our dedicated function:

0 commit comments

Comments
 (0)