Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[Master] Added ::GCD and ::LCM: <numeric> contains GCD and LCM implementation #20583

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/operator/mshadow_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@
#include <cuda_fp16.h>
#endif

#define MXNET_HAS_GCD_LCM() 0
#if __cplusplus >= 201703L
#ifdef __has_gcd_lcm
#if __has_gcd_lcm(<numeric>)
#include <numeric>
#undef MXNET_HAS_GCD_LCM
#define MXNET_HAS_GCD_LCM() 1
#endif
#endif
#endif

namespace mxnet {
namespace op {
namespace mshadow_op {
Expand Down Expand Up @@ -1809,6 +1820,9 @@ struct gcd : public mxnet_op::tunable {
template <typename DType>
MSHADOW_XINLINE static typename enable_if<is_integral<DType>::value, DType>::type Map(DType a,
DType b) {
#if MXNET_HAS_GCD_LCM()
return std::gcd(a, b);
#else
// minus cases.
if (a < 0) {
a = -a;
Expand Down Expand Up @@ -1840,6 +1854,7 @@ struct gcd : public mxnet_op::tunable {
c = b;
}
return c;
#endif
}

template <typename DType>
Expand All @@ -1854,6 +1869,9 @@ struct lcm : public mxnet_op::tunable {
template <typename DType>
MSHADOW_XINLINE static typename enable_if<is_integral<DType>::value, DType>::type Map(DType a,
DType b) {
#if MXNET_HAS_GCD_LCM()
return std::lcm(a, b);
#else
// minus cases.
if (a < 0) {
a = -a;
Expand Down Expand Up @@ -1883,6 +1901,7 @@ struct lcm : public mxnet_op::tunable {
c = tmp_a / b * tmp_b;
}
return c;
#endif
}
template <typename DType>
MSHADOW_XINLINE static typename enable_if<!is_integral<DType>::value, DType>::type Map(DType a,
Expand Down