decode_amd64.s 12 KB

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