+ ee_sqrt() now uses unsigned ints.
+ Avoid overflows in ee_sqrt().
tags/v0.99.beta14
| @@ -95,7 +95,7 @@ void ee_draw_thin_triangle(int, int, int, int, int, int); | |||||
| void ee_fill_triangle(int, int, int, int, int, int, char); | void ee_fill_triangle(int, int, int, int, int, int, char); | ||||
| int ee_rand(int, int); | int ee_rand(int, int); | ||||
| int ee_sqrt(int); | |||||
| unsigned int ee_sqrt(unsigned int); | |||||
| struct ee_sprite * ee_load_sprite(const char *); | struct ee_sprite * ee_load_sprite(const char *); | ||||
| int ee_get_sprite_frames(struct ee_sprite *); | int ee_get_sprite_frames(struct ee_sprite *); | ||||
| @@ -32,23 +32,28 @@ int ee_rand(int min, int max) | |||||
| return min + (int)((1.0*(max-min+1)) * rand() / (RAND_MAX+1.0)); | return min + (int)((1.0*(max-min+1)) * rand() / (RAND_MAX+1.0)); | ||||
| } | } | ||||
| int ee_sqrt(int a) | |||||
| unsigned int ee_sqrt(unsigned int a) | |||||
| { | { | ||||
| int x; | |||||
| if(a == 0) | |||||
| return 0; | |||||
| if(a <= 0) | |||||
| if(a < 1000000000) | |||||
| { | { | ||||
| return 0; | |||||
| unsigned int x = a < 10 ? 1 | |||||
| : a < 1000 ? 10 | |||||
| : a < 100000 ? 100 | |||||
| : a < 10000000 ? 1000 | |||||
| : 10000; | |||||
| /* Newton's method. Three iterations would be more than enough. */ | |||||
| x = (x * x + a) / x / 2; | |||||
| x = (x * x + a) / x / 2; | |||||
| x = (x * x + a) / x / 2; | |||||
| x = (x * x + a) / x / 2; | |||||
| return x; | |||||
| } | } | ||||
| x = a > 100000 ? 1000 : a > 1000 ? 100 : a > 10 ? 10 : 1; | |||||
| /* Newton's method. Three iterations would be more than enough. */ | |||||
| x = (x * x + a) / x / 2; | |||||
| x = (x * x + a) / x / 2; | |||||
| x = (x * x + a) / x / 2; | |||||
| x = (x * x + a) / x / 2; | |||||
| return x; | |||||
| return 2 * ee_sqrt(a / 4); | |||||
| } | } | ||||