memmove_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright 2013 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. package runtime_test
  5. import (
  6. . "runtime"
  7. "testing"
  8. )
  9. func TestMemmove(t *testing.T) {
  10. size := 256
  11. if testing.Short() {
  12. size = 128 + 16
  13. }
  14. src := make([]byte, size)
  15. dst := make([]byte, size)
  16. for i := 0; i < size; i++ {
  17. src[i] = byte(128 + (i & 127))
  18. }
  19. for i := 0; i < size; i++ {
  20. dst[i] = byte(i & 127)
  21. }
  22. for n := 0; n <= size; n++ {
  23. for x := 0; x <= size-n; x++ { // offset in src
  24. for y := 0; y <= size-n; y++ { // offset in dst
  25. copy(dst[y:y+n], src[x:x+n])
  26. for i := 0; i < y; i++ {
  27. if dst[i] != byte(i&127) {
  28. t.Fatalf("prefix dst[%d] = %d", i, dst[i])
  29. }
  30. }
  31. for i := y; i < y+n; i++ {
  32. if dst[i] != byte(128+((i-y+x)&127)) {
  33. t.Fatalf("copied dst[%d] = %d", i, dst[i])
  34. }
  35. dst[i] = byte(i & 127) // reset dst
  36. }
  37. for i := y + n; i < size; i++ {
  38. if dst[i] != byte(i&127) {
  39. t.Fatalf("suffix dst[%d] = %d", i, dst[i])
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }
  46. func TestMemmoveAlias(t *testing.T) {
  47. size := 256
  48. if testing.Short() {
  49. size = 128 + 16
  50. }
  51. buf := make([]byte, size)
  52. for i := 0; i < size; i++ {
  53. buf[i] = byte(i)
  54. }
  55. for n := 0; n <= size; n++ {
  56. for x := 0; x <= size-n; x++ { // src offset
  57. for y := 0; y <= size-n; y++ { // dst offset
  58. copy(buf[y:y+n], buf[x:x+n])
  59. for i := 0; i < y; i++ {
  60. if buf[i] != byte(i) {
  61. t.Fatalf("prefix buf[%d] = %d", i, buf[i])
  62. }
  63. }
  64. for i := y; i < y+n; i++ {
  65. if buf[i] != byte(i-y+x) {
  66. t.Fatalf("copied buf[%d] = %d", i, buf[i])
  67. }
  68. buf[i] = byte(i) // reset buf
  69. }
  70. for i := y + n; i < size; i++ {
  71. if buf[i] != byte(i) {
  72. t.Fatalf("suffix buf[%d] = %d", i, buf[i])
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. func bmMemmove(b *testing.B, n int) {
  80. x := make([]byte, n)
  81. y := make([]byte, n)
  82. b.SetBytes(int64(n))
  83. for i := 0; i < b.N; i++ {
  84. copy(x, y)
  85. }
  86. }
  87. func BenchmarkMemmove0(b *testing.B) { bmMemmove(b, 0) }
  88. func BenchmarkMemmove1(b *testing.B) { bmMemmove(b, 1) }
  89. func BenchmarkMemmove2(b *testing.B) { bmMemmove(b, 2) }
  90. func BenchmarkMemmove3(b *testing.B) { bmMemmove(b, 3) }
  91. func BenchmarkMemmove4(b *testing.B) { bmMemmove(b, 4) }
  92. func BenchmarkMemmove5(b *testing.B) { bmMemmove(b, 5) }
  93. func BenchmarkMemmove6(b *testing.B) { bmMemmove(b, 6) }
  94. func BenchmarkMemmove7(b *testing.B) { bmMemmove(b, 7) }
  95. func BenchmarkMemmove8(b *testing.B) { bmMemmove(b, 8) }
  96. func BenchmarkMemmove9(b *testing.B) { bmMemmove(b, 9) }
  97. func BenchmarkMemmove10(b *testing.B) { bmMemmove(b, 10) }
  98. func BenchmarkMemmove11(b *testing.B) { bmMemmove(b, 11) }
  99. func BenchmarkMemmove12(b *testing.B) { bmMemmove(b, 12) }
  100. func BenchmarkMemmove13(b *testing.B) { bmMemmove(b, 13) }
  101. func BenchmarkMemmove14(b *testing.B) { bmMemmove(b, 14) }
  102. func BenchmarkMemmove15(b *testing.B) { bmMemmove(b, 15) }
  103. func BenchmarkMemmove16(b *testing.B) { bmMemmove(b, 16) }
  104. func BenchmarkMemmove32(b *testing.B) { bmMemmove(b, 32) }
  105. func BenchmarkMemmove64(b *testing.B) { bmMemmove(b, 64) }
  106. func BenchmarkMemmove128(b *testing.B) { bmMemmove(b, 128) }
  107. func BenchmarkMemmove256(b *testing.B) { bmMemmove(b, 256) }
  108. func BenchmarkMemmove512(b *testing.B) { bmMemmove(b, 512) }
  109. func BenchmarkMemmove1024(b *testing.B) { bmMemmove(b, 1024) }
  110. func BenchmarkMemmove2048(b *testing.B) { bmMemmove(b, 2048) }
  111. func BenchmarkMemmove4096(b *testing.B) { bmMemmove(b, 4096) }
  112. func TestMemclr(t *testing.T) {
  113. size := 512
  114. if testing.Short() {
  115. size = 128 + 16
  116. }
  117. mem := make([]byte, size)
  118. for i := 0; i < size; i++ {
  119. mem[i] = 0xee
  120. }
  121. for n := 0; n < size; n++ {
  122. for x := 0; x <= size-n; x++ { // offset in mem
  123. MemclrBytes(mem[x : x+n])
  124. for i := 0; i < x; i++ {
  125. if mem[i] != 0xee {
  126. t.Fatalf("overwrite prefix mem[%d] = %d", i, mem[i])
  127. }
  128. }
  129. for i := x; i < x+n; i++ {
  130. if mem[i] != 0 {
  131. t.Fatalf("failed clear mem[%d] = %d", i, mem[i])
  132. }
  133. mem[i] = 0xee
  134. }
  135. for i := x + n; i < size; i++ {
  136. if mem[i] != 0xee {
  137. t.Fatalf("overwrite suffix mem[%d] = %d", i, mem[i])
  138. }
  139. }
  140. }
  141. }
  142. }
  143. func bmMemclr(b *testing.B, n int) {
  144. x := make([]byte, n)
  145. b.SetBytes(int64(n))
  146. for i := 0; i < b.N; i++ {
  147. MemclrBytes(x)
  148. }
  149. }
  150. func BenchmarkMemclr5(b *testing.B) { bmMemclr(b, 5) }
  151. func BenchmarkMemclr16(b *testing.B) { bmMemclr(b, 16) }
  152. func BenchmarkMemclr64(b *testing.B) { bmMemclr(b, 64) }
  153. func BenchmarkMemclr256(b *testing.B) { bmMemclr(b, 256) }
  154. func BenchmarkMemclr4096(b *testing.B) { bmMemclr(b, 4096) }
  155. func BenchmarkMemclr65536(b *testing.B) { bmMemclr(b, 65536) }
  156. func BenchmarkClearFat8(b *testing.B) {
  157. for i := 0; i < b.N; i++ {
  158. var x [8 / 4]uint32
  159. _ = x
  160. }
  161. }
  162. func BenchmarkClearFat12(b *testing.B) {
  163. for i := 0; i < b.N; i++ {
  164. var x [12 / 4]uint32
  165. _ = x
  166. }
  167. }
  168. func BenchmarkClearFat16(b *testing.B) {
  169. for i := 0; i < b.N; i++ {
  170. var x [16 / 4]uint32
  171. _ = x
  172. }
  173. }
  174. func BenchmarkClearFat24(b *testing.B) {
  175. for i := 0; i < b.N; i++ {
  176. var x [24 / 4]uint32
  177. _ = x
  178. }
  179. }
  180. func BenchmarkClearFat32(b *testing.B) {
  181. for i := 0; i < b.N; i++ {
  182. var x [32 / 4]uint32
  183. _ = x
  184. }
  185. }
  186. func BenchmarkClearFat64(b *testing.B) {
  187. for i := 0; i < b.N; i++ {
  188. var x [64 / 4]uint32
  189. _ = x
  190. }
  191. }
  192. func BenchmarkClearFat128(b *testing.B) {
  193. for i := 0; i < b.N; i++ {
  194. var x [128 / 4]uint32
  195. _ = x
  196. }
  197. }
  198. func BenchmarkClearFat256(b *testing.B) {
  199. for i := 0; i < b.N; i++ {
  200. var x [256 / 4]uint32
  201. _ = x
  202. }
  203. }
  204. func BenchmarkClearFat512(b *testing.B) {
  205. for i := 0; i < b.N; i++ {
  206. var x [512 / 4]uint32
  207. _ = x
  208. }
  209. }
  210. func BenchmarkClearFat1024(b *testing.B) {
  211. for i := 0; i < b.N; i++ {
  212. var x [1024 / 4]uint32
  213. _ = x
  214. }
  215. }
  216. func BenchmarkCopyFat8(b *testing.B) {
  217. var x [8 / 4]uint32
  218. for i := 0; i < b.N; i++ {
  219. y := x
  220. _ = y
  221. }
  222. }
  223. func BenchmarkCopyFat12(b *testing.B) {
  224. var x [12 / 4]uint32
  225. for i := 0; i < b.N; i++ {
  226. y := x
  227. _ = y
  228. }
  229. }
  230. func BenchmarkCopyFat16(b *testing.B) {
  231. var x [16 / 4]uint32
  232. for i := 0; i < b.N; i++ {
  233. y := x
  234. _ = y
  235. }
  236. }
  237. func BenchmarkCopyFat24(b *testing.B) {
  238. var x [24 / 4]uint32
  239. for i := 0; i < b.N; i++ {
  240. y := x
  241. _ = y
  242. }
  243. }
  244. func BenchmarkCopyFat32(b *testing.B) {
  245. var x [32 / 4]uint32
  246. for i := 0; i < b.N; i++ {
  247. y := x
  248. _ = y
  249. }
  250. }
  251. func BenchmarkCopyFat64(b *testing.B) {
  252. var x [64 / 4]uint32
  253. for i := 0; i < b.N; i++ {
  254. y := x
  255. _ = y
  256. }
  257. }
  258. func BenchmarkCopyFat128(b *testing.B) {
  259. var x [128 / 4]uint32
  260. for i := 0; i < b.N; i++ {
  261. y := x
  262. _ = y
  263. }
  264. }
  265. func BenchmarkCopyFat256(b *testing.B) {
  266. var x [256 / 4]uint32
  267. for i := 0; i < b.N; i++ {
  268. y := x
  269. _ = y
  270. }
  271. }
  272. func BenchmarkCopyFat512(b *testing.B) {
  273. var x [512 / 4]uint32
  274. for i := 0; i < b.N; i++ {
  275. y := x
  276. _ = y
  277. }
  278. }
  279. func BenchmarkCopyFat1024(b *testing.B) {
  280. var x [1024 / 4]uint32
  281. for i := 0; i < b.N; i++ {
  282. y := x
  283. _ = y
  284. }
  285. }