summaryrefslogtreecommitdiff
path: root/navit/graphics.c
diff options
context:
space:
mode:
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>2009-08-25 21:04:33 +0000
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>2009-08-25 21:04:33 +0000
commit77b388f5d59547e670048a6b964a079f99380a0c (patch)
treeec1bc66e4a8841b6048058c8a639a5784eb21d2b /navit/graphics.c
parent9909e47a34ab3dd80fd500b414b81437c8b4d6cc (diff)
downloadnavit-77b388f5d59547e670048a6b964a079f99380a0c.tar.gz
Fix:Core:Improved int_sqrt
git-svn-id: http://svn.code.sf.net/p/navit/code/trunk/navit@2530 ffa7fe5e-494d-0410-b361-a75ebd5db220
Diffstat (limited to 'navit/graphics.c')
-rw-r--r--navit/graphics.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/navit/graphics.c b/navit/graphics.c
index 8fe23721e..a7f75ac33 100644
--- a/navit/graphics.c
+++ b/navit/graphics.c
@@ -999,21 +999,25 @@ static int
int_sqrt(unsigned int n)
{
unsigned int h, p= 0, q= 1, r= n;
- while ( q <= n ) {
- q <<= 2;
- if(q == 0) {
- return (int) sqrtf( (float) n ); /* use float sqrt if we reach q MAX */
- }
- }
-
- while ( q != 1 ) {
+
+ /* avoid q rollover */
+ if(n > (1<<(sizeof(n)*8-1))) {
+ q = 1<<(sizeof(n)*8-1);
+ } else {
+ while ( q <= n ) {
+ q <<= 2;
+ }
q >>= 2;
+ }
+
+ while ( q != 0 ) {
h = p + q;
p >>= 1;
if ( r >= h ) {
p += q;
r -= h;
- }
+ }
+ q >>= 2;
}
return p;
}