本站源代码
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.

491 lines
12KB

  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. tagCopy4:
  202. // case tagCopy4:
  203. // s += 5
  204. ADDQ $5, 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-5])>>2
  211. SHRQ $2, CX
  212. INCQ CX
  213. // offset = int(uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24)
  214. MOVLQZX -4(SI), DX
  215. JMP doCopy
  216. tagCopy2:
  217. // case tagCopy2:
  218. // s += 3
  219. ADDQ $3, SI
  220. // if uint(s) > uint(len(src)) { etc }
  221. MOVQ SI, BX
  222. SUBQ R11, BX
  223. CMPQ BX, R12
  224. JA errCorrupt
  225. // length = 1 + int(src[s-3])>>2
  226. SHRQ $2, CX
  227. INCQ CX
  228. // offset = int(uint32(src[s-2]) | uint32(src[s-1])<<8)
  229. MOVWQZX -2(SI), DX
  230. JMP doCopy
  231. tagCopy:
  232. // We have a copy tag. We assume that:
  233. // - BX == src[s] & 0x03
  234. // - CX == src[s]
  235. CMPQ BX, $2
  236. JEQ tagCopy2
  237. JA tagCopy4
  238. // case tagCopy1:
  239. // s += 2
  240. ADDQ $2, SI
  241. // if uint(s) > uint(len(src)) { etc }
  242. MOVQ SI, BX
  243. SUBQ R11, BX
  244. CMPQ BX, R12
  245. JA errCorrupt
  246. // offset = int(uint32(src[s-2])&0xe0<<3 | uint32(src[s-1]))
  247. MOVQ CX, DX
  248. ANDQ $0xe0, DX
  249. SHLQ $3, DX
  250. MOVBQZX -1(SI), BX
  251. ORQ BX, DX
  252. // length = 4 + int(src[s-2])>>2&0x7
  253. SHRQ $2, CX
  254. ANDQ $7, CX
  255. ADDQ $4, CX
  256. doCopy:
  257. // This is the end of the outer "switch", when we have a copy tag.
  258. //
  259. // We assume that:
  260. // - CX == length && CX > 0
  261. // - DX == offset
  262. // if offset <= 0 { etc }
  263. CMPQ DX, $0
  264. JLE errCorrupt
  265. // if d < offset { etc }
  266. MOVQ DI, BX
  267. SUBQ R8, BX
  268. CMPQ BX, DX
  269. JLT errCorrupt
  270. // if length > len(dst)-d { etc }
  271. MOVQ R10, BX
  272. SUBQ DI, BX
  273. CMPQ CX, BX
  274. JGT errCorrupt
  275. // forwardCopy(dst[d:d+length], dst[d-offset:]); d += length
  276. //
  277. // Set:
  278. // - R14 = len(dst)-d
  279. // - R15 = &dst[d-offset]
  280. MOVQ R10, R14
  281. SUBQ DI, R14
  282. MOVQ DI, R15
  283. SUBQ DX, R15
  284. // !!! Try a faster technique for short (16 or fewer bytes) forward copies.
  285. //
  286. // First, try using two 8-byte load/stores, similar to the doLit technique
  287. // above. Even if dst[d:d+length] and dst[d-offset:] can overlap, this is
  288. // still OK if offset >= 8. Note that this has to be two 8-byte load/stores
  289. // and not one 16-byte load/store, and the first store has to be before the
  290. // second load, due to the overlap if offset is in the range [8, 16).
  291. //
  292. // if length > 16 || offset < 8 || len(dst)-d < 16 {
  293. // goto slowForwardCopy
  294. // }
  295. // copy 16 bytes
  296. // d += length
  297. CMPQ CX, $16
  298. JGT slowForwardCopy
  299. CMPQ DX, $8
  300. JLT slowForwardCopy
  301. CMPQ R14, $16
  302. JLT slowForwardCopy
  303. MOVQ 0(R15), AX
  304. MOVQ AX, 0(DI)
  305. MOVQ 8(R15), BX
  306. MOVQ BX, 8(DI)
  307. ADDQ CX, DI
  308. JMP loop
  309. slowForwardCopy:
  310. // !!! If the forward copy is longer than 16 bytes, or if offset < 8, we
  311. // can still try 8-byte load stores, provided we can overrun up to 10 extra
  312. // bytes. As above, the overrun will be fixed up by subsequent iterations
  313. // of the outermost loop.
  314. //
  315. // The C++ snappy code calls this technique IncrementalCopyFastPath. Its
  316. // commentary says:
  317. //
  318. // ----
  319. //
  320. // The main part of this loop is a simple copy of eight bytes at a time
  321. // until we've copied (at least) the requested amount of bytes. However,
  322. // if d and d-offset are less than eight bytes apart (indicating a
  323. // repeating pattern of length < 8), we first need to expand the pattern in
  324. // order to get the correct results. For instance, if the buffer looks like
  325. // this, with the eight-byte <d-offset> and <d> patterns marked as
  326. // intervals:
  327. //
  328. // abxxxxxxxxxxxx
  329. // [------] d-offset
  330. // [------] d
  331. //
  332. // a single eight-byte copy from <d-offset> to <d> will repeat the pattern
  333. // once, after which we can move <d> two bytes without moving <d-offset>:
  334. //
  335. // ababxxxxxxxxxx
  336. // [------] d-offset
  337. // [------] d
  338. //
  339. // and repeat the exercise until the two no longer overlap.
  340. //
  341. // This allows us to do very well in the special case of one single byte
  342. // repeated many times, without taking a big hit for more general cases.
  343. //
  344. // The worst case of extra writing past the end of the match occurs when
  345. // offset == 1 and length == 1; the last copy will read from byte positions
  346. // [0..7] and write to [4..11], whereas it was only supposed to write to
  347. // position 1. Thus, ten excess bytes.
  348. //
  349. // ----
  350. //
  351. // That "10 byte overrun" worst case is confirmed by Go's
  352. // TestSlowForwardCopyOverrun, which also tests the fixUpSlowForwardCopy
  353. // and finishSlowForwardCopy algorithm.
  354. //
  355. // if length > len(dst)-d-10 {
  356. // goto verySlowForwardCopy
  357. // }
  358. SUBQ $10, R14
  359. CMPQ CX, R14
  360. JGT verySlowForwardCopy
  361. makeOffsetAtLeast8:
  362. // !!! As above, expand the pattern so that offset >= 8 and we can use
  363. // 8-byte load/stores.
  364. //
  365. // for offset < 8 {
  366. // copy 8 bytes from dst[d-offset:] to dst[d:]
  367. // length -= offset
  368. // d += offset
  369. // offset += offset
  370. // // The two previous lines together means that d-offset, and therefore
  371. // // R15, is unchanged.
  372. // }
  373. CMPQ DX, $8
  374. JGE fixUpSlowForwardCopy
  375. MOVQ (R15), BX
  376. MOVQ BX, (DI)
  377. SUBQ DX, CX
  378. ADDQ DX, DI
  379. ADDQ DX, DX
  380. JMP makeOffsetAtLeast8
  381. fixUpSlowForwardCopy:
  382. // !!! Add length (which might be negative now) to d (implied by DI being
  383. // &dst[d]) so that d ends up at the right place when we jump back to the
  384. // top of the loop. Before we do that, though, we save DI to AX so that, if
  385. // length is positive, copying the remaining length bytes will write to the
  386. // right place.
  387. MOVQ DI, AX
  388. ADDQ CX, DI
  389. finishSlowForwardCopy:
  390. // !!! Repeat 8-byte load/stores until length <= 0. Ending with a negative
  391. // length means that we overrun, but as above, that will be fixed up by
  392. // subsequent iterations of the outermost loop.
  393. CMPQ CX, $0
  394. JLE loop
  395. MOVQ (R15), BX
  396. MOVQ BX, (AX)
  397. ADDQ $8, R15
  398. ADDQ $8, AX
  399. SUBQ $8, CX
  400. JMP finishSlowForwardCopy
  401. verySlowForwardCopy:
  402. // verySlowForwardCopy is a simple implementation of forward copy. In C
  403. // parlance, this is a do/while loop instead of a while loop, since we know
  404. // that length > 0. In Go syntax:
  405. //
  406. // for {
  407. // dst[d] = dst[d - offset]
  408. // d++
  409. // length--
  410. // if length == 0 {
  411. // break
  412. // }
  413. // }
  414. MOVB (R15), BX
  415. MOVB BX, (DI)
  416. INCQ R15
  417. INCQ DI
  418. DECQ CX
  419. JNZ verySlowForwardCopy
  420. JMP loop
  421. // The code above handles copy tags.
  422. // ----------------------------------------
  423. end:
  424. // This is the end of the "for s < len(src)".
  425. //
  426. // if d != len(dst) { etc }
  427. CMPQ DI, R10
  428. JNE errCorrupt
  429. // return 0
  430. MOVQ $0, ret+48(FP)
  431. RET
  432. errCorrupt:
  433. // return decodeErrCodeCorrupt
  434. MOVQ $1, ret+48(FP)
  435. RET
上海开阖软件有限公司 沪ICP备12045867号-1