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 TestMemclr(t *testing.T) { 120 size := 512 121 if testing.Short() { 122 size = 128 + 16 123 } 124 mem := make([]byte, size) 125 for i := 0; i < size; i++ { 126 mem[i] = 0xee 127 } 128 for n := 0; n < size; n++ { 129 for x := 0; x <= size-n; x++ { // offset in mem 130 MemclrBytes(mem[x : x+n]) 131 for i := 0; i < x; i++ { 132 if mem[i] != 0xee { 133 t.Fatalf("overwrite prefix mem[%d] = %d", i, mem[i]) 134 } 135 } 136 for i := x; i < x+n; i++ { 137 if mem[i] != 0 { 138 t.Fatalf("failed clear mem[%d] = %d", i, mem[i]) 139 } 140 mem[i] = 0xee 141 } 142 for i := x + n; i < size; i++ { 143 if mem[i] != 0xee { 144 t.Fatalf("overwrite suffix mem[%d] = %d", i, mem[i]) 145 } 146 } 147 } 148 } 149 } 150 151 func bmMemclr(b *testing.B, n int) { 152 x := make([]byte, n) 153 b.SetBytes(int64(n)) 154 for i := 0; i < b.N; i++ { 155 MemclrBytes(x) 156 } 157 } 158 func BenchmarkMemclr5(b *testing.B) { bmMemclr(b, 5) } 159 func BenchmarkMemclr16(b *testing.B) { bmMemclr(b, 16) } 160 func BenchmarkMemclr64(b *testing.B) { bmMemclr(b, 64) } 161 func BenchmarkMemclr256(b *testing.B) { bmMemclr(b, 256) } 162 func BenchmarkMemclr4096(b *testing.B) { bmMemclr(b, 4096) } 163 func BenchmarkMemclr65536(b *testing.B) { bmMemclr(b, 65536) } 164 165 func bmGoMemclr(b *testing.B, n int) { 166 x := make([]byte, n) 167 b.SetBytes(int64(n)) 168 for i := 0; i < b.N; i++ { 169 for j := range x { 170 x[j] = 0 171 } 172 } 173 } 174 func BenchmarkGoMemclr5(b *testing.B) { bmGoMemclr(b, 5) } 175 func BenchmarkGoMemclr16(b *testing.B) { bmGoMemclr(b, 16) } 176 func BenchmarkGoMemclr64(b *testing.B) { bmGoMemclr(b, 64) } 177 func BenchmarkGoMemclr256(b *testing.B) { bmGoMemclr(b, 256) } 178 179 func BenchmarkClearFat8(b *testing.B) { 180 for i := 0; i < b.N; i++ { 181 var x [8 / 4]uint32 182 _ = x 183 } 184 } 185 func BenchmarkClearFat12(b *testing.B) { 186 for i := 0; i < b.N; i++ { 187 var x [12 / 4]uint32 188 _ = x 189 } 190 } 191 func BenchmarkClearFat16(b *testing.B) { 192 for i := 0; i < b.N; i++ { 193 var x [16 / 4]uint32 194 _ = x 195 } 196 } 197 func BenchmarkClearFat24(b *testing.B) { 198 for i := 0; i < b.N; i++ { 199 var x [24 / 4]uint32 200 _ = x 201 } 202 } 203 func BenchmarkClearFat32(b *testing.B) { 204 for i := 0; i < b.N; i++ { 205 var x [32 / 4]uint32 206 _ = x 207 } 208 } 209 func BenchmarkClearFat40(b *testing.B) { 210 for i := 0; i < b.N; i++ { 211 var x [40 / 4]uint32 212 _ = x 213 } 214 } 215 func BenchmarkClearFat48(b *testing.B) { 216 for i := 0; i < b.N; i++ { 217 var x [48 / 4]uint32 218 _ = x 219 } 220 } 221 func BenchmarkClearFat56(b *testing.B) { 222 for i := 0; i < b.N; i++ { 223 var x [56 / 4]uint32 224 _ = x 225 } 226 } 227 func BenchmarkClearFat64(b *testing.B) { 228 for i := 0; i < b.N; i++ { 229 var x [64 / 4]uint32 230 _ = x 231 } 232 } 233 func BenchmarkClearFat128(b *testing.B) { 234 for i := 0; i < b.N; i++ { 235 var x [128 / 4]uint32 236 _ = x 237 } 238 } 239 func BenchmarkClearFat256(b *testing.B) { 240 for i := 0; i < b.N; i++ { 241 var x [256 / 4]uint32 242 _ = x 243 } 244 } 245 func BenchmarkClearFat512(b *testing.B) { 246 for i := 0; i < b.N; i++ { 247 var x [512 / 4]uint32 248 _ = x 249 } 250 } 251 func BenchmarkClearFat1024(b *testing.B) { 252 for i := 0; i < b.N; i++ { 253 var x [1024 / 4]uint32 254 _ = x 255 } 256 } 257 258 func BenchmarkCopyFat8(b *testing.B) { 259 var x [8 / 4]uint32 260 for i := 0; i < b.N; i++ { 261 y := x 262 _ = y 263 } 264 } 265 func BenchmarkCopyFat12(b *testing.B) { 266 var x [12 / 4]uint32 267 for i := 0; i < b.N; i++ { 268 y := x 269 _ = y 270 } 271 } 272 func BenchmarkCopyFat16(b *testing.B) { 273 var x [16 / 4]uint32 274 for i := 0; i < b.N; i++ { 275 y := x 276 _ = y 277 } 278 } 279 func BenchmarkCopyFat24(b *testing.B) { 280 var x [24 / 4]uint32 281 for i := 0; i < b.N; i++ { 282 y := x 283 _ = y 284 } 285 } 286 func BenchmarkCopyFat32(b *testing.B) { 287 var x [32 / 4]uint32 288 for i := 0; i < b.N; i++ { 289 y := x 290 _ = y 291 } 292 } 293 func BenchmarkCopyFat64(b *testing.B) { 294 var x [64 / 4]uint32 295 for i := 0; i < b.N; i++ { 296 y := x 297 _ = y 298 } 299 } 300 func BenchmarkCopyFat128(b *testing.B) { 301 var x [128 / 4]uint32 302 for i := 0; i < b.N; i++ { 303 y := x 304 _ = y 305 } 306 } 307 func BenchmarkCopyFat256(b *testing.B) { 308 var x [256 / 4]uint32 309 for i := 0; i < b.N; i++ { 310 y := x 311 _ = y 312 } 313 } 314 func BenchmarkCopyFat512(b *testing.B) { 315 var x [512 / 4]uint32 316 for i := 0; i < b.N; i++ { 317 y := x 318 _ = y 319 } 320 } 321 func BenchmarkCopyFat1024(b *testing.B) { 322 var x [1024 / 4]uint32 323 for i := 0; i < b.N; i++ { 324 y := x 325 _ = y 326 } 327 } 328