xref: /openbmc/linux/arch/microblaze/include/asm/hash.h (revision b2441318)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
27b13277bSGeorge Spelvin #ifndef _ASM_HASH_H
37b13277bSGeorge Spelvin #define _ASM_HASH_H
47b13277bSGeorge Spelvin 
57b13277bSGeorge Spelvin /*
67b13277bSGeorge Spelvin  * Fortunately, most people who want to run Linux on Microblaze enable
77b13277bSGeorge Spelvin  * both multiplier and barrel shifter, but omitting them is technically
87b13277bSGeorge Spelvin  * a supported configuration.
97b13277bSGeorge Spelvin  *
107b13277bSGeorge Spelvin  * With just a barrel shifter, we can implement an efficient constant
117b13277bSGeorge Spelvin  * multiply using shifts and adds.  GCC can find a 9-step solution, but
127b13277bSGeorge Spelvin  * this 6-step solution was found by Yevgen Voronenko's implementation
137b13277bSGeorge Spelvin  * of the Hcub algorithm at http://spiral.ece.cmu.edu/mcm/gen.html.
147b13277bSGeorge Spelvin  *
157b13277bSGeorge Spelvin  * That software is really not designed for a single multiplier this large,
167b13277bSGeorge Spelvin  * but if you run it enough times with different seeds, it'll find several
177b13277bSGeorge Spelvin  * 6-shift, 6-add sequences for computing x * 0x61C88647.  They are all
187b13277bSGeorge Spelvin  *	c = (x << 19) + x;
197b13277bSGeorge Spelvin  *	a = (x <<  9) + c;
207b13277bSGeorge Spelvin  *	b = (x << 23) + a;
217b13277bSGeorge Spelvin  *	return (a<<11) + (b<<6) + (c<<3) - b;
227b13277bSGeorge Spelvin  * with variations on the order of the final add.
237b13277bSGeorge Spelvin  *
247b13277bSGeorge Spelvin  * Without even a shifter, it's hopless; any hash function will suck.
257b13277bSGeorge Spelvin  */
267b13277bSGeorge Spelvin 
277b13277bSGeorge Spelvin #if CONFIG_XILINX_MICROBLAZE0_USE_HW_MUL == 0
287b13277bSGeorge Spelvin 
297b13277bSGeorge Spelvin #define HAVE_ARCH__HASH_32 1
307b13277bSGeorge Spelvin 
317b13277bSGeorge Spelvin /* Multiply by GOLDEN_RATIO_32 = 0x61C88647 */
__hash_32(u32 a)327b13277bSGeorge Spelvin static inline u32 __attribute_const__ __hash_32(u32 a)
337b13277bSGeorge Spelvin {
347b13277bSGeorge Spelvin #if CONFIG_XILINX_MICROBLAZE0_USE_BARREL
357b13277bSGeorge Spelvin 	unsigned int b, c;
367b13277bSGeorge Spelvin 
377b13277bSGeorge Spelvin 	/* Phase 1: Compute three intermediate values */
387b13277bSGeorge Spelvin 	b =  a << 23;
397b13277bSGeorge Spelvin 	c = (a << 19) + a;
407b13277bSGeorge Spelvin 	a = (a <<  9) + c;
417b13277bSGeorge Spelvin 	b += a;
427b13277bSGeorge Spelvin 
437b13277bSGeorge Spelvin 	/* Phase 2: Compute (a << 11) + (b << 6) + (c << 3) - b */
447b13277bSGeorge Spelvin 	a <<= 5;
457b13277bSGeorge Spelvin 	a += b;		/* (a << 5) + b */
467b13277bSGeorge Spelvin 	a <<= 3;
477b13277bSGeorge Spelvin 	a += c;		/* (a << 8) + (b << 3) + c */
487b13277bSGeorge Spelvin 	a <<= 3;
497b13277bSGeorge Spelvin 	return a - b;	/* (a << 11) + (b << 6) + (c << 3) - b */
507b13277bSGeorge Spelvin #else
517b13277bSGeorge Spelvin 	/*
527b13277bSGeorge Spelvin 	 * "This is really going to hurt."
537b13277bSGeorge Spelvin 	 *
547b13277bSGeorge Spelvin 	 * Without a barrel shifter, left shifts are implemented as
557b13277bSGeorge Spelvin 	 * repeated additions, and the best we can do is an optimal
567b13277bSGeorge Spelvin 	 * addition-subtraction chain.  This one is not known to be
577b13277bSGeorge Spelvin 	 * optimal, but at 37 steps, it's decent for a 31-bit multiplier.
587b13277bSGeorge Spelvin 	 *
597b13277bSGeorge Spelvin 	 * Question: given its size (37*4 = 148 bytes per instance),
607b13277bSGeorge Spelvin 	 * and slowness, is this worth having inline?
617b13277bSGeorge Spelvin 	 */
627b13277bSGeorge Spelvin 	unsigned int b, c, d;
637b13277bSGeorge Spelvin 
647b13277bSGeorge Spelvin 	b = a << 4;	/* 4    */
657b13277bSGeorge Spelvin 	c = b << 1;	/* 1  5 */
667b13277bSGeorge Spelvin 	b += a;		/* 1  6 */
677b13277bSGeorge Spelvin 	c += b;		/* 1  7 */
687b13277bSGeorge Spelvin 	c <<= 3;	/* 3 10 */
697b13277bSGeorge Spelvin 	c -= a;		/* 1 11 */
707b13277bSGeorge Spelvin 	d = c << 7;	/* 7 18 */
717b13277bSGeorge Spelvin 	d += b;		/* 1 19 */
727b13277bSGeorge Spelvin 	d <<= 8;	/* 8 27 */
737b13277bSGeorge Spelvin 	d += a;		/* 1 28 */
747b13277bSGeorge Spelvin 	d <<= 1;	/* 1 29 */
757b13277bSGeorge Spelvin 	d += b;		/* 1 30 */
767b13277bSGeorge Spelvin 	d <<= 6;	/* 6 36 */
777b13277bSGeorge Spelvin 	return d + c;	/* 1 37 total instructions*/
787b13277bSGeorge Spelvin #endif
797b13277bSGeorge Spelvin }
807b13277bSGeorge Spelvin 
817b13277bSGeorge Spelvin #endif /* !CONFIG_XILINX_MICROBLAZE0_USE_HW_MUL */
827b13277bSGeorge Spelvin #endif /* _ASM_HASH_H */
83