mirror of
https://github.com/genodelabs/genode.git
synced 2025-06-20 16:10:29 +00:00
Imported Genode release 11.11
This commit is contained in:
committed by
Christian Helmuth
parent
6bcc9aef0e
commit
da4e1feaa5
72
base/include/util/misc_math.h
Normal file
72
base/include/util/misc_math.h
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* \brief Commonly used math functions
|
||||
* \author Norman Feske
|
||||
* \date 2006-04-12
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006-2011 Genode Labs GmbH
|
||||
*
|
||||
* 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 {
|
||||
|
||||
template <typename T>
|
||||
T max(T v1, T v2) { return v1 > v2 ? v1 : v2; }
|
||||
|
||||
template <typename T>
|
||||
T min(T v1, T v2) { return v1 < v2 ? v1 : v2; }
|
||||
|
||||
template <typename T>
|
||||
T abs(T value) { return value >= 0 ? value : -value; }
|
||||
|
||||
|
||||
/**
|
||||
* Alignment to the power of two
|
||||
*/
|
||||
template <typename T>
|
||||
static inline T _align_mask(T align) {
|
||||
return ~((1 << align) - 1); }
|
||||
|
||||
template <typename T>
|
||||
static inline T _align_offset(T align) {
|
||||
return (1 << align) - 1; }
|
||||
|
||||
template <typename T>
|
||||
static inline T align_addr(T addr, int align) {
|
||||
return (addr + _align_offset(align)) & _align_mask(align); }
|
||||
|
||||
|
||||
/**
|
||||
* 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)
|
||||
if ((1 << i) & value) return i;
|
||||
|
||||
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_ */
|
Reference in New Issue
Block a user