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