GNU Linux-libre 4.9.296-gnu1
[releases.git] / arch / microblaze / include / asm / hash.h
1 #ifndef _ASM_HASH_H
2 #define _ASM_HASH_H
3
4 /*
5  * Fortunately, most people who want to run Linux on Microblaze enable
6  * both multiplier and barrel shifter, but omitting them is technically
7  * a supported configuration.
8  *
9  * With just a barrel shifter, we can implement an efficient constant
10  * multiply using shifts and adds.  GCC can find a 9-step solution, but
11  * this 6-step solution was found by Yevgen Voronenko's implementation
12  * of the Hcub algorithm at http://spiral.ece.cmu.edu/mcm/gen.html.
13  *
14  * That software is really not designed for a single multiplier this large,
15  * but if you run it enough times with different seeds, it'll find several
16  * 6-shift, 6-add sequences for computing x * 0x61C88647.  They are all
17  *      c = (x << 19) + x;
18  *      a = (x <<  9) + c;
19  *      b = (x << 23) + a;
20  *      return (a<<11) + (b<<6) + (c<<3) - b;
21  * with variations on the order of the final add.
22  *
23  * Without even a shifter, it's hopless; any hash function will suck.
24  */
25
26 #if CONFIG_XILINX_MICROBLAZE0_USE_HW_MUL == 0
27
28 #define HAVE_ARCH__HASH_32 1
29
30 /* Multiply by GOLDEN_RATIO_32 = 0x61C88647 */
31 static inline u32 __attribute_const__ __hash_32(u32 a)
32 {
33 #if CONFIG_XILINX_MICROBLAZE0_USE_BARREL
34         unsigned int b, c;
35
36         /* Phase 1: Compute three intermediate values */
37         b =  a << 23;
38         c = (a << 19) + a;
39         a = (a <<  9) + c;
40         b += a;
41
42         /* Phase 2: Compute (a << 11) + (b << 6) + (c << 3) - b */
43         a <<= 5;
44         a += b;         /* (a << 5) + b */
45         a <<= 3;
46         a += c;         /* (a << 8) + (b << 3) + c */
47         a <<= 3;
48         return a - b;   /* (a << 11) + (b << 6) + (c << 3) - b */
49 #else
50         /*
51          * "This is really going to hurt."
52          *
53          * Without a barrel shifter, left shifts are implemented as
54          * repeated additions, and the best we can do is an optimal
55          * addition-subtraction chain.  This one is not known to be
56          * optimal, but at 37 steps, it's decent for a 31-bit multiplier.
57          *
58          * Question: given its size (37*4 = 148 bytes per instance),
59          * and slowness, is this worth having inline?
60          */
61         unsigned int b, c, d;
62
63         b = a << 4;     /* 4    */
64         c = b << 1;     /* 1  5 */
65         b += a;         /* 1  6 */
66         c += b;         /* 1  7 */
67         c <<= 3;        /* 3 10 */
68         c -= a;         /* 1 11 */
69         d = c << 7;     /* 7 18 */
70         d += b;         /* 1 19 */
71         d <<= 8;        /* 8 27 */
72         d += a;         /* 1 28 */
73         d <<= 1;        /* 1 29 */
74         d += b;         /* 1 30 */
75         d <<= 6;        /* 6 36 */
76         return d + c;   /* 1 37 total instructions*/
77 #endif
78 }
79
80 #endif /* !CONFIG_XILINX_MICROBLAZE0_USE_HW_MUL */
81 #endif /* _ASM_HASH_H */