1 /* Measure memcpy performance.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #define MIN_PAGE_SIZE 131072
21 #define TEST_NAME "memcpy"
22 #include "bench-string.h"
27 #define NUM_COPIES 4096
29 typedef struct { uint16_t size
; uint16_t freq
; } freq_data_t
;
30 typedef struct { uint8_t align
; uint8_t freq
; } align_data_t
;
33 #define SIZE_MASK (SIZE_NUM-1)
34 static uint8_t size_arr
[SIZE_NUM
];
36 /* Frequency data for memcpy of less than 256 bytes based on SPEC2006. */
37 static freq_data_t size_freq
[] =
39 { 8, 576}, {104, 94}, { 24, 78}, { 48, 58}, { 32, 48}, { 16, 46},
40 { 1, 30}, { 96, 12}, { 72, 11}, {216, 11}, {192, 8}, { 12, 7},
41 {144, 5}, { 2, 4}, { 64, 4}, {120, 4}, { 4, 3}, { 40, 2},
42 { 7, 2}, {168, 2}, {160, 2}, {128, 1}, { 3, 1}, { 9, 1},
43 {176, 1}, {240, 1}, { 11, 1}, { 0, 1}, { 5, 1}, { 6, 1},
44 { 80, 1}, { 52, 1}, {152, 1}, { 10, 1}, { 56, 1}, { 51, 1},
45 { 14, 1}, {208, 1}, { 0, 0}
49 #define ALIGN_MASK (ALIGN_NUM-1)
50 static uint8_t src_align_arr
[ALIGN_NUM
];
51 static uint8_t dst_align_arr
[ALIGN_NUM
];
53 /* Source alignment frequency for memcpy based on SPEC2006. */
54 static align_data_t src_align_freq
[] =
56 {16, 144}, {8, 86}, {3, 23}, {1, 3}, {0, 0}
59 /* Destination alignment frequency for memcpy based on SPEC2006. */
60 static align_data_t dst_align_freq
[] =
62 {16, 197}, {8, 30}, {3, 23}, {1, 6}, {0, 0}
72 static copy_t copy
[NUM_COPIES
];
74 typedef char *(*proto_t
) (char *, const char *, size_t);
77 init_copy_distribution (void)
79 int i
, j
, freq
, size
, n
;
81 for (n
= i
= 0; (freq
= size_freq
[i
].freq
) != 0; i
++)
82 for (j
= 0, size
= size_freq
[i
].size
; j
< freq
; j
++)
84 assert (n
== SIZE_NUM
);
86 for (n
= i
= 0; (freq
= src_align_freq
[i
].freq
) != 0; i
++)
87 for (j
= 0, size
= src_align_freq
[i
].align
; j
< freq
; j
++)
88 src_align_arr
[n
++] = size
- 1;
89 assert (n
== ALIGN_NUM
);
91 for (n
= i
= 0; (freq
= dst_align_freq
[i
].freq
) != 0; i
++)
92 for (j
= 0, size
= dst_align_freq
[i
].align
; j
< freq
; j
++)
93 dst_align_arr
[n
++] = size
- 1;
94 assert (n
== ALIGN_NUM
);
99 do_one_test (impl_t
*impl
, char *dst
, char *src
, copy_t
*copy
, size_t n
)
101 timing_t start
, stop
, cur
;
102 size_t iters
= INNER_LOOP_ITERS
* 20;
105 for (int i
= 0; i
< iters
; ++i
)
106 for (int j
= 0; j
< n
; j
++)
107 CALL (impl
, dst
+ copy
[j
].dst
, src
+ copy
[j
].src
, copy
[j
].len
);
110 TIMING_DIFF (cur
, start
, stop
);
112 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
116 do_test (size_t max_size
)
118 for (int i
= 0; i
< max_size
; i
++)
121 /* Create a random set of copies with the given size and alignment
123 for (int i
= 0; i
< NUM_COPIES
; i
++)
125 copy
[i
].dst
= (rand () & (max_size
- 1)) | 1;
126 copy
[i
].dst
&= ~dst_align_arr
[rand () & ALIGN_MASK
];
127 copy
[i
].src
= (rand () & (max_size
- 1)) | 3;
128 copy
[i
].src
&= ~src_align_arr
[rand () & ALIGN_MASK
];
129 copy
[i
].len
= size_arr
[rand () & SIZE_MASK
];
132 printf ("Memory size %6zd:", max_size
);
134 FOR_EACH_IMPL (impl
, 0)
135 do_one_test (impl
, (char *) buf2
, (char *) buf1
, copy
, NUM_COPIES
);
144 init_copy_distribution ();
147 FOR_EACH_IMPL (impl
, 0)
148 printf ("\t%s", impl
->name
);
151 for (int i
= 4; i
<= 64; i
= i
* 2)
157 #include <support/test-driver.c>