github.com/aloncn/graphics-go@v0.0.1/src/runtime/memmove_test.go (about) 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 5 package runtime_test 6 7 import ( 8 . "runtime" 9 "testing" 10 ) 11 12 func TestMemmove(t *testing.T) { 13 size := 256 14 if testing.Short() { 15 size = 128 + 16 16 } 17 src := make([]byte, size) 18 dst := make([]byte, size) 19 for i := 0; i < size; i++ { 20 src[i] = byte(128 + (i & 127)) 21 } 22 for i := 0; i < size; i++ { 23 dst[i] = byte(i & 127) 24 } 25 for n := 0; n <= size; n++ { 26 for x := 0; x <= size-n; x++ { // offset in src 27 for y := 0; y <= size-n; y++ { // offset in dst 28 copy(dst[y:y+n], src[x:x+n]) 29 for i := 0; i < y; i++ { 30 if dst[i] != byte(i&127) { 31 t.Fatalf("prefix dst[%d] = %d", i, dst[i]) 32 } 33 } 34 for i := y; i < y+n; i++ { 35 if dst[i] != byte(128+((i-y+x)&127)) { 36 t.Fatalf("copied dst[%d] = %d", i, dst[i]) 37 } 38 dst[i] = byte(i & 127) // reset dst 39 } 40 for i := y + n; i < size; i++ { 41 if dst[i] != byte(i&127) { 42 t.Fatalf("suffix dst[%d] = %d", i, dst[i]) 43 } 44 } 45 } 46 } 47 } 48 } 49 50 func TestMemmoveAlias(t *testing.T) { 51 size := 256 52 if testing.Short() { 53 size = 128 + 16 54 } 55 buf := make([]byte, size) 56 for i := 0; i < size; i++ { 57 buf[i] = byte(i) 58 } 59 for n := 0; n <= size; n++ { 60 for x := 0; x <= size-n; x++ { // src offset 61 for y := 0; y <= size-n; y++ { // dst offset 62 copy(buf[y:y+n], buf[x:x+n]) 63 for i := 0; i < y; i++ { 64 if buf[i] != byte(i) { 65 t.Fatalf("prefix buf[%d] = %d", i, buf[i]) 66 } 67 } 68 for i := y; i < y+n; i++ { 69 if buf[i] != byte(i-y+x) { 70 t.Fatalf("copied buf[%d] = %d", i, buf[i]) 71 } 72 buf[i] = byte(i) // reset buf 73 } 74 for i := y + n; i < size; i++ { 75 if buf[i] != byte(i) { 76 t.Fatalf("suffix buf[%d] = %d", i, buf[i]) 77 } 78 } 79 } 80 } 81 } 82 } 83 84 func bmMemmove(b *testing.B, n int) { 85 x := make([]byte, n) 86 y := make([]byte, n) 87 b.SetBytes(int64(n)) 88 for i := 0; i < b.N; i++ { 89 copy(x, y) 90 } 91 } 92 93 func BenchmarkMemmove0(b *testing.B) { bmMemmove(b, 0) } 94 func BenchmarkMemmove1(b *testing.B) { bmMemmove(b, 1) } 95 func BenchmarkMemmove2(b *testing.B) { bmMemmove(b, 2) } 96 func BenchmarkMemmove3(b *testing.B) { bmMemmove(b, 3) } 97 func BenchmarkMemmove4(b *testing.B) { bmMemmove(b, 4) } 98 func BenchmarkMemmove5(b *testing.B) { bmMemmove(b, 5) } 99 func BenchmarkMemmove6(b *testing.B) { bmMemmove(b, 6) } 100 func BenchmarkMemmove7(b *testing.B) { bmMemmove(b, 7) } 101 func BenchmarkMemmove8(b *testing.B) { bmMemmove(b, 8) } 102 func BenchmarkMemmove9(b *testing.B) { bmMemmove(b, 9) } 103 func BenchmarkMemmove10(b *testing.B) { bmMemmove(b, 10) } 104 func BenchmarkMemmove11(b *testing.B) { bmMemmove(b, 11) } 105 func BenchmarkMemmove12(b *testing.B) { bmMemmove(b, 12) } 106 func BenchmarkMemmove13(b *testing.B) { bmMemmove(b, 13) } 107 func BenchmarkMemmove14(b *testing.B) { bmMemmove(b, 14) } 108 func BenchmarkMemmove15(b *testing.B) { bmMemmove(b, 15) } 109 func BenchmarkMemmove16(b *testing.B) { bmMemmove(b, 16) } 110 func BenchmarkMemmove32(b *testing.B) { bmMemmove(b, 32) } 111 func BenchmarkMemmove64(b *testing.B) { bmMemmove(b, 64) } 112 func BenchmarkMemmove128(b *testing.B) { bmMemmove(b, 128) } 113 func BenchmarkMemmove256(b *testing.B) { bmMemmove(b, 256) } 114 func BenchmarkMemmove512(b *testing.B) { bmMemmove(b, 512) } 115 func BenchmarkMemmove1024(b *testing.B) { bmMemmove(b, 1024) } 116 func BenchmarkMemmove2048(b *testing.B) { bmMemmove(b, 2048) } 117 func BenchmarkMemmove4096(b *testing.B) { bmMemmove(b, 4096) } 118 119 func bmMemmoveUnaligned(b *testing.B, n int) { 120 x := make([]byte, n+1) 121 y := make([]byte, n) 122 b.SetBytes(int64(n)) 123 for i := 0; i < b.N; i++ { 124 copy(x[1:], y) 125 } 126 } 127 128 func BenchmarkMemmoveUnaligned0(b *testing.B) { bmMemmoveUnaligned(b, 0) } 129 func BenchmarkMemmoveUnaligned1(b *testing.B) { bmMemmoveUnaligned(b, 1) } 130 func BenchmarkMemmoveUnaligned2(b *testing.B) { bmMemmoveUnaligned(b, 2) } 131 func BenchmarkMemmoveUnaligned3(b *testing.B) { bmMemmoveUnaligned(b, 3) } 132 func BenchmarkMemmoveUnaligned4(b *testing.B) { bmMemmoveUnaligned(b, 4) } 133 func BenchmarkMemmoveUnaligned5(b *testing.B) { bmMemmoveUnaligned(b, 5) } 134 func BenchmarkMemmoveUnaligned6(b *testing.B) { bmMemmoveUnaligned(b, 6) } 135 func BenchmarkMemmoveUnaligned7(b *testing.B) { bmMemmoveUnaligned(b, 7) } 136 func BenchmarkMemmoveUnaligned8(b *testing.B) { bmMemmoveUnaligned(b, 8) } 137 func BenchmarkMemmoveUnaligned9(b *testing.B) { bmMemmoveUnaligned(b, 9) } 138 func BenchmarkMemmoveUnaligned10(b *testing.B) { bmMemmoveUnaligned(b, 10) } 139 func BenchmarkMemmoveUnaligned11(b *testing.B) { bmMemmoveUnaligned(b, 11) } 140 func BenchmarkMemmoveUnaligned12(b *testing.B) { bmMemmoveUnaligned(b, 12) } 141 func BenchmarkMemmoveUnaligned13(b *testing.B) { bmMemmoveUnaligned(b, 13) } 142 func BenchmarkMemmoveUnaligned14(b *testing.B) { bmMemmoveUnaligned(b, 14) } 143 func BenchmarkMemmoveUnaligned15(b *testing.B) { bmMemmoveUnaligned(b, 15) } 144 func BenchmarkMemmoveUnaligned16(b *testing.B) { bmMemmoveUnaligned(b, 16) } 145 func BenchmarkMemmoveUnaligned32(b *testing.B) { bmMemmoveUnaligned(b, 32) } 146 func BenchmarkMemmoveUnaligned64(b *testing.B) { bmMemmoveUnaligned(b, 64) } 147 func BenchmarkMemmoveUnaligned128(b *testing.B) { bmMemmoveUnaligned(b, 128) } 148 func BenchmarkMemmoveUnaligned256(b *testing.B) { bmMemmoveUnaligned(b, 256) } 149 func BenchmarkMemmoveUnaligned512(b *testing.B) { bmMemmoveUnaligned(b, 512) } 150 func BenchmarkMemmoveUnaligned1024(b *testing.B) { bmMemmoveUnaligned(b, 1024) } 151 func BenchmarkMemmoveUnaligned2048(b *testing.B) { bmMemmoveUnaligned(b, 2048) } 152 func BenchmarkMemmoveUnaligned4096(b *testing.B) { bmMemmoveUnaligned(b, 4096) } 153 154 func TestMemclr(t *testing.T) { 155 size := 512 156 if testing.Short() { 157 size = 128 + 16 158 } 159 mem := make([]byte, size) 160 for i := 0; i < size; i++ { 161 mem[i] = 0xee 162 } 163 for n := 0; n < size; n++ { 164 for x := 0; x <= size-n; x++ { // offset in mem 165 MemclrBytes(mem[x : x+n]) 166 for i := 0; i < x; i++ { 167 if mem[i] != 0xee { 168 t.Fatalf("overwrite prefix mem[%d] = %d", i, mem[i]) 169 } 170 } 171 for i := x; i < x+n; i++ { 172 if mem[i] != 0 { 173 t.Fatalf("failed clear mem[%d] = %d", i, mem[i]) 174 } 175 mem[i] = 0xee 176 } 177 for i := x + n; i < size; i++ { 178 if mem[i] != 0xee { 179 t.Fatalf("overwrite suffix mem[%d] = %d", i, mem[i]) 180 } 181 } 182 } 183 } 184 } 185 186 func bmMemclr(b *testing.B, n int) { 187 x := make([]byte, n) 188 b.SetBytes(int64(n)) 189 for i := 0; i < b.N; i++ { 190 MemclrBytes(x) 191 } 192 } 193 func BenchmarkMemclr5(b *testing.B) { bmMemclr(b, 5) } 194 func BenchmarkMemclr16(b *testing.B) { bmMemclr(b, 16) } 195 func BenchmarkMemclr64(b *testing.B) { bmMemclr(b, 64) } 196 func BenchmarkMemclr256(b *testing.B) { bmMemclr(b, 256) } 197 func BenchmarkMemclr4096(b *testing.B) { bmMemclr(b, 4096) } 198 func BenchmarkMemclr65536(b *testing.B) { bmMemclr(b, 65536) } 199 func BenchmarkMemclr1M(b *testing.B) { bmMemclr(b, 1<<20) } 200 func BenchmarkMemclr4M(b *testing.B) { bmMemclr(b, 4<<20) } 201 func BenchmarkMemclr8M(b *testing.B) { bmMemclr(b, 8<<20) } 202 func BenchmarkMemclr16M(b *testing.B) { bmMemclr(b, 16<<20) } 203 func BenchmarkMemclr64M(b *testing.B) { bmMemclr(b, 64<<20) } 204 205 func bmGoMemclr(b *testing.B, n int) { 206 x := make([]byte, n) 207 b.SetBytes(int64(n)) 208 for i := 0; i < b.N; i++ { 209 for j := range x { 210 x[j] = 0 211 } 212 } 213 } 214 func BenchmarkGoMemclr5(b *testing.B) { bmGoMemclr(b, 5) } 215 func BenchmarkGoMemclr16(b *testing.B) { bmGoMemclr(b, 16) } 216 func BenchmarkGoMemclr64(b *testing.B) { bmGoMemclr(b, 64) } 217 func BenchmarkGoMemclr256(b *testing.B) { bmGoMemclr(b, 256) } 218 219 func BenchmarkClearFat8(b *testing.B) { 220 for i := 0; i < b.N; i++ { 221 var x [8 / 4]uint32 222 _ = x 223 } 224 } 225 func BenchmarkClearFat12(b *testing.B) { 226 for i := 0; i < b.N; i++ { 227 var x [12 / 4]uint32 228 _ = x 229 } 230 } 231 func BenchmarkClearFat16(b *testing.B) { 232 for i := 0; i < b.N; i++ { 233 var x [16 / 4]uint32 234 _ = x 235 } 236 } 237 func BenchmarkClearFat24(b *testing.B) { 238 for i := 0; i < b.N; i++ { 239 var x [24 / 4]uint32 240 _ = x 241 } 242 } 243 func BenchmarkClearFat32(b *testing.B) { 244 for i := 0; i < b.N; i++ { 245 var x [32 / 4]uint32 246 _ = x 247 } 248 } 249 func BenchmarkClearFat40(b *testing.B) { 250 for i := 0; i < b.N; i++ { 251 var x [40 / 4]uint32 252 _ = x 253 } 254 } 255 func BenchmarkClearFat48(b *testing.B) { 256 for i := 0; i < b.N; i++ { 257 var x [48 / 4]uint32 258 _ = x 259 } 260 } 261 func BenchmarkClearFat56(b *testing.B) { 262 for i := 0; i < b.N; i++ { 263 var x [56 / 4]uint32 264 _ = x 265 } 266 } 267 func BenchmarkClearFat64(b *testing.B) { 268 for i := 0; i < b.N; i++ { 269 var x [64 / 4]uint32 270 _ = x 271 } 272 } 273 func BenchmarkClearFat128(b *testing.B) { 274 for i := 0; i < b.N; i++ { 275 var x [128 / 4]uint32 276 _ = x 277 } 278 } 279 func BenchmarkClearFat256(b *testing.B) { 280 for i := 0; i < b.N; i++ { 281 var x [256 / 4]uint32 282 _ = x 283 } 284 } 285 func BenchmarkClearFat512(b *testing.B) { 286 for i := 0; i < b.N; i++ { 287 var x [512 / 4]uint32 288 _ = x 289 } 290 } 291 func BenchmarkClearFat1024(b *testing.B) { 292 for i := 0; i < b.N; i++ { 293 var x [1024 / 4]uint32 294 _ = x 295 } 296 } 297 298 func BenchmarkCopyFat8(b *testing.B) { 299 var x [8 / 4]uint32 300 for i := 0; i < b.N; i++ { 301 y := x 302 _ = y 303 } 304 } 305 func BenchmarkCopyFat12(b *testing.B) { 306 var x [12 / 4]uint32 307 for i := 0; i < b.N; i++ { 308 y := x 309 _ = y 310 } 311 } 312 func BenchmarkCopyFat16(b *testing.B) { 313 var x [16 / 4]uint32 314 for i := 0; i < b.N; i++ { 315 y := x 316 _ = y 317 } 318 } 319 func BenchmarkCopyFat24(b *testing.B) { 320 var x [24 / 4]uint32 321 for i := 0; i < b.N; i++ { 322 y := x 323 _ = y 324 } 325 } 326 func BenchmarkCopyFat32(b *testing.B) { 327 var x [32 / 4]uint32 328 for i := 0; i < b.N; i++ { 329 y := x 330 _ = y 331 } 332 } 333 func BenchmarkCopyFat64(b *testing.B) { 334 var x [64 / 4]uint32 335 for i := 0; i < b.N; i++ { 336 y := x 337 _ = y 338 } 339 } 340 func BenchmarkCopyFat128(b *testing.B) { 341 var x [128 / 4]uint32 342 for i := 0; i < b.N; i++ { 343 y := x 344 _ = y 345 } 346 } 347 func BenchmarkCopyFat256(b *testing.B) { 348 var x [256 / 4]uint32 349 for i := 0; i < b.N; i++ { 350 y := x 351 _ = y 352 } 353 } 354 func BenchmarkCopyFat512(b *testing.B) { 355 var x [512 / 4]uint32 356 for i := 0; i < b.N; i++ { 357 y := x 358 _ = y 359 } 360 } 361 func BenchmarkCopyFat1024(b *testing.B) { 362 var x [1024 / 4]uint32 363 for i := 0; i < b.N; i++ { 364 y := x 365 _ = y 366 } 367 }