Login
spookyhashinternal.cr at tip
Login

File src/remilib/digest/spookyhash/spookyhashinternal.cr from the latest check-in


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    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
   113
   114
   115
   116
   117
   118
   119
   120
   121
   122
   123
   124
   125
   126
   127
   128
   129
   130
   131
   132
   133
   134
   135
   136
   137
   138
   139
   140
   141
   142
   143
   144
   145
   146
   147
   148
   149
   150
   151
   152
   153
   154
   155
   156
   157
   158
   159
   160
   161
   162
   163
   164
   165
   166
   167
   168
   169
   170
   171
   172
   173
   174
   175
   176
   177
   178
   179
   180
   181
   182
   183
   184
   185
   186
   187
   188
   189
   190
   191
   192
   193
   194
   195
   196
   197
   198
   199
   200
   201
   202
   203
   204
   205
   206
   207
   208
   209
   210
   211
   212
   213
   214
   215
   216
   217
   218
   219
   220
   221
   222
   223
   224
   225
   226
   227
   228
   229
   230
   231
   232
   233
   234
   235
   236
   237
   238
   239
   240
   241
   242
   243
   244
   245
   246
   247
   248
   249
   250
   251
   252
   253
   254
   255
   256
   257
   258
   259
   260
   261
   262
   263
   264
   265
   266
   267
   268
   269
   270
   271
   272
   273
   274
   275
   276
   277
   278
   279
   280
   281
   282
   283
   284
   285
   286
   287
   288
   289
   290
   291
   292
   293
   294
   295
   296
   297
   298
   299
   300
   301
   302
   303
   304
   305
   306
   307
   308
   309
   310
   311
   312
   313
   314
   315
   316
   317
   318
   319
   320
   321
   322
   323
   324
   325
   326
   327
   328
   329
   330
   331
   332
   333
   334
   335
   336
   337
   338
   339
   340
   341
   342
   343
   344
   345
   346
   347
   348
   349
   350
   351
   352
   353
   354
   355
   356
   357
   358
   359
   360
   361
   362
   363
   364
   365
   366
   367
   368
   369
   370
   371
   372
   373
   374
   375
   376
   377
   378
   379
   380
   381
   382
   383
   384
   385
   386
   387
   388
   389
   390
   391
   392
   393
   394
#### SpookyHash
#### Copyright (c) 2023-2024, Remilia Scarlet
#### Copyright (c) 2015, Guillaume Voirin
#### BSD 3-Clause License
####
#### All rights reserved.
####
#### Redistribution and use in source and binary forms, with or without
#### modification, are permitted provided that the following conditions are met:
####
####     Redistributions of source code must retain the above copyright notice,
####     this list of conditions and the following disclaimer.
####
####     Redistributions in binary form must reproduce the above copyright
####     notice, this list of conditions and the following disclaimer in the
####     documentation and/or other materials provided with the distribution.
####
####     Neither the name of the copyright holder nor the names of its
####     contributors may be used to endorse or promote products derived from
####     this software without specific prior written permission.
####
#### THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#### AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#### IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#### ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#### LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#### CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#### SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#### INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#### CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#### ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#### POSSIBILITY OF SUCH DAMAGE.
require "./globals"

####
#### Core internal functionality.
####

# :nodoc:
# Hacky stuff.
lib SpookyHashLib
  union DataUnion
    p8 : Pointer(UInt8)
    p32 : Pointer(UInt32)
    p64 : Pointer(UInt64)
    i : LibC::SizeT
  end

  union DataUnion128
    p8 : Pointer(UInt8)
    p64 : Pointer(UInt64)
    i : LibC::SizeT
  end
end

