md5-util_amd64.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2020 MinIO Inc. All rights reserved.
  2. // Use of this source code is governed by a license that can be
  3. // found in the LICENSE file.
  4. package md5simd
  5. import (
  6. "sort"
  7. )
  8. // Helper struct for sorting blocks based on length
  9. type lane struct {
  10. len uint
  11. pos uint
  12. }
  13. // Helper struct for generating number of rounds in combination with mask for valid lanes
  14. type maskRounds struct {
  15. mask uint64
  16. rounds uint64
  17. }
  18. func generateMaskAndRounds8(input [8][]byte, mr *[8]maskRounds) (rounds int) {
  19. // Sort on blocks length small to large
  20. var sorted [8]lane
  21. for c, inpt := range input {
  22. sorted[c] = lane{uint(len(inpt)), uint(c)}
  23. }
  24. sort.Slice(sorted[:], func(i, j int) bool { return sorted[i].len < sorted[j].len })
  25. // Create mask array including 'rounds' (of processing blocks of 64 bytes) between masks
  26. m, round := uint64(0xff), uint64(0)
  27. for _, s := range sorted {
  28. if s.len > 0 {
  29. if uint64(s.len)>>6 > round {
  30. mr[rounds] = maskRounds{m, (uint64(s.len) >> 6) - round}
  31. rounds++
  32. }
  33. round = uint64(s.len) >> 6
  34. }
  35. m = m & ^(1 << uint(s.pos))
  36. }
  37. return
  38. }
  39. func generateMaskAndRounds16(input [16][]byte, mr *[16]maskRounds) (rounds int) {
  40. // Sort on blocks length small to large
  41. var sorted [16]lane
  42. for c, inpt := range input {
  43. sorted[c] = lane{uint(len(inpt)), uint(c)}
  44. }
  45. sort.Slice(sorted[:], func(i, j int) bool { return sorted[i].len < sorted[j].len })
  46. // Create mask array including 'rounds' (of processing blocks of 64 bytes) between masks
  47. m, round := uint64(0xffff), uint64(0)
  48. for _, s := range sorted {
  49. if s.len > 0 {
  50. if uint64(s.len)>>6 > round {
  51. mr[rounds] = maskRounds{m, (uint64(s.len) >> 6) - round}
  52. rounds++
  53. }
  54. round = uint64(s.len) >> 6
  55. }
  56. m = m & ^(1 << uint(s.pos))
  57. }
  58. return
  59. }