]> mj.ucw.cz Git - libucw.git/blob - images/math.h
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git#v3.10
[libucw.git] / images / math.h
1 #ifndef _IMAGES_MATH_H
2 #define _IMAGES_MATH_H
3
4 extern const u32 fast_div_tab[];
5 extern const byte fast_sqrt_tab[];
6
7 static inline uns
8 isqr(int x)
9 {
10   return x * x;
11 }
12
13 static inline uns
14 fast_div_u32_u8(uns x, uns y)
15 {
16   return ((u64)(x) * fast_div_tab[y]) >> 32;
17 }
18
19 static inline uns
20 fast_sqrt_u16(uns x)
21 {
22   uns y;
23   if (x < (1 << 10) - 3)
24     y = fast_sqrt_tab[(x + 3) >> 2] >> 3;
25   else if (x < (1 << 14) - 28)
26     y = fast_sqrt_tab[(x + 28) >> 6] >> 1;
27   else
28     y = fast_sqrt_tab[x >> 8];
29   return (x < y * y) ? y - 1 : y;
30 }
31
32 static inline uns
33 fast_sqrt_u32(uns x)
34 {
35   uns y;
36   if (x < (1 << 16))
37     {
38       if (x < (1 << 10) - 3)
39         y = fast_sqrt_tab[(x + 3) >> 2] >> 3;
40       else if (x < (1 << 14) - 28)
41         y = fast_sqrt_tab[(x + 28) >> 6] >> 1;
42       else
43         y = fast_sqrt_tab[x >> 8];
44     }
45   else
46     {
47       if (x < (1 << 24))
48         {
49           if (x < (1 << 20))
50             {
51               y = fast_sqrt_tab[x >> 12];
52               y = (fast_div_u32_u8(x, y) >> 3) + (y << 1);
53             }
54           else
55             {
56               y = fast_sqrt_tab[x >> 16];
57               y = (fast_div_u32_u8(x, y) >> 5) + (y << 3);
58             }
59         }
60       else
61         {
62           if (x < (1 << 28))
63             {
64               if (x < (1 << 26))
65                 {
66                   y = fast_sqrt_tab[x >> 18];
67                   y = (fast_div_u32_u8(x, y) >> 6) + (y << 4);
68                 }
69               else
70                 {
71                   y = fast_sqrt_tab[x >> 20];
72                   y = (fast_div_u32_u8(x, y) >> 7) + (y << 5);
73                 }
74             }
75           else
76             {
77               if (x < (1 << 30))
78                 {
79                   y = fast_sqrt_tab[x >> 22];
80                   y = (fast_div_u32_u8(x, y) >> 8) + (y << 6);
81                 }
82               else
83                 {
84                   y = fast_sqrt_tab[x >> 24];
85                   y = (fast_div_u32_u8(x, y) >> 9) + (y << 7);
86                 }
87             }
88         }
89     }
90   return (x < y * y) ? y - 1 : y;
91 }
92
93 #endif