genode/repos/gems/include/nano3d/sqrt.h
Norman Feske 259b127f96 Polygon drawing and rudimentary 3D routines
This patch adds two new painters located at gems/include/polygon_gfx.
Both painters draw convex polygons with an arbirary number of points.
The shaded-polygon painter interpolates the color and alpha values
whereas the textured-polygon painter applies a texture to the polygon.
The painters are accompanied by simplistic 3D routines located at
gems/include/nano3d/ and a corresponding example (gems/run/nano3d.run).
2015-07-07 19:48:04 +02:00

42 lines
930 B
C++

/*
* \brief Square root of integer values
* \date 2010-09-27
* \author Norman Feske
*/
/*
* Copyright (C) 2010-2015 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__NANO3D__SQRT_H_
#define _INCLUDE__NANO3D__SQRT_H__
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;
}
}
#endif /* _INCLUDE__NANO3D__SQRT_H__ */