Skip to content

Commit

Permalink
common: math_utils: introduce an is_prime function
Browse files Browse the repository at this point in the history
  • Loading branch information
dzarukin authored and karturov committed Mar 27, 2024
1 parent 74a343b commit 4b72361
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/common/math_utils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2017-2023 Intel Corporation
* Copyright 2017-2024 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@
#ifndef COMMON_MATH_UTILS_HPP
#define COMMON_MATH_UTILS_HPP

#include <type_traits>

#include <math.h>
#include <stdint.h>

Expand All @@ -28,6 +30,22 @@ namespace dnnl {
namespace impl {
namespace math {

// Algorithm is picked from https://en.wikipedia.org/wiki/Primality_test
template <typename T>
inline bool is_prime(T n) {
static_assert(std::is_integral<T>::value == true, "Not an integral type");

if (n <= 1 || n % 2 == 0 || n % 3 == 0 || n % 5 == 0) return false;

const T sqrtn = static_cast<T>(std::sqrt(n));
// It is enough to check prime divisors up to `sqrt(n)`.
// All potential prime divisors are represented with `6*i + k` for k={1, 5}.
for (T i = 1; 6 * i + 5 <= sqrtn; i++) {
if ((n % (6 * i + 1) == 0) || (n % (6 * i + 5) == 0)) return false;
}
return true;
}

template <typename T>
inline T gcd(T a, T b) {
a = impl::nstl::abs(a);
Expand Down

0 comments on commit 4b72361

Please sign in to comment.