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