2015-06-30 09:47:59 +00:00
|
|
|
/*
|
|
|
|
* \brief Square root of integer values
|
|
|
|
* \date 2010-09-27
|
|
|
|
* \author Norman Feske
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2010-2017 Genode Labs GmbH
|
2015-06-30 09:47:59 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2015-06-30 09:47:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__NANO3D__SQRT_H_
|
2021-01-12 00:47:04 +00:00
|
|
|
#define _INCLUDE__NANO3D__SQRT_H_
|
2015-06-30 09:47:59 +00:00
|
|
|
|
|
|
|
namespace Nano3d {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate square root of an integer value
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T sqrt(T value)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Calculate square root using nested intervalls. The range of values
|
|
|
|
* is log(x) with x being the maximum value of type T. We narrow the
|
|
|
|
* result bit by bit starting with the most significant bit.
|
|
|
|
*/
|
|
|
|
T result = 0;
|
|
|
|
for (T i = sizeof(T)*8 / 2; i > 0; i--) {
|
|
|
|
T const bit = i - 1;
|
|
|
|
T const test = result + (1 << bit);
|
|
|
|
if (test*test <= value)
|
|
|
|
result = test;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 00:47:04 +00:00
|
|
|
#endif /* _INCLUDE__NANO3D__SQRT_H_ */
|