Sprout/sprout/cstdlib/abs.hpp

68 lines
1.7 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011 RiSK (sscrisk)
https://github.com/sscrisk/CEL---ConstExpr-Library
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
2012-04-01 13:15:09 +00:00
#ifndef SPROUT_CSTDLIB_ABS_HPP
#define SPROUT_CSTDLIB_ABS_HPP
#include <type_traits>
2012-04-01 13:15:09 +00:00
#include <sprout/config.hpp>
#include <sprout/type_traits/std_type_traits.hpp>
2012-04-01 13:15:09 +00:00
namespace sprout {
2013-03-22 05:24:19 +00:00
// 7.20.6.1 abs<62>Clabs<62>C<EFBFBD>y<EFBFBD><79> llabs <20>֐<EFBFBD>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR int
abs(int j) {
2012-04-01 13:15:09 +00:00
return j < 0 ? -j : j;
}
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR long
labs(long j) {
2012-04-01 13:15:09 +00:00
return j < 0 ? -j : j;
}
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR long long
llabs(long long j) {
2012-04-01 13:15:09 +00:00
return j < 0 ? -j : j;
}
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR long
abs(long j) {
2012-05-05 06:57:55 +00:00
return sprout::labs(j);
2012-04-01 13:15:09 +00:00
}
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR long long
abs(long long j) {
2012-05-05 06:57:55 +00:00
return sprout::llabs(j);
2012-04-01 13:15:09 +00:00
}
2012-05-26 10:19:08 +00:00
namespace {
2012-12-17 11:10:17 +00:00
template<typename IntType>
inline SPROUT_CONSTEXPR typename std::enable_if<
std::is_integral<IntType>::value && sprout::is_signed<IntType>::value,
2012-12-17 11:10:17 +00:00
IntType
>::type
2012-10-05 15:58:56 +00:00
abs(IntType j) {
2012-05-26 10:19:08 +00:00
return j < 0 ? -j : j;
}
2012-12-17 11:10:17 +00:00
template<typename IntType>
inline SPROUT_CONSTEXPR typename std::enable_if<
std::is_integral<IntType>::value && sprout::is_unsigned<IntType>::value,
2012-12-17 11:10:17 +00:00
IntType
>::type
2012-10-05 15:58:56 +00:00
abs(IntType j) {
2012-05-26 10:19:08 +00:00
return j;
}
} // anonymous-namespace
2012-04-01 13:15:09 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_CSTDLIB_ABS_HPP