encode_amd64.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // +build !appengine
  2. // +build !noasm
  3. // +build gc
  4. package s2
  5. func init() {
  6. avxAvailable = cpu.avx()
  7. }
  8. // encodeBlock encodes a non-empty src to a guaranteed-large-enough dst. It
  9. // assumes that the varint-encoded length of the decompressed bytes has already
  10. // been written.
  11. //
  12. // It also assumes that:
  13. // len(dst) >= MaxEncodedLen(len(src)) &&
  14. // minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
  15. func encodeBlock(dst, src []byte) (d int) {
  16. const (
  17. // Use 12 bit table when less than...
  18. limit12B = 16 << 10
  19. // Use 10 bit table when less than...
  20. limit10B = 4 << 10
  21. // Use 8 bit table when less than...
  22. limit8B = 512
  23. )
  24. if avxAvailable {
  25. // Big blocks, use full table...
  26. if len(src) >= limit12B {
  27. return encodeBlockAsmAvx(dst, src)
  28. }
  29. if len(src) >= limit10B {
  30. return encodeBlockAsm12BAvx(dst, src)
  31. }
  32. if len(src) >= limit8B {
  33. return encodeBlockAsm10BAvx(dst, src)
  34. }
  35. if len(src) < minNonLiteralBlockSize {
  36. return 0
  37. }
  38. return encodeBlockAsm8BAvx(dst, src)
  39. }
  40. if len(src) >= limit12B {
  41. return encodeBlockAsm(dst, src)
  42. }
  43. if len(src) >= limit10B {
  44. return encodeBlockAsm12B(dst, src)
  45. }
  46. if len(src) >= limit8B {
  47. return encodeBlockAsm10B(dst, src)
  48. }
  49. if len(src) < minNonLiteralBlockSize {
  50. return 0
  51. }
  52. return encodeBlockAsm8B(dst, src)
  53. }
  54. // encodeBlockSnappy encodes a non-empty src to a guaranteed-large-enough dst. It
  55. // assumes that the varint-encoded length of the decompressed bytes has already
  56. // been written.
  57. //
  58. // It also assumes that:
  59. // len(dst) >= MaxEncodedLen(len(src)) &&
  60. // minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
  61. func encodeBlockSnappy(dst, src []byte) (d int) {
  62. const (
  63. // Use 12 bit table when less than...
  64. limit12B = 16 << 10
  65. // Use 10 bit table when less than...
  66. limit10B = 4 << 10
  67. // Use 8 bit table when less than...
  68. limit8B = 512
  69. )
  70. if avxAvailable {
  71. // Big blocks, use full table...
  72. if len(src) >= limit12B {
  73. return encodeSnappyBlockAsmAvx(dst, src)
  74. }
  75. if len(src) >= limit10B {
  76. return encodeSnappyBlockAsm12BAvx(dst, src)
  77. }
  78. if len(src) >= limit8B {
  79. return encodeSnappyBlockAsm10BAvx(dst, src)
  80. }
  81. if len(src) < minNonLiteralBlockSize {
  82. return 0
  83. }
  84. return encodeSnappyBlockAsm8BAvx(dst, src)
  85. }
  86. if len(src) >= limit12B {
  87. return encodeSnappyBlockAsm(dst, src)
  88. }
  89. if len(src) >= limit10B {
  90. return encodeSnappyBlockAsm12B(dst, src)
  91. }
  92. if len(src) >= limit8B {
  93. return encodeSnappyBlockAsm10B(dst, src)
  94. }
  95. if len(src) < minNonLiteralBlockSize {
  96. return 0
  97. }
  98. return encodeSnappyBlockAsm8B(dst, src)
  99. }