class RemiLib::Digest::SpookyHash
  @data : Slice(UInt64) = Slice(UInt64).new(2 * SPOOKYHASH_VARIABLES, 0u64)
  @state : Slice(UInt64) = Slice(UInt64).new(SPOOKYHASH_VARIABLES, 0u64)
  @length : Int64 = 0
  @remainder : UInt8 = 0

  @[AlwaysInline]
  protected def self.shortEnd(h0 : PUInt64, h1 : PUInt64, h2 : PUInt64, h3 : PUInt64) : Nil
    h3.value ^= h2.value
    h2.value = rotate(h2.value, 15)
    h3.value = h3.value &+ h2.value
    h0.value ^= h3.value
    h3.value = rotate(h3.value, 52)
    h0.value = h0.value &+ h3.value
    h1.value ^= h0.value
    h0.value = rotate(h0.value, 26)
    h1.value = h1.value &+ h0.value
    h2.value ^= h1.value
    h1.value = rotate(h1.value, 51)
    h2.value = h2.value &+ h1.value
    h3.value ^= h2.value
    h2.value = rotate(h2.value, 28)
    h3.value = h3.value &+ h2.value
    h0.value ^= h3.value
    h3.value = rotate(h3.value, 9)
    h0.value = h0.value &+ h3.value
    h1.value ^= h0.value
    h0.value = rotate(h0.value, 47)
    h1.value = h1.value &+ h0.value
    h2.value ^= h1.value
    h1.value = rotate(h1.value, 54)
    h2.value = h2.value &+ h1.value
    h3.value ^= h2.value
    h2.value = rotate(h2.value, 32)
    h3.value = h3.value &+ h2.value
    h0.value ^= h3.value
    h3.value = rotate(h3.value, 25)
    h0.value = h0.value &+ h3.value
    h1.value ^= h0.value
    h0.value = rotate(h0.value, 63)
    h1.value = h1.value &+ h0.value
  end

  @[AlwaysInline]
  protected def self.shortMix(h0 : PUInt64, h1 : PUInt64, h2 : PUInt64, h3 : PUInt64) : Nil
    h2.value = rotate(h2.value, 50)
    h2.value = h2.value &+ h3.value
    h0.value ^= h2.value
    h3.value = rotate(h3.value, 52)
    h3.value = h3.value &+ h0.value
    h1.value ^= h3.value
    h0.value = rotate(h0.value, 30)
    h0.value = h0.value &+ h1.value
    h2.value ^= h0.value
    h1.value = rotate(h1.value, 41)
    h1.value = h1.value &+ h2.value
    h3.value ^= h1.value
    h2.value = rotate(h2.value, 54)
    h2.value = h2.value &+ h3.value
    h0.value ^= h2.value
    h3.value = rotate(h3.value, 48)
    h3.value = h3.value &+ h0.value
    h1.value ^= h3.value
    h0.value = rotate(h0.value, 38)
    h0.value = h0.value &+ h1.value
    h2.value ^= h0.value
    h1.value = rotate(h1.value, 37)
    h1.value = h1.value &+ h2.value
    h3.value ^= h1.value
    h2.value = rotate(h2.value, 62)
    h2.value = h2.value &+ h3.value
    h0.value ^= h2.value
    h3.value = rotate(h3.value, 34)
    h3.value = h3.value &+ h0.value
    h1.value ^= h3.value
    h0.value = rotate(h0.value, 5)
    h0.value = h0.value &+ h1.value
    h2.value ^= h0.value
    h1.value = rotate(h1.value, 36)
    h1.value = h1.value &+ h2.value
    h3.value ^= h1.value
  end

  @[AlwaysInline]
  protected def self.finishPartial(h0 : PUInt64, h1 : PUInt64, h2 : PUInt64, h3 : PUInt64, h4 : PUInt64,
                                   h5 : PUInt64, h6 : PUInt64, h7 : PUInt64, h8 : PUInt64, h9 : PUInt64,
                                   h10 : PUInt64, h11 : PUInt64) : Nil
    h11.value = h11.value &+ h1.value
    h2.value ^= h11.value
    h1.value = rotate(h1.value, 44)
    h0.value = h0.value &+ h2.value
    h3.value ^= h0.value
    h2.value = rotate(h2.value, 15)
    h1.value = h1.value &+ h3.value
    h4.value ^= h1.value
    h3.value = rotate(h3.value, 34)
    h2.value = h2.value &+ h4.value
    h5.value ^= h2.value
    h4.value = rotate(h4.value, 21)
    h3.value = h3.value &+ h5.value
    h6.value ^= h3.value
    h5.value = rotate(h5.value, 38)
    h4.value = h4.value &+ h6.value
    h7.value ^= h4.value
    h6.value = rotate(h6.value, 33)
    h5.value = h5.value &+ h7.value
    h8.value ^= h5.value
    h7.value = rotate(h7.value, 10)
    h6.value = h6.value &+ h8.value
    h9.value ^= h6.value
    h8.value = rotate(h8.value, 13)
    h7.value = h7.value &+ h9.value
    h10.value ^= h7.value
    h9.value = rotate(h9.value, 38)
    h8.value = h8.value &+ h10.value
    h11.value ^= h8.value
    h10.value = rotate(h10.value, 53)
    h9.value = h9.value &+ h11.value
    h0.value ^= h9.value
    h11.value = rotate(h11.value, 42)
    h10.value = h10.value &+ h0.value
    h1.value ^= h10.value
    h0.value = rotate(h0.value, 54)
  end

  @[AlwaysInline]
  protected def self.finish(data : PUInt64, h0 : PUInt64, h1 : PUInt64, h2 : PUInt64, h3 : PUInt64, h4 : PUInt64,
                            h5 : PUInt64, h6 : PUInt64, h7 : PUInt64, h8 : PUInt64, h9 : PUInt64,
                            h10 : PUInt64, h11 : PUInt64) : Nil
    h0.value = h0.value &+ little(data.[0])
    h1.value = h1.value &+ little(data.[1])
    h2.value = h2.value &+ little(data.[2])
    h3.value = h3.value &+ little(data.[3])
    h4.value = h4.value &+ little(data.[4])
    h5.value = h5.value &+ little(data.[5])
    h6.value = h6.value &+ little(data.[6])
    h7.value = h7.value &+ little(data.[7])
    h8.value = h8.value &+ little(data.[8])
    h9.value = h9.value &+ little(data.[9])
    h10.value = h10.value &+ little(data.[10])
    h11.value = h11.value &+ little(data.[11])
    finishPartial(h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11)
    finishPartial(h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11)
    finishPartial(h0, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11)
  end

  @[AlwaysInline]
  protected def self.mix(data : PUInt64, s0 : PUInt64, s1 : PUInt64, s2 : PUInt64, s3 : PUInt64, s4 : PUInt64,
                         s5 : PUInt64, s6 : PUInt64, s7 : PUInt64, s8 : PUInt64, s9 : PUInt64,
                         s10 : PUInt64, s11 : PUInt64) : Nil
    s0.value = s0.value &+ little(data[0])
    s2.value ^= s10.value
    s11.value ^= s0.value
    s0.value = rotate(s0.value, 11)
    s11.value = s11.value &+ s1.value
    s1.value = s1.value &+ little(data[1])
    s3.value ^= s11.value
    s0.value ^= s1.value
    s1.value = rotate(s1.value, 32)
    s0.value = s0.value &+ s2.value
    s2.value = s2.value &+ little(data[2])
    s4.value ^= s0.value
    s1.value ^= s2.value
    s2.value = rotate(s2.value, 43)
    s1.value = s1.value &+ s3.value
    s3.value = s3.value &+ little(data[3])
    s5.value ^= s1.value
    s2.value ^= s3.value
    s3.value = rotate(s3.value, 31)
    s2.value = s2.value &+ s4.value
    s4.value = s4.value &+ little(data[4])
    s6.value ^= s2.value
    s3.value ^= s4.value
    s4.value = rotate(s4.value, 17)
    s3.value = s3.value &+ s5.value
    s5.value = s5.value &+ little(data[5])
    s7.value ^= s3.value
    s4.value ^= s5.value
    s5.value = rotate(s5.value, 28)
    s4.value = s4.value &+ s6.value
    s6.value = s6.value &+ little(data[6])
    s8.value ^= s4.value
    s5.value ^= s6.value
    s6.value = rotate(s6.value, 39)
    s5.value = s5.value &+ s7.value
    s7.value = s7.value &+ little(data[7])
    s9.value ^= s5.value
    s6.value ^= s7.value
    s7.value = rotate(s7.value, 57)
    s6.value = s6.value &+ s8.value
    s8.value = s8.value &+ little(data[8])
    s10.value ^= s6.value
    s7.value ^= s8.value
    s8.value = rotate(s8.value, 55)
    s7.value = s7.value &+ s9.value
    s9.value = s9.value &+ little(data[9])
    s11.value ^= s7.value
    s8.value ^= s9.value
    s9.value = rotate(s9.value, 54)
    s8.value = s8.value &+ s10.value
    s10.value = s10.value &+ little(data[10])
    s0.value ^= s8.value
    s9.value ^= s10.value
    s10.value = rotate(s10.value, 22)
    s9.value = s9.value &+ s11.value
    s11.value = s11.value &+ little(data[11])
    s1.value ^= s9.value
    s10.value ^= s11.value
    s11.value = rotate(s11.value, 46)
    s10.value = s10.value &+ s0.value
  end

  @[AlwaysInline]
  protected def self.short(message : PUInt8, len : Int, hash1 : PUInt64, hash2 : PUInt64) : Nil
    buffer : Slice(UInt64) = Slice(UInt64).new(2 * SPOOKYHASH_VARIABLES, 0u64)
    bufferPtr : PUInt64 = buffer.to_unsafe

    u : SpookyHashLib::DataUnion = SpookyHashLib::DataUnion.new
    u.p8 = message

    if bitflag?(u.i, 0x07)
      (bufferPtr.unsafe_as(PUInt8)).copy_from(message, len)
      u.p64 = bufferPtr
    end

    remainder : Int32 = (len % 32).to_i32!
    a : UInt64 = hash1.value
    b : UInt64 = hash2.value
    c : UInt64 = SPOOKYHASH_CONSTANT
    d : UInt64 = SPOOKYHASH_CONSTANT
    aPtr : PUInt64 = pointerof(a)
    bPtr : PUInt64 = pointerof(b)
    cPtr : PUInt64 = pointerof(c)
    dPtr : PUInt64 = pointerof(d)

    if len > 15
      finish = u.p64 + (len.tdiv(32) &* 4)
      while u.p64 < finish
        c = c &+ little(u.p64[0])
        d = d &+ little(u.p64[1])
        shortMix(aPtr, bPtr, cPtr, dPtr)
        a = a &+ little(u.p64[2])
        b = b &+ little(u.p64[3])
        u.p64 += 4
      end

      if remainder >= 16
        c = c &+ little(u.p64[0])
        d = d &+ little(u.p64[1])
        shortMix(aPtr, bPtr, cPtr, dPtr)
        u.p64 += 2
        remainder = remainder &- 16
      end
    end

    d = d &+ len.to_u64!.unsafe_shl(56)

    # Crystal needs a fallthrough statement ^_^;
    case remainder
    when 15
      d = d &+ u.p8[14].to_u64!.unsafe_shl(48)
      d = d &+ u.p8[13].to_u64!.unsafe_shl(40)
      d = d &+ u.p8[12].to_u64!.unsafe_shl(32)
      d = d &+ u.p32[2]
      c = c &+ u.p64[0]

    when 14
      d = d &+ u.p8[13].to_u64!.unsafe_shl(40)
      d = d &+ u.p8[12].to_u64!.unsafe_shl(32)
      d = d &+ u.p32[2]
      c = c &+ u.p64[0]

    when 13
      d = d &+ u.p8[12].to_u64!.unsafe_shl(32)
      d = d &+ u.p32[2]
      c = c &+ u.p64[0]

    when 12
      d = d &+ u.p32[2]
      c = c &+ u.p64[0]

    when 11
      d = d &+ u.p8[10].to_u64!.unsafe_shl(16)
      d = d &+ u.p8[9].to_u64!.unsafe_shl(8)
      d = d &+ u.p8[8].to_u64!
      c = c &+ u.p64[0]

    when 10
      d = d &+ u.p8[9].to_u64!.unsafe_shl(8)
      d = d &+ u.p8[8].to_u64!
      c = c &+ u.p64[0]

    when 9
      d = d &+ u.p8[8].to_u64!
      c = c &+ u.p64[0]

    when 8
      c = c &+ u.p64[0]

    when 7
      c = c &+ u.p8[6].to_u64!.unsafe_shl(48)
      c = c &+ u.p8[5].to_u64!.unsafe_shl(40)
      c = c &+ u.p8[4].to_u64!.unsafe_shl(32)
      c = c &+ u.p32[0]

    when 6
      c = c &+ u.p8[5].to_u64!.unsafe_shl(40)
      c = c &+ u.p8[4].to_u64!.unsafe_shl(32)
      c = c &+ u.p32[0]

    when 5
      c = c &+ u.p8[4].to_u64!.unsafe_shl(32)
      c = c &+ u.p32[0]

    when 4
      c = c &+ u.p32[0]

    when 3
      c = c &+ u.p8[2].to_u64!.unsafe_shl(16)
      c = c &+ u.p8[1].to_u64!.unsafe_shl(8)
      c = c &+ u.p8[0].to_u64!

    when 2
      c = c &+ u.p8[1].to_u64!.unsafe_shl(8)
      c = c &+ u.p8[0].to_u64!

    when 1
      c = c &+ u.p8[0].to_u64!

    when 0
      c = c &+ SPOOKYHASH_CONSTANT
      d = d &+ SPOOKYHASH_CONSTANT
    end

    shortEnd(aPtr, bPtr, cPtr, dPtr)
    hash1.value = a
    hash2.value = b
  end
end