github.com/primecitizens/pcz/std@v0.2.1/core/bits/reverse.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2017 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package bits 9 10 import ( 11 "github.com/primecitizens/pcz/std/core/arch" 12 ) 13 14 // Reverse returns the value of x with its bits in reversed order. 15 func Reverse(x uint) uint { 16 if arch.UintBits == 32 { 17 return uint(Reverse32(uint32(x))) 18 } 19 return uint(Reverse64(uint64(x))) 20 } 21 22 const Rev8Table = "" + 23 "\x00\x80\x40\xc0\x20\xa0\x60\xe0\x10\x90\x50\xd0\x30\xb0\x70\xf0" + 24 "\x08\x88\x48\xc8\x28\xa8\x68\xe8\x18\x98\x58\xd8\x38\xb8\x78\xf8" + 25 "\x04\x84\x44\xc4\x24\xa4\x64\xe4\x14\x94\x54\xd4\x34\xb4\x74\xf4" + 26 "\x0c\x8c\x4c\xcc\x2c\xac\x6c\xec\x1c\x9c\x5c\xdc\x3c\xbc\x7c\xfc" + 27 "\x02\x82\x42\xc2\x22\xa2\x62\xe2\x12\x92\x52\xd2\x32\xb2\x72\xf2" + 28 "\x0a\x8a\x4a\xca\x2a\xaa\x6a\xea\x1a\x9a\x5a\xda\x3a\xba\x7a\xfa" + 29 "\x06\x86\x46\xc6\x26\xa6\x66\xe6\x16\x96\x56\xd6\x36\xb6\x76\xf6" + 30 "\x0e\x8e\x4e\xce\x2e\xae\x6e\xee\x1e\x9e\x5e\xde\x3e\xbe\x7e\xfe" + 31 "\x01\x81\x41\xc1\x21\xa1\x61\xe1\x11\x91\x51\xd1\x31\xb1\x71\xf1" + 32 "\x09\x89\x49\xc9\x29\xa9\x69\xe9\x19\x99\x59\xd9\x39\xb9\x79\xf9" + 33 "\x05\x85\x45\xc5\x25\xa5\x65\xe5\x15\x95\x55\xd5\x35\xb5\x75\xf5" + 34 "\x0d\x8d\x4d\xcd\x2d\xad\x6d\xed\x1d\x9d\x5d\xdd\x3d\xbd\x7d\xfd" + 35 "\x03\x83\x43\xc3\x23\xa3\x63\xe3\x13\x93\x53\xd3\x33\xb3\x73\xf3" + 36 "\x0b\x8b\x4b\xcb\x2b\xab\x6b\xeb\x1b\x9b\x5b\xdb\x3b\xbb\x7b\xfb" + 37 "\x07\x87\x47\xc7\x27\xa7\x67\xe7\x17\x97\x57\xd7\x37\xb7\x77\xf7" + 38 "\x0f\x8f\x4f\xcf\x2f\xaf\x6f\xef\x1f\x9f\x5f\xdf\x3f\xbf\x7f\xff" 39 40 // Reverse8 returns the value of x with its bits in reversed order. 41 func Reverse8(x uint8) uint8 { 42 return Rev8Table[x] 43 } 44 45 // Reverse16 returns the value of x with its bits in reversed order. 46 func Reverse16(x uint16) uint16 { 47 return uint16(Rev8Table[x>>8]) | uint16(Rev8Table[x&0xff])<<8 48 } 49 50 // Reverse32 returns the value of x with its bits in reversed order. 51 func Reverse32(x uint32) uint32 { 52 const m = 1<<32 - 1 53 x = x>>1&(m0&m) | x&(m0&m)<<1 54 x = x>>2&(m1&m) | x&(m1&m)<<2 55 x = x>>4&(m2&m) | x&(m2&m)<<4 56 return ReverseBytes32(x) 57 } 58 59 // Reverse64 returns the value of x with its bits in reversed order. 60 func Reverse64(x uint64) uint64 { 61 const m = 1<<64 - 1 62 x = x>>1&(m0&m) | x&(m0&m)<<1 63 x = x>>2&(m1&m) | x&(m1&m)<<2 64 x = x>>4&(m2&m) | x&(m2&m)<<4 65 return ReverseBytes64(x) 66 } 67 68 // ReverseBytes returns the value of x with its bytes in reversed order. 69 // 70 // This function's execution time does not depend on the inputs. 71 func ReverseBytes(x uint) uint { 72 if arch.UintBits == 32 { 73 return uint(ReverseBytes32(uint32(x))) 74 } 75 return uint(ReverseBytes64(uint64(x))) 76 } 77 78 // ReverseBytes16 returns the value of x with its bytes in reversed order. 79 // 80 // This function's execution time does not depend on the inputs. 81 func ReverseBytes16(x uint16) uint16 { 82 return x>>8 | x<<8 83 } 84 85 // ReverseBytes32 returns the value of x with its bytes in reversed order. 86 // 87 // This function's execution time does not depend on the inputs. 88 func ReverseBytes32(x uint32) uint32 { 89 const m = 1<<32 - 1 90 x = x>>8&(m3&m) | x&(m3&m)<<8 91 return x>>16 | x<<16 92 } 93 94 // ReverseBytes64 returns the value of x with its bytes in reversed order. 95 // 96 // This function's execution time does not depend on the inputs. 97 func ReverseBytes64(x uint64) uint64 { 98 const m = 1<<64 - 1 99 x = x>>8&(m3&m) | x&(m3&m)<<8 100 x = x>>16&(m4&m) | x&(m4&m)<<16 101 return x>>32 | x<<32 102 }