2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Commonly used math functions
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2006-04-12
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 20:44:47 +00:00
|
|
|
* Copyright (C) 2006-2013 Genode Labs GmbH
|
2011-12-22 15:19:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__UTIL__MISC_MATH_H_
|
|
|
|
#define _INCLUDE__UTIL__MISC_MATH_H_
|
|
|
|
|
|
|
|
namespace Genode {
|
|
|
|
|
2012-01-09 20:46:32 +00:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
T1 max(T1 v1, T2 v2) { return v1 > v2 ? v1 : v2; }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2012-01-09 20:46:32 +00:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
T1 min(T1 v1, T2 v2) { return v1 < v2 ? v1 : v2; }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T abs(T value) { return value >= 0 ? value : -value; }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alignment to the power of two
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2015-06-26 09:31:29 +00:00
|
|
|
static constexpr T _align_mask(T align) {
|
|
|
|
return ~(((T)1 << align) - (T)1); }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2015-06-26 09:31:29 +00:00
|
|
|
static constexpr T _align_offset(T align) {
|
|
|
|
return ((T)1 << align) - (T)1; }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2015-06-26 09:31:29 +00:00
|
|
|
static constexpr T align_addr(T addr, int align) {
|
2013-05-07 14:31:41 +00:00
|
|
|
return (addr + _align_offset((T)align)) & _align_mask((T)align); }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LOG2
|
|
|
|
*
|
|
|
|
* Scan for most-significant set bit.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static inline T log2(T value)
|
|
|
|
{
|
|
|
|
if (!value) return -1;
|
|
|
|
for (int i = 8 * sizeof(value) - 1; i >= 0; --i)
|
2012-09-21 07:34:31 +00:00
|
|
|
if (((T)1 << i) & value) return i;
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Align value to next machine-word boundary
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline T align_natural(T value)
|
|
|
|
{
|
|
|
|
T mask = sizeof(long) - 1;
|
|
|
|
return (value + mask) & ~mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__UTIL__MISC_MATH_H_ */
|