cleanup
[rpikern] / src / libc / math.c
1 #include "math.h"
2
3 static double calc_pow(double x, double y, double precision);
4
5 double pow(double x, double y)
6 {
7         if(y == 0.0 || y == -0.0) {
8                 return 1.0;
9         }
10         if(y == 1.0) {
11                 return x;
12         }
13         if(y == -INFINITY) {
14                 return fabs(x) < 1.0 ? INFINITY : 0.0;
15         }
16         if(y == INFINITY) {
17                 return fabs(x) < 1.0 ? 0.0 : INFINITY;
18         }
19         return calc_pow(x, y, 1e-6);
20 }
21
22 static double calc_pow(double x, double y, double precision)
23 {
24         if(y < 0.0) {
25                 return 1.0 / calc_pow(x, -y, precision);
26         }
27         if(y >= 10.0) {
28                 double p = calc_pow(x, y / 2.0, precision / 2.0);
29                 return p * p;
30         }
31         if(y >= 1.0) {
32                 return x * calc_pow(x, y - 1.0, precision);
33         }
34         if(precision >= 1) {
35                 return __builtin_sqrt(x);
36         }
37         return __builtin_sqrt(calc_pow(x, y * 2.0, precision * 2.0));
38 }