You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

472 lines
12 KiB

  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. #include "textflag.h"
  5. // func decode(dst, src []byte) int
  6. //
  7. // The asm code generally follows the pure Go code in decode_other.go, except
  8. // where marked with a "!!!".
  9. //
  10. // All local variables fit into registers. The non-zero stack size is only to
  11. // spill registers and push args when issuing a CALL. The register allocation:
  12. // - AX scratch
  13. // - BX scratch
  14. // - CX length or x
  15. // - DX offset
  16. // - SI &src[s]
  17. // - DI &dst[d]
  18. // + R8 dst_base
  19. // + R9 dst_len
  20. // + R10 dst_base + dst_len
  21. // + R11 src_base
  22. // + R12 src_len
  23. // + R13 src_base + src_len
  24. // - R14 used by doCopy
  25. // - R15 used by doCopy
  26. //
  27. // The registers R8-R13 (marked with a "+") are set at the start of the
  28. // function, and after a CALL returns, and are not otherwise modified.
  29. //
  30. // The d variable is implicitly DI - R8, and len(dst)-d is R10 - DI.
  31. // The s variable is implicitly SI - R11, and len(src)-s is R13 - SI.
  32. TEXT ·decode(SB), NOSPLIT, $48-56
  33. // Initialize SI, DI and R8-R13.
  34. MOVQ dst_base+0(FP), R8
  35. MOVQ dst_len+8(FP), R9
  36. MOVQ R8, DI
  37. MOVQ R8, R10
  38. ADDQ R9, R10
  39. MOVQ src_base+24(FP), R11
  40. MOVQ src_len+32(FP), R12
  41. MOVQ R11, SI
  42. MOVQ R11, R13
  43. ADDQ R12, R13
  44. loop:
  45. // for s < len(src)
  46. CMPQ SI, R13
  47. JEQ end
  48. // CX = uint32(src[s])
  49. //
  50. // switch src[s] & 0x03
  51. MOVBLZX (SI), CX
  52. MOVL CX, BX
  53. ANDL $3, BX
  54. CMPL BX, $1
  55. JAE tagCopy
  56. // ----------------------------------------
  57. // The code below handles literal tags.
  58. // case tagLiteral:
  59. // x := uint32(src[s] >> 2)
  60. // switch
  61. SHRL $2, CX
  62. CMPL CX, $60
  63. JAE tagLit60Plus
  64. // case x < 60:
  65. // s++
  66. INCQ SI
  67. doLit:
  68. // This is the end of the inner "switch", when we have a literal tag.
  69. //
  70. // We assume that CX == x and x fits in a uint32, where x is the variable
  71. // used in the pure Go decode_other.go code.
  72. // length = int(x) + 1
  73. //
  74. // Unlike the pure Go code, we don't need to check if length <= 0 because
  75. // CX can hold 64 bits, so the increment cannot overflow.
  76. INCQ CX
  77. // Prepare to check if copying length bytes will run past the end of dst or
  78. // src.
  79. //
  80. // AX = len(dst) - d
  81. // BX = len(src) - s
  82. MOVQ R10, AX
  83. SUBQ DI, AX
  84. MOVQ R13, BX
  85. SUBQ SI, BX
  86. // !!! Try a faster technique for short (16 or fewer bytes) copies.
  87. //
  88. // if length > 16 || len(dst)-d < 16 || len(src)-s < 16 {
  89. // goto callMemmove // Fall back on calling runtime·memmove.
  90. // }
  91. //
  92. // The C++ snappy code calls this TryFastAppend. It also checks len(src)-s
  93. // against 21 instead of 16, because it cannot assume that all of its input
  94. // is contiguous in memory and so it needs to leave enough source bytes to
  95. // read the next tag without refilling buffers, but Go's Decode assumes
  96. // contiguousness (the src argument is a []byte).
  97. CMPQ CX, $16
  98. JGT callMemmove
  99. CMPQ AX, $16
  100. JLT callMemmove
  101. CMPQ BX, $16
  102. JLT callMemmove
  103. // !!! Implement the copy from src to dst as a 16-byte load and store.
  104. // (Decode's documentation says that dst and src must not overlap.)
  105. //
  106. // This always copies 16 bytes, instead of only length bytes, but that's
  107. // OK. If the input is a valid Snappy encoding then subsequent iterations
  108. // will fix up the overrun. Otherwise, Decode returns a nil []byte (and a
  109. // non-nil error), so the overrun will be ignored.
  110. //
  111. // Note that on amd64, it is legal and cheap to issue unaligned 8-byte or
  112. // 16-byte loads and stores. This technique probably wouldn't be as
  113. // effective on architectures that are fussier about alignment.
  114. MOVOU 0(SI), X0
  115. MOVOU X0, 0(DI)
  116. // d += length
  117. // s += length
  118. ADDQ CX, DI
  119. ADDQ CX, SI
  120. JMP loop
  121. callMemmove:
  122. // if length > len(dst)-d || length > len(src)-s { etc }
  123. CMPQ CX, AX
  124. JGT errCorrupt
  125. CMPQ CX, BX
  126. JGT errCorrupt
  127. // copy(dst[d:], src[s:s+length])
  128. //
  129. // This means calling runtime·memmove(&dst[d], &src[s], length), so we push
  130. // DI, SI and CX as arguments. Coincidentally, we also need to spill those
  131. // three registers to the stack, to save local variables across the CALL.
  132. MOVQ DI, 0(SP)
  133. MOVQ SI, 8(SP)
  134. MOVQ CX, 16(SP)
  135. MOVQ DI, 24(SP)
  136. MOVQ SI, 32(SP)
  137. MOVQ CX, 40(SP)
  138. CALL runtime·memmove(SB)
  139. // Restore local variables: unspill registers from the stack and
  140. // re-calculate R8-R13.
  141. MOVQ 24(SP), DI
  142. MOVQ 32(SP), SI
  143. MOVQ 40(SP), CX
  144. MOVQ dst_base+0(FP), R8
  145. MOVQ dst_len+8(FP), R9
  146. MOVQ R8, R10
  147. ADDQ R9, R10
  148. MOVQ src_base+24(FP), R11
  149. MOVQ src_len+32(FP), R12
  150. MOVQ R11, R13
  151. ADDQ R12, R13
  152. // d += length
  153. // s += length
  154. ADDQ CX, DI
  155. ADDQ CX, SI
  156. JMP loop
  157. tagLit60Plus:
  158. // !!! This fragment does the
  159. //
  160. // s += x - 58; if uint(s) > uint(len(src)) { etc }
  161. //
  162. // checks. In the asm version, we code it once instead of once per switch case.
  163. ADDQ CX, SI
  164. SUBQ $58, SI
  165. MOVQ SI, BX
  166. SUBQ R11, BX
  167. CMPQ BX, R12
  168. JA errCorrupt
  169. // case x == 60:
  170. CMPL CX, $61
  171. JEQ tagLit61
  172. JA tagLit62Plus
  173. // x = uint32(src[s-1])
  174. MOVBLZX -1(SI), CX
  175. JMP doLit
  176. tagLit61:
  177. // case x == 61:
  178. // x = uint32(src[s-2]) | uint32(src[s-1])<<8
  179. MOVWLZX -2(SI), CX
  180. JMP doLit
  181. tagLit62Plus:
  182. CMPL CX, $62
  183. JA tagLit63
  184. // case x == 62:
  185. // x = uint32(src[s-3]) | uint32(src[s-2])<<8 | uint32(src[s-1])<<16
  186. MOVWLZX -3(SI), CX
  187. MOVBLZX -1(SI), BX
  188. SHLL $16, BX
  189. ORL BX, CX
  190. JMP doLit
  191. tagLit63:
  192. // case x == 63:
  193. // x = uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24
  194. MOVL -4(SI), CX
  195. JMP doLit
  196. // The code above handles literal tags.
  197. // ----------------------------------------
  198. // The code below handles copy tags.
  199. tagCopy2:
  200. // case tagCopy2:
  201. // s += 3
  202. ADDQ $3, SI
  203. // if uint(s) > uint(len(src)) { etc }
  204. MOVQ SI, BX
  205. SUBQ R11, BX
  206. CMPQ BX, R12
  207. JA errCorrupt
  208. // length = 1 + int(src[s-3])>>2
  209. SHRQ $2, CX
  210. INCQ CX
  211. // offset = int(src[s-2]) | int(src[s-1])<<8
  212. MOVWQZX -2(SI), DX
  213. JMP doCopy
  214. tagCopy:
  215. // We have a copy tag. We assume that:
  216. // - BX == src[s] & 0x03
  217. // - CX == src[s]
  218. CMPQ BX, $2
  219. JEQ tagCopy2
  220. JA errUC4T
  221. // case tagCopy1:
  222. // s += 2
  223. ADDQ $2, SI
  224. // if uint(s) > uint(len(src)) { etc }
  225. MOVQ SI, BX
  226. SUBQ R11, BX
  227. CMPQ BX, R12
  228. JA errCorrupt
  229. // offset = int(src[s-2])&0xe0<<3 | int(src[s-1])
  230. MOVQ CX, DX
  231. ANDQ $0xe0, DX
  232. SHLQ $3, DX
  233. MOVBQZX -1(SI), BX
  234. ORQ BX, DX
  235. // length = 4 + int(src[s-2])>>2&0x7
  236. SHRQ $2, CX
  237. ANDQ $7, CX
  238. ADDQ $4, CX
  239. doCopy:
  240. // This is the end of the outer "switch", when we have a copy tag.
  241. //
  242. // We assume that:
  243. // - CX == length && CX > 0
  244. // - DX == offset
  245. // if offset <= 0 { etc }
  246. CMPQ DX, $0
  247. JLE errCorrupt
  248. // if d < offset { etc }
  249. MOVQ DI, BX
  250. SUBQ R8, BX
  251. CMPQ BX, DX
  252. JLT errCorrupt
  253. // if length > len(dst)-d { etc }
  254. MOVQ R10, BX
  255. SUBQ DI, BX
  256. CMPQ CX, BX
  257. JGT errCorrupt
  258. // forwardCopy(dst[d:d+length], dst[d-offset:]); d += length
  259. //
  260. // Set:
  261. // - R14 = len(dst)-d
  262. // - R15 = &dst[d-offset]
  263. MOVQ R10, R14
  264. SUBQ DI, R14
  265. MOVQ DI, R15
  266. SUBQ DX, R15
  267. // !!! Try a faster technique for short (16 or fewer bytes) forward copies.
  268. //
  269. // First, try using two 8-byte load/stores, similar to the doLit technique
  270. // above. Even if dst[d:d+length] and dst[d-offset:] can overlap, this is
  271. // still OK if offset >= 8. Note that this has to be two 8-byte load/stores
  272. // and not one 16-byte load/store, and the first store has to be before the
  273. // second load, due to the overlap if offset is in the range [8, 16).
  274. //
  275. // if length > 16 || offset < 8 || len(dst)-d < 16 {
  276. // goto slowForwardCopy
  277. // }
  278. // copy 16 bytes
  279. // d += length
  280. CMPQ CX, $16
  281. JGT slowForwardCopy
  282. CMPQ DX, $8
  283. JLT slowForwardCopy
  284. CMPQ R14, $16
  285. JLT slowForwardCopy
  286. MOVQ 0(R15), AX
  287. MOVQ AX, 0(DI)
  288. MOVQ 8(R15), BX
  289. MOVQ BX, 8(DI)
  290. ADDQ CX, DI
  291. JMP loop
  292. slowForwardCopy:
  293. // !!! If the forward copy is longer than 16 bytes, or if offset < 8, we
  294. // can still try 8-byte load stores, provided we can overrun up to 10 extra
  295. // bytes. As above, the overrun will be fixed up by subsequent iterations
  296. // of the outermost loop.
  297. //
  298. // The C++ snappy code calls this technique IncrementalCopyFastPath. Its
  299. // commentary says:
  300. //
  301. // ----
  302. //
  303. // The main part of this loop is a simple copy of eight bytes at a time
  304. // until we've copied (at least) the requested amount of bytes. However,
  305. // if d and d-offset are less than eight bytes apart (indicating a
  306. // repeating pattern of length < 8), we first need to expand the pattern in
  307. // order to get the correct results. For instance, if the buffer looks like
  308. // this, with the eight-byte <d-offset> and <d> patterns marked as
  309. // intervals:
  310. //
  311. // abxxxxxxxxxxxx
  312. // [------] d-offset
  313. // [------] d
  314. //
  315. // a single eight-byte copy from <d-offset> to <d> will repeat the pattern
  316. // once, after which we can move <d> two bytes without moving <d-offset>:
  317. //
  318. // ababxxxxxxxxxx
  319. // [------] d-offset
  320. // [------] d
  321. //
  322. // and repeat the exercise until the two no longer overlap.
  323. //
  324. // This allows us to do very well in the special case of one single byte
  325. // repeated many times, without taking a big hit for more general cases.
  326. //
  327. // The worst case of extra writing past the end of the match occurs when
  328. // offset == 1 and length == 1; the last copy will read from byte positions
  329. // [0..7] and write to [4..11], whereas it was only supposed to write to
  330. // position 1. Thus, ten excess bytes.
  331. //
  332. // ----
  333. //
  334. // That "10 byte overrun" worst case is confirmed by Go's
  335. // TestSlowForwardCopyOverrun, which also tests the fixUpSlowForwardCopy
  336. // and finishSlowForwardCopy algorithm.
  337. //
  338. // if length > len(dst)-d-10 {
  339. // goto verySlowForwardCopy
  340. // }
  341. SUBQ $10, R14
  342. CMPQ CX, R14
  343. JGT verySlowForwardCopy
  344. makeOffsetAtLeast8:
  345. // !!! As above, expand the pattern so that offset >= 8 and we can use
  346. // 8-byte load/stores.
  347. //
  348. // for offset < 8 {
  349. // copy 8 bytes from dst[d-offset:] to dst[d:]
  350. // length -= offset
  351. // d += offset
  352. // offset += offset
  353. // // The two previous lines together means that d-offset, and therefore
  354. // // R15, is unchanged.
  355. // }
  356. CMPQ DX, $8
  357. JGE fixUpSlowForwardCopy
  358. MOVQ (R15), BX
  359. MOVQ BX, (DI)
  360. SUBQ DX, CX
  361. ADDQ DX, DI
  362. ADDQ DX, DX
  363. JMP makeOffsetAtLeast8
  364. fixUpSlowForwardCopy:
  365. // !!! Add length (which might be negative now) to d (implied by DI being
  366. // &dst[d]) so that d ends up at the right place when we jump back to the
  367. // top of the loop. Before we do that, though, we save DI to AX so that, if
  368. // length is positive, copying the remaining length bytes will write to the
  369. // right place.
  370. MOVQ DI, AX
  371. ADDQ CX, DI
  372. finishSlowForwardCopy:
  373. // !!! Repeat 8-byte load/stores until length <= 0. Ending with a negative
  374. // length means that we overrun, but as above, that will be fixed up by
  375. // subsequent iterations of the outermost loop.
  376. CMPQ CX, $0
  377. JLE loop
  378. MOVQ (R15), BX
  379. MOVQ BX, (AX)
  380. ADDQ $8, R15
  381. ADDQ $8, AX
  382. SUBQ $8, CX
  383. JMP finishSlowForwardCopy
  384. verySlowForwardCopy:
  385. // verySlowForwardCopy is a simple implementation of forward copy. In C
  386. // parlance, this is a do/while loop instead of a while loop, since we know
  387. // that length > 0. In Go syntax:
  388. //
  389. // for {
  390. // dst[d] = dst[d - offset]
  391. // d++
  392. // length--
  393. // if length == 0 {
  394. // break
  395. // }
  396. // }
  397. MOVB (R15), BX
  398. MOVB BX, (DI)
  399. INCQ R15
  400. INCQ DI
  401. DECQ CX
  402. JNZ verySlowForwardCopy
  403. JMP loop
  404. // The code above handles copy tags.
  405. // ----------------------------------------
  406. end:
  407. // This is the end of the "for s < len(src)".
  408. //
  409. // if d != len(dst) { etc }
  410. CMPQ DI, R10
  411. JNE errCorrupt
  412. // return 0
  413. MOVQ $0, ret+48(FP)
  414. RET
  415. errCorrupt:
  416. // return decodeErrCodeCorrupt
  417. MOVQ $1, ret+48(FP)
  418. RET
  419. errUC4T:
  420. // return decodeErrCodeUnsupportedCopy4Tag
  421. MOVQ $3, ret+48(FP)
  422. RET