Sprout/sprout/cstdlib/abs.hpp

58 lines
1.2 KiB
C++
Raw Normal View History

2012-04-01 22:15:09 +09:00
#ifndef SPROUT_CSTDLIB_ABS_HPP
#define SPROUT_CSTDLIB_ABS_HPP
#include <type_traits>
2012-04-01 22:15:09 +09:00
#include <sprout/config.hpp>
namespace sprout {
// Copyright (C) 2011 RiSK (sscrisk)
// 7.20.6.1 abs<62>Clabs<62>C<EFBFBD>y<EFBFBD><79> llabs <20>֐<EFBFBD>
2012-10-06 00:58:56 +09:00
inline SPROUT_CONSTEXPR int
abs(int j) {
2012-04-01 22:15:09 +09:00
return j < 0 ? -j : j;
}
2012-10-06 00:58:56 +09:00
inline SPROUT_CONSTEXPR long
labs(long j) {
2012-04-01 22:15:09 +09:00
return j < 0 ? -j : j;
}
2012-10-06 00:58:56 +09:00
inline SPROUT_CONSTEXPR long long
llabs(long long j) {
2012-04-01 22:15:09 +09:00
return j < 0 ? -j : j;
}
2012-10-06 00:58:56 +09:00
inline SPROUT_CONSTEXPR long
abs(long j) {
2012-05-05 15:57:55 +09:00
return sprout::labs(j);
2012-04-01 22:15:09 +09:00
}
2012-10-06 00:58:56 +09:00
inline SPROUT_CONSTEXPR long long
abs(long long j) {
2012-05-05 15:57:55 +09:00
return sprout::llabs(j);
2012-04-01 22:15:09 +09:00
}
2012-05-26 19:19:08 +09:00
namespace {
2012-12-17 20:10:17 +09:00
template<typename IntType>
inline SPROUT_CONSTEXPR typename std::enable_if<
std::is_integral<IntType>::value && std::is_signed<IntType>::value,
IntType
>::type
2012-10-06 00:58:56 +09:00
abs(IntType j) {
2012-05-26 19:19:08 +09:00
return j < 0 ? -j : j;
}
2012-12-17 20:10:17 +09:00
template<typename IntType>
inline SPROUT_CONSTEXPR typename std::enable_if<
std::is_integral<IntType>::value && std::is_unsigned<IntType>::value,
IntType
>::type
2012-10-06 00:58:56 +09:00
abs(IntType j) {
2012-05-26 19:19:08 +09:00
return j;
}
} // anonymous-namespace
2012-04-01 22:15:09 +09:00
} // namespace sprout
#endif // #ifndef SPROUT_CSTDLIB_ABS_HPP