Skip to content

Commit d9b84c2

Browse files
author
Vadim Paretsky
committed
[OpenMP] remove usage of std::abs in the new loop collapse support code
On some platforms, std::abs may inadvertently pull in a math library. This patch replaces its use in the new loop collapse code with a no thrills in-situ implementation. Differential Revision: https://reviews.llvm.org/D150882
1 parent 3b78065 commit d9b84c2

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

openmp/runtime/src/kmp_collapse.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,25 @@
1818
#include "kmp_str.h"
1919
#include "kmp_collapse.h"
2020

21-
#include <cmath>
22-
2321
#if OMPT_SUPPORT
2422
#include "ompt-specific.h"
2523
#endif
2624

2725
// OMPTODO: different style of comments (see kmp_sched)
2826
// OMPTODO: OMPT/OMPD
2927

28+
// avoid inadevertently using a library based abs
29+
template <typename T> T __kmp_abs(const T val) {
30+
return (val < 0) ? -val: val;
31+
}
32+
kmp_uint32 __kmp_abs(const kmp_uint32 val) { return val; }
33+
kmp_uint64 __kmp_abs(const kmp_uint64 val) { return val; }
34+
3035
//----------------------------------------------------------------------------
3136
// Common functions for working with rectangular and non-rectangular loops
3237
//----------------------------------------------------------------------------
3338

34-
template <typename T> int sign(T val) { return (T(0) < val) - (val < T(0)); }
39+
template <typename T> int __kmp_sign(T val) { return (T(0) < val) - (val < T(0)); }
3540

3641
//----------Loop canonicalization---------------------------------------------
3742

@@ -131,7 +136,7 @@ kmp_loop_nest_iv_t kmp_calculate_trip_count_XX(
131136
// kmp_loop_nest_iv_t anyway
132137
bounds->trip_count =
133138
static_cast<kmp_loop_nest_iv_t>(bounds->ub0 - bounds->lb0) /
134-
std::abs(bounds->step) +
139+
__kmp_abs(bounds->step) +
135140
1;
136141
}
137142
} else if (bounds->comparison == comparison_t::comp_greater_or_eq) {
@@ -144,7 +149,7 @@ kmp_loop_nest_iv_t kmp_calculate_trip_count_XX(
144149
// kmp_loop_nest_iv_t anyway
145150
bounds->trip_count =
146151
static_cast<kmp_loop_nest_iv_t>(bounds->lb0 - bounds->ub0) /
147-
std::abs(bounds->step) +
152+
__kmp_abs(bounds->step) +
148153
1;
149154
}
150155
} else {
@@ -658,16 +663,16 @@ void kmp_calc_new_bounds_XX(
658663
T old_lb1 = bbounds.lb1;
659664
T old_ub1 = bbounds.ub1;
660665

661-
if (sign(old_lb1) != sign(old_ub1)) {
666+
if (__kmp_sign(old_lb1) != __kmp_sign(old_ub1)) {
662667
// With this shape we can adjust to a rectangle:
663668
bbounds.lb1 = 0;
664669
bbounds.ub1 = 0;
665670
} else {
666671
// get upper and lower bounds to be parallel
667672
// with values in the old range.
668-
// Note: std::abs didn't work here.
669-
if (((sign(old_lb1) == -1) && (old_lb1 < old_ub1)) ||
670-
((sign(old_lb1) == 1) && (old_lb1 > old_ub1))) {
673+
// Note: abs didn't work here.
674+
if (((old_lb1 < 0) && (old_lb1 < old_ub1)) ||
675+
((old_lb1 > 0) && (old_lb1 > old_ub1))) {
671676
bbounds.lb1 = old_ub1;
672677
} else {
673678
bbounds.ub1 = old_lb1;
@@ -804,13 +809,13 @@ kmp_calc_number_of_iterations_XX(const bounds_infoXX_template<T> *bounds,
804809
iterations =
805810
(static_cast<T>(original_ivs[ind]) - bounds->lb0 -
806811
bounds->lb1 * static_cast<T>(original_ivs[bounds->outer_iv])) /
807-
std::abs(bounds->step);
812+
__kmp_abs(bounds->step);
808813
} else {
809814
KMP_DEBUG_ASSERT(bounds->comparison == comparison_t::comp_greater_or_eq);
810815
iterations = (bounds->lb0 +
811816
bounds->lb1 * static_cast<T>(original_ivs[bounds->outer_iv]) -
812817
static_cast<T>(original_ivs[ind])) /
813-
std::abs(bounds->step);
818+
__kmp_abs(bounds->step);
814819
}
815820

816821
return iterations;

0 commit comments

Comments
 (0)