xref: /linux/arch/parisc/include/asm/hash.h (revision 498495dba268b20e8eadd7fe93c140c68b6cc9d2)
1*b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2773e1c5fSGeorge Spelvin #ifndef _ASM_HASH_H
3773e1c5fSGeorge Spelvin #define _ASM_HASH_H
4773e1c5fSGeorge Spelvin 
5773e1c5fSGeorge Spelvin /*
6773e1c5fSGeorge Spelvin  * HP-PA only implements integer multiply in the FPU.  However, for
7773e1c5fSGeorge Spelvin  * integer multiplies by constant, it has a number of shift-and-add
8773e1c5fSGeorge Spelvin  * (but no shift-and-subtract, sigh!) instructions that a compiler
9773e1c5fSGeorge Spelvin  * can synthesize a code sequence with.
10773e1c5fSGeorge Spelvin  *
11773e1c5fSGeorge Spelvin  * Unfortunately, GCC isn't very efficient at using them.  For example
12773e1c5fSGeorge Spelvin  * it uses three instructions for "x *= 21" when only two are needed.
13773e1c5fSGeorge Spelvin  * But we can find a sequence manually.
14773e1c5fSGeorge Spelvin  */
15773e1c5fSGeorge Spelvin 
16773e1c5fSGeorge Spelvin #define HAVE_ARCH__HASH_32 1
17773e1c5fSGeorge Spelvin 
18773e1c5fSGeorge Spelvin /*
19773e1c5fSGeorge Spelvin  * This is a multiply by GOLDEN_RATIO_32 = 0x61C88647 optimized for the
20773e1c5fSGeorge Spelvin  * PA7100 pairing rules.  This is an in-order 2-way superscalar processor.
21773e1c5fSGeorge Spelvin  * Only one instruction in a pair may be a shift (by more than 3 bits),
22773e1c5fSGeorge Spelvin  * but other than that, simple ALU ops (including shift-and-add by up
23773e1c5fSGeorge Spelvin  * to 3 bits) may be paired arbitrarily.
24773e1c5fSGeorge Spelvin  *
25773e1c5fSGeorge Spelvin  * PA8xxx processors also dual-issue ALU instructions, although with
26773e1c5fSGeorge Spelvin  * fewer constraints, so this schedule is good for them, too.
27773e1c5fSGeorge Spelvin  *
28773e1c5fSGeorge Spelvin  * This 6-step sequence was found by Yevgen Voronenko's implementation
29773e1c5fSGeorge Spelvin  * of the Hcub algorithm at http://spiral.ece.cmu.edu/mcm/gen.html.
30773e1c5fSGeorge Spelvin  */
__hash_32(u32 x)31773e1c5fSGeorge Spelvin static inline u32 __attribute_const__ __hash_32(u32 x)
32773e1c5fSGeorge Spelvin {
33773e1c5fSGeorge Spelvin 	u32 a, b, c;
34773e1c5fSGeorge Spelvin 
35773e1c5fSGeorge Spelvin 	/*
36773e1c5fSGeorge Spelvin 	 * Phase 1: Compute  a = (x << 19) + x,
37773e1c5fSGeorge Spelvin 	 * b = (x << 9) + a, c = (x << 23) + b.
38773e1c5fSGeorge Spelvin 	 */
39773e1c5fSGeorge Spelvin 	a = x << 19;		/* Two shifts can't be paired */
40773e1c5fSGeorge Spelvin 	b = x << 9;	a += x;
41773e1c5fSGeorge Spelvin 	c = x << 23;	b += a;
42773e1c5fSGeorge Spelvin 			c += b;
43773e1c5fSGeorge Spelvin 	/* Phase 2: Return (b<<11) + (c<<6) + (a<<3) - c */
44773e1c5fSGeorge Spelvin 	b <<= 11;
45773e1c5fSGeorge Spelvin 	a += c << 3;	b -= c;
46773e1c5fSGeorge Spelvin 	return (a << 3) + b;
47773e1c5fSGeorge Spelvin }
48773e1c5fSGeorge Spelvin 
49773e1c5fSGeorge Spelvin #if BITS_PER_LONG == 64
50773e1c5fSGeorge Spelvin 
51773e1c5fSGeorge Spelvin #define HAVE_ARCH_HASH_64 1
52773e1c5fSGeorge Spelvin 
53773e1c5fSGeorge Spelvin /*
54773e1c5fSGeorge Spelvin  * Finding a good shift-and-add chain for GOLDEN_RATIO_64 is tricky,
55773e1c5fSGeorge Spelvin  * because available software for the purpose chokes on constants this
56773e1c5fSGeorge Spelvin  * large.  (It's mostly designed for compiling FIR filter coefficients
57773e1c5fSGeorge Spelvin  * into FPGAs.)
58773e1c5fSGeorge Spelvin  *
59773e1c5fSGeorge Spelvin  * However, Jason Thong pointed out a work-around.  The Hcub software
60773e1c5fSGeorge Spelvin  * (http://spiral.ece.cmu.edu/mcm/gen.html) is designed for *multiple*
61773e1c5fSGeorge Spelvin  * constant multiplication, and is good at finding shift-and-add chains
62773e1c5fSGeorge Spelvin  * which share common terms.
63773e1c5fSGeorge Spelvin  *
64773e1c5fSGeorge Spelvin  * Looking at 0x0x61C8864680B583EB in binary:
65773e1c5fSGeorge Spelvin  * 0110000111001000100001100100011010000000101101011000001111101011
66773e1c5fSGeorge Spelvin  *  \______________/    \__________/       \_______/     \________/
67773e1c5fSGeorge Spelvin  *   \____________________________/         \____________________/
68773e1c5fSGeorge Spelvin  * you can see the non-zero bits are divided into several well-separated
69773e1c5fSGeorge Spelvin  * blocks.  Hcub can find algorithms for those terms separately, which
70773e1c5fSGeorge Spelvin  * can then be shifted and added together.
71773e1c5fSGeorge Spelvin  *
72773e1c5fSGeorge Spelvin  * Dividing the input into 2, 3 or 4 blocks, Hcub can find solutions
73773e1c5fSGeorge Spelvin  * with 10, 9 or 8 adds, respectively, making a total of 11 for the
74773e1c5fSGeorge Spelvin  * whole number.
75773e1c5fSGeorge Spelvin  *
76773e1c5fSGeorge Spelvin  * Using just two large blocks, 0xC3910C8D << 31 in the high bits,
77773e1c5fSGeorge Spelvin  * and 0xB583EB in the low bits, produces as good an algorithm as any,
78773e1c5fSGeorge Spelvin  * and with one more small shift than alternatives.
79773e1c5fSGeorge Spelvin  *
80773e1c5fSGeorge Spelvin  * The high bits are a larger number and more work to compute, as well
81773e1c5fSGeorge Spelvin  * as needing one extra cycle to shift left 31 bits before the final
82773e1c5fSGeorge Spelvin  * addition, so they are the critical path for scheduling.  The low bits
83773e1c5fSGeorge Spelvin  * can fit into the scheduling slots left over.
84773e1c5fSGeorge Spelvin  */
85773e1c5fSGeorge Spelvin 
86773e1c5fSGeorge Spelvin 
87773e1c5fSGeorge Spelvin /*
88773e1c5fSGeorge Spelvin  * This _ASSIGN(dst, src) macro performs "dst = src", but prevents GCC
89773e1c5fSGeorge Spelvin  * from inferring anything about the value assigned to "dest".
90773e1c5fSGeorge Spelvin  *
91773e1c5fSGeorge Spelvin  * This prevents it from mis-optimizing certain sequences.
92773e1c5fSGeorge Spelvin  * In particular, gcc is annoyingly eager to combine consecutive shifts.
93773e1c5fSGeorge Spelvin  * Given "x <<= 19; y += x; z += x << 1;", GCC will turn this into
94773e1c5fSGeorge Spelvin  * "y += x << 19; z += x << 20;" even though the latter sequence needs
95773e1c5fSGeorge Spelvin  * an additional instruction and temporary register.
96773e1c5fSGeorge Spelvin  *
97773e1c5fSGeorge Spelvin  * Because no actual assembly code is generated, this construct is
98773e1c5fSGeorge Spelvin  * usefully portable across all GCC platforms, and so can be test-compiled
99773e1c5fSGeorge Spelvin  * on non-PA systems.
100773e1c5fSGeorge Spelvin  *
101773e1c5fSGeorge Spelvin  * In two places, additional unused input dependencies are added.  This
102773e1c5fSGeorge Spelvin  * forces GCC's scheduling so it does not rearrange instructions too much.
103773e1c5fSGeorge Spelvin  * Because the PA-8xxx is out of order, I'm not sure how much this matters,
104773e1c5fSGeorge Spelvin  * but why make it more difficult for the processor than necessary?
105773e1c5fSGeorge Spelvin  */
106773e1c5fSGeorge Spelvin #define _ASSIGN(dst, src, ...) asm("" : "=r" (dst) : "0" (src), ##__VA_ARGS__)
107773e1c5fSGeorge Spelvin 
108773e1c5fSGeorge Spelvin /*
109773e1c5fSGeorge Spelvin  * Multiply by GOLDEN_RATIO_64 = 0x0x61C8864680B583EB using a heavily
110773e1c5fSGeorge Spelvin  * optimized shift-and-add sequence.
111773e1c5fSGeorge Spelvin  *
112773e1c5fSGeorge Spelvin  * Without the final shift, the multiply proper is 19 instructions,
113773e1c5fSGeorge Spelvin  * 10 cycles and uses only 4 temporaries.  Whew!
114773e1c5fSGeorge Spelvin  *
115773e1c5fSGeorge Spelvin  * You are not expected to understand this.
116773e1c5fSGeorge Spelvin  */
117773e1c5fSGeorge Spelvin static __always_inline u32 __attribute_const__
hash_64(u64 a,unsigned int bits)118773e1c5fSGeorge Spelvin hash_64(u64 a, unsigned int bits)
119773e1c5fSGeorge Spelvin {
120773e1c5fSGeorge Spelvin 	u64 b, c, d;
121773e1c5fSGeorge Spelvin 
122773e1c5fSGeorge Spelvin 	/*
123773e1c5fSGeorge Spelvin 	 * Encourage GCC to move a dynamic shift to %sar early,
124773e1c5fSGeorge Spelvin 	 * thereby freeing up an additional temporary register.
125773e1c5fSGeorge Spelvin 	 */
126773e1c5fSGeorge Spelvin 	if (!__builtin_constant_p(bits))
127773e1c5fSGeorge Spelvin 		asm("" : "=q" (bits) : "0" (64 - bits));
128773e1c5fSGeorge Spelvin 	else
129773e1c5fSGeorge Spelvin 		bits = 64 - bits;
130773e1c5fSGeorge Spelvin 
131773e1c5fSGeorge Spelvin 	_ASSIGN(b, a*5);	c = a << 13;
132773e1c5fSGeorge Spelvin 	b = (b << 2) + a;	_ASSIGN(d, a << 17);
133773e1c5fSGeorge Spelvin 	a = b + (a << 1);	c += d;
134773e1c5fSGeorge Spelvin 	d = a << 10;		_ASSIGN(a, a << 19);
135773e1c5fSGeorge Spelvin 	d = a - d;		_ASSIGN(a, a << 4, "X" (d));
136773e1c5fSGeorge Spelvin 	c += b;			a += b;
137773e1c5fSGeorge Spelvin 	d -= c;			c += a << 1;
138773e1c5fSGeorge Spelvin 	a += c << 3;		_ASSIGN(b, b << (7+31), "X" (c), "X" (d));
139773e1c5fSGeorge Spelvin 	a <<= 31;		b += d;
140773e1c5fSGeorge Spelvin 	a += b;
141773e1c5fSGeorge Spelvin 	return a >> bits;
142773e1c5fSGeorge Spelvin }
143773e1c5fSGeorge Spelvin #undef _ASSIGN	/* We're a widely-used header file, so don't litter! */
144773e1c5fSGeorge Spelvin 
145773e1c5fSGeorge Spelvin #endif /* BITS_PER_LONG == 64 */
146773e1c5fSGeorge Spelvin 
147773e1c5fSGeorge Spelvin #endif /* _ASM_HASH_H */
148