/* SPDX-License-Identifier: GPL-2.0 */1#ifndef _ASM_HASH_H2#define _ASM_HASH_H34/*5* Fortunately, most people who want to run Linux on Microblaze enable6* both multiplier and barrel shifter, but omitting them is technically7* a supported configuration.8*9* With just a barrel shifter, we can implement an efficient constant10* multiply using shifts and adds. GCC can find a 9-step solution, but11* this 6-step solution was found by Yevgen Voronenko's implementation12* 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 several16* 6-shift, 6-add sequences for computing x * 0x61C88647. They are all17* 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*/2526#if CONFIG_XILINX_MICROBLAZE0_USE_HW_MUL == 02728#define HAVE_ARCH__HASH_32 12930/* Multiply by GOLDEN_RATIO_32 = 0x61C88647 */31static inline u32 __attribute_const__ __hash_32(u32 a)32{33#if CONFIG_XILINX_MICROBLAZE0_USE_BARREL34unsigned int b, c;3536/* Phase 1: Compute three intermediate values */37b = a << 23;38c = (a << 19) + a;39a = (a << 9) + c;40b += a;4142/* Phase 2: Compute (a << 11) + (b << 6) + (c << 3) - b */43a <<= 5;44a += b; /* (a << 5) + b */45a <<= 3;46a += c; /* (a << 8) + (b << 3) + c */47a <<= 3;48return a - b; /* (a << 11) + (b << 6) + (c << 3) - b */49#else50/*51* "This is really going to hurt."52*53* Without a barrel shifter, left shifts are implemented as54* repeated additions, and the best we can do is an optimal55* addition-subtraction chain. This one is not known to be56* 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*/61unsigned int b, c, d;6263b = a << 4; /* 4 */64c = b << 1; /* 1 5 */65b += a; /* 1 6 */66c += b; /* 1 7 */67c <<= 3; /* 3 10 */68c -= a; /* 1 11 */69d = c << 7; /* 7 18 */70d += b; /* 1 19 */71d <<= 8; /* 8 27 */72d += a; /* 1 28 */73d <<= 1; /* 1 29 */74d += b; /* 1 30 */75d <<= 6; /* 6 36 */76return d + c; /* 1 37 total instructions*/77#endif78}7980#endif /* !CONFIG_XILINX_MICROBLAZE0_USE_HW_MUL */81#endif /* _ASM_HASH_H */828384