decode_arm64.s 12 KB

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