Effective implementation of algorithms (Master Thesis)
Effective and error-free implementation of algorithms
|
00001 #ifndef H_MATH_POWERMOD_MULTMOD_SIMPLE 00002 #define H_MATH_POWERMOD_MULTMOD_SIMPLE 00003 00004 #include "utils/static_assert/static_assert.h" 00005 #include "utils/preconditions/preconditions.h" 00006 #include <limits> 00007 #include "utils/macros/unused.h" 00008 00009 namespace math { 00010 namespace powermod { 00011 00012 class MultmodSimple { 00013 typedef int BaseType; 00014 typedef long long DoubleType; 00015 STATIC_ASSERT(std::numeric_limits<BaseType>::digits * 2 <= 00016 std::numeric_limits<DoubleType>::digits, 00017 "The long long is not enough for temporary " 00018 "computations"); 00019 00020 public: 00021 template <typename T> 00022 static T max_argument(T UNUSED(x)) { 00023 if (std::numeric_limits<BaseType>::digits > 00024 std::numeric_limits<T>::digits) { 00025 return std::numeric_limits<T>::max(); 00026 } else { 00027 return std::numeric_limits<BaseType>::max(); 00028 } 00029 } 00030 00031 static BaseType multmod(BaseType a, BaseType b, BaseType modulo) { 00032 Preconditions::check(modulo > 0, "Modulo should be positive."); 00033 Preconditions::checkRange(a, 0, modulo); 00034 Preconditions::checkRange(b, 0, modulo); 00035 00036 return ((DoubleType) a * (DoubleType) b) % (DoubleType) modulo; 00037 } 00038 }; 00039 00040 } // namespace powermod 00041 } // namespace math 00042 00043 #endif