Path: blob/main/contrib/arm-optimized-routines/string/bench/memset.c
39500 views
/*1* memset benchmark.2*3* Copyright (c) 2021, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#define _GNU_SOURCE8#include <stdint.h>9#include <stdio.h>10#include <string.h>11#include <assert.h>12#include "stringlib.h"13#include "benchlib.h"1415#define ITERS 500016#define ITERS2 2000000017#define ITERS3 100000018#define NUM_TESTS 1638419#define MIN_SIZE 3276820#define MAX_SIZE (1024 * 1024)2122static uint8_t a[MAX_SIZE + 4096] __attribute__((__aligned__(4096)));2324#define DOTEST(STR,TESTFN) \25printf (STR); \26RUN (TESTFN, memset); \27RUNA64 (TESTFN, __memset_aarch64); \28RUNSVE (TESTFN, __memset_aarch64_sve); \29RUNMOPS (TESTFN, __memset_mops); \30RUNA32 (TESTFN, __memset_arm); \31printf ("\n");3233typedef struct { uint32_t offset : 20, len : 12; } memset_test_t;34static memset_test_t test_arr[NUM_TESTS];3536typedef struct { uint16_t size; uint16_t freq; } freq_data_t;37typedef struct { uint8_t align; uint16_t freq; } align_data_t;3839#define SIZE_NUM 6553640#define SIZE_MASK (SIZE_NUM-1)41static uint8_t len_arr[SIZE_NUM];4243/* Frequency data for memset sizes up to 4096 based on SPEC2017. */44static freq_data_t memset_len_freq[] =45{46{40,28817}, {32,15336}, { 16,3823}, {296,3545}, { 24,3454}, { 8,1412},47{292,1202}, { 48, 927}, { 12, 613}, { 11, 539}, {284, 493}, {108, 414},48{ 88, 380}, { 20, 295}, {312, 271}, { 72, 233}, { 2, 200}, { 4, 192},49{ 15, 180}, { 14, 174}, { 13, 160}, { 56, 151}, { 36, 144}, { 64, 140},50{4095,133}, { 10, 130}, { 9, 124}, { 3, 124}, { 28, 120}, { 0, 118},51{288, 110}, {1152, 96}, {104, 90}, { 1, 86}, {832, 76}, {248, 74},52{1024, 69}, {120, 64}, {512, 63}, {384, 60}, { 6, 59}, { 80, 54},53{ 17, 50}, { 7, 49}, {520, 47}, {2048, 39}, {256, 37}, {864, 33},54{1440, 28}, { 22, 27}, {2056, 24}, {260, 23}, { 68, 23}, { 5, 22},55{ 18, 21}, {200, 18}, {2120, 18}, { 60, 17}, { 52, 16}, {336, 15},56{ 44, 13}, {192, 13}, {160, 12}, {2064, 12}, {128, 12}, { 76, 11},57{164, 11}, {152, 10}, {136, 9}, {488, 7}, { 96, 6}, {560, 6},58{1016, 6}, {112, 5}, {232, 5}, {168, 5}, {952, 5}, {184, 5},59{144, 4}, {252, 4}, { 84, 3}, {960, 3}, {3808, 3}, {244, 3},60{280, 3}, {224, 3}, {156, 3}, {1088, 3}, {440, 3}, {216, 2},61{304, 2}, { 23, 2}, { 25, 2}, { 26, 2}, {264, 2}, {328, 2},62{1096, 2}, {240, 2}, {1104, 2}, {704, 2}, {1664, 2}, {360, 2},63{808, 1}, {544, 1}, {236, 1}, {720, 1}, {368, 1}, {424, 1},64{640, 1}, {1112, 1}, {552, 1}, {272, 1}, {776, 1}, {376, 1},65{ 92, 1}, {536, 1}, {824, 1}, {496, 1}, {760, 1}, {792, 1},66{504, 1}, {344, 1}, {1816, 1}, {880, 1}, {176, 1}, {320, 1},67{352, 1}, {2008, 1}, {208, 1}, {408, 1}, {228, 1}, {2072, 1},68{568, 1}, {220, 1}, {616, 1}, {600, 1}, {392, 1}, {696, 1},69{2144, 1}, {1280, 1}, {2136, 1}, {632, 1}, {584, 1}, {456, 1},70{472, 1}, {3440, 1}, {2088, 1}, {680, 1}, {2928, 1}, {212, 1},71{648, 1}, {1752, 1}, {664, 1}, {3512, 1}, {1032, 1}, {528, 1},72{4072, 1}, {204, 1}, {2880, 1}, {3392, 1}, {712, 1}, { 59, 1},73{736, 1}, {592, 1}, {2520, 1}, {744, 1}, {196, 1}, {172, 1},74{728, 1}, {2040, 1}, {1192, 1}, {3600, 1}, {0, 0}75};7677#define ALIGN_NUM 102478#define ALIGN_MASK (ALIGN_NUM-1)79static uint8_t align_arr[ALIGN_NUM];8081/* Alignment data for memset based on SPEC2017. */82static align_data_t memset_align_freq[] =83{84{16, 338}, {8, 307}, {32, 148}, {64, 131}, {4, 72}, {1, 23}, {2, 5}, {0, 0}85};8687static void88init_memset_distribution (void)89{90int i, j, freq, size, n;9192for (n = i = 0; (freq = memset_len_freq[i].freq) != 0; i++)93for (j = 0, size = memset_len_freq[i].size; j < freq; j++)94len_arr[n++] = size;95assert (n == SIZE_NUM);9697for (n = i = 0; (freq = memset_align_freq[i].freq) != 0; i++)98for (j = 0, size = memset_align_freq[i].align; j < freq; j++)99align_arr[n++] = size - 1;100assert (n == ALIGN_NUM);101}102103static size_t104init_memset (size_t max_size)105{106size_t total = 0;107/* Create a random set of memsets with the given size and alignment108distributions. */109for (int i = 0; i < NUM_TESTS; i++)110{111test_arr[i].offset = (rand32 (0) & (max_size - 1));112test_arr[i].offset &= ~align_arr[rand32 (0) & ALIGN_MASK];113test_arr[i].len = len_arr[rand32 (0) & SIZE_MASK];114total += test_arr[i].len;115}116117return total;118}119120static void inline __attribute ((always_inline))121memset_random (const char *name, void *(*set)(void *, int, size_t))122{123uint64_t total_size = 0;124uint64_t tsum = 0;125printf ("%22s ", name);126rand32 (0x12345678);127128for (int size = MIN_SIZE; size <= MAX_SIZE; size *= 2)129{130uint64_t memset_size = init_memset (size) * ITERS;131132for (int c = 0; c < NUM_TESTS; c++)133set (a + test_arr[c].offset, 0, test_arr[c].len);134135uint64_t t = clock_get_ns ();136for (int i = 0; i < ITERS; i++)137for (int c = 0; c < NUM_TESTS; c++)138set (a + test_arr[c].offset, 0, test_arr[c].len);139t = clock_get_ns () - t;140total_size += memset_size;141tsum += t;142printf ("%dK: %5.2f ", size / 1024, (double)memset_size / t);143}144printf( "avg %5.2f\n", (double)total_size / tsum);145}146147static void inline __attribute ((always_inline))148memset_medium (const char *name, void *(*set)(void *, int, size_t))149{150printf ("%22s ", name);151152for (int size = 8; size <= 512; size *= 2)153{154uint64_t t = clock_get_ns ();155for (int i = 0; i < ITERS2; i++)156set (a, 0, size);157t = clock_get_ns () - t;158printf ("%dB: %5.2f ", size, (double)size * ITERS2 / t);159}160printf ("\n");161}162163static void inline __attribute ((always_inline))164memset_large (const char *name, void *(*set)(void *, int, size_t))165{166printf ("%22s ", name);167168for (int size = 1024; size <= 65536; size *= 2)169{170uint64_t t = clock_get_ns ();171for (int i = 0; i < ITERS3; i++)172set (a, 0, size);173t = clock_get_ns () - t;174printf ("%dKB: %6.2f ", size / 1024, (double)size * ITERS3 / t);175}176printf ("\n");177}178179int main (void)180{181init_memset_distribution ();182183memset (a, 1, sizeof (a));184185DOTEST ("Random memset (bytes/ns):\n", memset_random);186DOTEST ("Medium memset (bytes/ns):\n", memset_medium);187DOTEST ("Large memset (bytes/ns):\n", memset_large);188return 0;189}190191192