clistmathinteger

Branchlessly map one list of integers to another


I have a list of integers, which are: 28525 30068 25975 26740 29286 24947 30067. Using a single mathematical and logical expression (the faster it computes the better), I need to convert them to the list 0 1 2 3 4 5 6. I can not use a lookup table or any known hashing as it's too slow, the expression also needs to be in C.

I tried to minimize the amount of information the numbers carry while staying distinguishable by bruteforcing the second component in expression number & x. The smallest number I found this way that kept all the results different from each other was 1031, which gave me this: 1029 1028 1031 4 6 3 1027. I suppose I could add an another logical operation to narrow it down even further, however, I don't think this is the most efficient approach. Do you have any ideas on how to approach this?

By the way, I'm not sure whether I added the right tags, feel free to fix them if you think they are not suitable.

Update: I tried using remainder operator, which with bruteforcing game me: number % 14 = 7 10 5 0 12 13 9, which is much better, but still requires a couple more steps.


Solution

  • x * 0x3160du >> 16 & 7.

    Found by brute force.