Module: CRC::Utils
Overview
Utilities.
Instance Method Summary collapse
- #bitreflect(num, bitsize) ⇒ Object
- #bitreflect128(n) ⇒ Object
- #bitreflect16(n) ⇒ Object
- #bitreflect32(n) ⇒ Object
- #bitreflect64(n) ⇒ Object
- #bitreflect8(n) ⇒ Object
- #bitreflect_reference(num, bitsize) ⇒ Object
- #build_reflect_table(bitsize, polynomial, unfreeze = false, slice: 16) ⇒ Object
- #build_table(bitsize, polynomial, unfreeze = false, slice: 16) ⇒ Object
- #export_table(table, bitsize, linewidth, indentsize = 2) ⇒ Object
Instance Method Details
#bitreflect(num, bitsize) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/crc/_utils.rb', line 14 def bitreflect(num, bitsize) case when bitsize > 128 bitreflect_reference(num, bitsize) when bitsize > 64 bitreflect128(num) >> (128 - bitsize) when bitsize > 32 bitreflect64(num) >> (64 - bitsize) when bitsize > 16 bitreflect32(num) >> (32 - bitsize) when bitsize > 8 bitreflect16(num) >> (16 - bitsize) else bitreflect8(num) >> (8 - bitsize) end end |
#bitreflect128(n) ⇒ Object
300 301 302 303 304 305 306 307 308 309 |
# File 'lib/crc/_byruby.rb', line 300 def bitreflect128(n) n = n.to_i n = ((n & 0x55555555555555555555555555555555) << 1) | ((n >> 1) & 0x55555555555555555555555555555555) n = ((n & 0x33333333333333333333333333333333) << 2) | ((n >> 2) & 0x33333333333333333333333333333333) n = ((n & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) << 4) | ((n >> 4) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) n = ((n & 0x00ff00ff00ff00ff00ff00ff00ff00ff) << 8) | ((n >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) n = ((n & 0x0000ffff0000ffff0000ffff0000ffff) << 16) | ((n >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) n = ((n & 0x00000000ffffffff00000000ffffffff) << 32) | ((n >> 32) & 0x00000000ffffffff00000000ffffffff) return ((n & 0x0000000000000000ffffffffffffffff) << 64) | (n >> 64) # 0x0000000000000000ffffffffffffffff end |
#bitreflect16(n) ⇒ Object
273 274 275 276 277 278 279 |
# File 'lib/crc/_byruby.rb', line 273 def bitreflect16(n) n = n.to_i n = ((n & 0x5555) << 1) | ((n >> 1) & 0x5555) n = ((n & 0x3333) << 2) | ((n >> 2) & 0x3333) n = ((n & 0x0f0f) << 4) | ((n >> 4) & 0x0f0f) return ((n & 0x00ff) << 8) | (n >> 8) # 0x00ff end |
#bitreflect32(n) ⇒ Object
281 282 283 284 285 286 287 288 |
# File 'lib/crc/_byruby.rb', line 281 def bitreflect32(n) n = n.to_i n = ((n & 0x55555555) << 1) | ((n >> 1) & 0x55555555) n = ((n & 0x33333333) << 2) | ((n >> 2) & 0x33333333) n = ((n & 0x0f0f0f0f) << 4) | ((n >> 4) & 0x0f0f0f0f) n = ((n & 0x00ff00ff) << 8) | ((n >> 8) & 0x00ff00ff) return ((n & 0x0000ffff) << 16) | (n >> 16) # 0x0000ffff end |
#bitreflect64(n) ⇒ Object
290 291 292 293 294 295 296 297 298 |
# File 'lib/crc/_byruby.rb', line 290 def bitreflect64(n) n = n.to_i n = ((n & 0x5555555555555555) << 1) | ((n >> 1) & 0x5555555555555555) n = ((n & 0x3333333333333333) << 2) | ((n >> 2) & 0x3333333333333333) n = ((n & 0x0f0f0f0f0f0f0f0f) << 4) | ((n >> 4) & 0x0f0f0f0f0f0f0f0f) n = ((n & 0x00ff00ff00ff00ff) << 8) | ((n >> 8) & 0x00ff00ff00ff00ff) n = ((n & 0x0000ffff0000ffff) << 16) | ((n >> 16) & 0x0000ffff0000ffff) return ((n & 0x00000000ffffffff) << 32) | (n >> 32) # 0x00000000ffffffff end |
#bitreflect8(n) ⇒ Object
266 267 268 269 270 271 |
# File 'lib/crc/_byruby.rb', line 266 def bitreflect8(n) n = n.to_i n = ((n & 0x55) << 1) | ((n >> 1) & 0x55) n = ((n & 0x33) << 2) | ((n >> 2) & 0x33) return ((n & 0x0f) << 4) | (n >> 4) # 0x0f end |
#bitreflect_reference(num, bitsize) ⇒ Object
8 9 10 11 12 |
# File 'lib/crc/_utils.rb', line 8 def bitreflect_reference(num, bitsize) n = 0 bitsize.times { n <<= 1; n |= (num & 0x01); num >>= 1 } n end |
#build_reflect_table(bitsize, polynomial, unfreeze = false, slice: 16) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/crc/_utils.rb', line 58 def build_reflect_table(bitsize, polynomial, unfreeze = false, slice: 16) polynomial = bitreflect(polynomial, bitsize) table = [] table << (t = []) 256.times do |b| 8.times { b = (b[0] == 0) ? (b >> 1) : ((b >> 1) ^ polynomial) } t << b end t.freeze unless unfreeze (1...slice).step do tt = table[-1] table << (t = []) 256.times do |b| t << (table[0][tt[b] & 0xff] ^ (tt[b] >> 8)) end t.freeze unless unfreeze end table.freeze unless unfreeze table end |
#build_table(bitsize, polynomial, unfreeze = false, slice: 16) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/crc/_utils.rb', line 31 def build_table(bitsize, polynomial, unfreeze = false, slice: 16) bitmask = ~(~0 << bitsize) table = [] Aux.(bitsize, 0, bitmask & polynomial, bitmask) do |xx, poly, csh, head, carries, pad| table << (t = []) 256.times do |b| b <<= csh 8.times { b = (b[head] == 0) ? (b << 1) : (((carries & b) << 1) ^ poly) } t << b end t.freeze unless unfreeze carries8 = carries >> 7 (1...slice).step do tt = table[-1] table << (t = []) 256.times do |b| t << (table[0][tt[b] >> csh] ^ ((carries8 & tt[b]) << 8)) end t.freeze unless unfreeze end 0 end table.freeze unless unfreeze table end |
#export_table(table, bitsize, linewidth, indentsize = 2) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/crc/_utils.rb', line 82 def export_table(table, bitsize, linewidth, indentsize = 2) bitsize0 = bitsize.to_i indent = " " * indentsize.to_i case when bitsize0 > 64 || bitsize0 < 1 raise "invalid bitsize (expected to 1..64, but given #{bitsize})" when bitsize0 > 32 packformat = "Q>" hexwidth = 16 when bitsize0 > 16 packformat = "N" hexwidth = 8 when bitsize0 > 8 packformat = "n" hexwidth = 4 else # when bitsize0 > 0 packformat = "C" hexwidth = 2 end table = table.to_a.pack("#{packformat}*").unpack("H*")[0] table.gsub!(/(?<=\w)(?=\w{#{hexwidth}}{#{linewidth}}+$)/, "\n") table.gsub!(/(?<=\w)(?=\w{#{hexwidth}}+$)/, " ") table.gsub!(/(?<=\w)(?=\s|$)/, ",") table.gsub!(/(?:(?<=^)|(?<=\s))(?=\w)/, "0x") table.gsub!(/^/, "#{indent} ") <<-EOS #{indent}TABLE = [ #{table} #{indent}].freeze EOS end |