Path: blob/main/contrib/arm-optimized-routines/string/test/memset.c
39500 views
/*1* memset test.2*3* Copyright (c) 2019-2023, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include <stdint.h>8#include <stdio.h>9#include <stdlib.h>10#include <string.h>11#include "mte.h"12#include "stringlib.h"13#include "stringtest.h"1415#define F(x, mte) {#x, x, mte},1617static const struct fun18{19const char *name;20void *(*fun) (void *s, int c, size_t n);21int test_mte;22} funtab[] = {23// clang-format off24F(memset, 0)25#if __aarch64__26F(__memset_aarch64, 1)27# if __ARM_FEATURE_SVE28F(__memset_aarch64_sve, 1)29# endif30# if WANT_MOPS31F(__memset_aarch64_mops, 1)32# endif33#elif __arm__34F(__memset_arm, 0)35#endif36{0, 0, 0}37// clang-format on38};39#undef F4041#define A 3242#define LEN 25000043static unsigned char *sbuf;4445static void *46alignup (void *p)47{48return (void *) (((uintptr_t) p + A - 1) & -A);49}5051static void52test (const struct fun *fun, int salign, int c, int len)53{54unsigned char *src = alignup (sbuf);55unsigned char *s = src + salign;56void *p;57int i;5859if (err_count >= ERR_LIMIT)60return;61if (len > LEN || salign >= A)62abort ();63for (i = 0; i < len + A; i++)64src[i] = '?';65for (i = 0; i < len; i++)66s[i] = 'a' + i % 23;6768s = tag_buffer (s, len, fun->test_mte);69p = fun->fun (s, c, len);70untag_buffer (s, len, fun->test_mte);7172if (p != s)73ERR ("%s(%p,..) returned %p\n", fun->name, s, p);7475for (i = 0; i < salign; i++)76{77if (src[i] != '?')78{79ERR ("%s(align %d, %d, %d) failed\n", fun->name, salign, c, len);80quoteat ("got", src, len + A, i);81return;82}83}84for (; i < salign + len; i++)85{86if (src[i] != (unsigned char) c)87{88ERR ("%s(align %d, %d, %d) failed\n", fun->name, salign, c, len);89quoteat ("got", src, len + A, i);90return;91}92}93for (; i < len + A; i++)94{95if (src[i] != '?')96{97ERR ("%s(align %d, %d, %d) failed\n", fun->name, salign, c, len);98quoteat ("got", src, len + A, i);99return;100}101}102}103104int105main ()106{107sbuf = mte_mmap (LEN + 2 * A);108int r = 0;109for (int i = 0; funtab[i].name; i++)110{111err_count = 0;112for (int s = 0; s < A; s++)113{114int n;115for (n = 0; n < 100; n++)116{117test (funtab + i, s, 0, n);118test (funtab + i, s, 0x25, n);119test (funtab + i, s, 0xaa25, n);120}121for (; n < LEN; n *= 2)122{123test (funtab + i, s, 0, n);124test (funtab + i, s, 0x25, n);125test (funtab + i, s, 0xaa25, n);126}127}128char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";129printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);130if (err_count)131r = -1;132}133return r;134}135136137