Path: blob/main/contrib/arm-optimized-routines/string/test/strcmp.c
39491 views
/*1* strcmp test.2*3* Copyright (c) 2019-2022, 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;20int (*fun) (const char *s1, const char *s2);21int test_mte;22} funtab[] = {23// clang-format off24F(strcmp, 0)25#if __aarch64__26F(__strcmp_aarch64, 1)27# if __ARM_FEATURE_SVE28F(__strcmp_aarch64_sve, 1)29# endif30#elif __arm__31# if __ARM_ARCH >= 7 && __ARM_ARCH_ISA_ARM >= 132F(__strcmp_arm, 0)33# elif __ARM_ARCH == 6 && __ARM_ARCH_6M__ >= 134F(__strcmp_armv6m, 0)35# endif36#endif37{0, 0, 0}38// clang-format on39};40#undef F4142#define A 3243#define LEN 25000044static char *s1buf;45static char *s2buf;4647static void *48alignup (void *p)49{50return (void *) (((uintptr_t) p + A - 1) & -A);51}5253static void54test (const struct fun *fun, int s1align, int s2align, int len, int diffpos,55int delta)56{57char *src1 = alignup (s1buf);58char *src2 = alignup (s2buf);59char *s1 = src1 + s1align;60char *s2 = src2 + s2align;61int r;6263if (err_count >= ERR_LIMIT)64return;65if (len > LEN || s1align >= A || s2align >= A)66abort ();67if (diffpos >= len)68abort ();69if ((diffpos < 0) != (delta == 0))70abort ();7172for (int i = 0; i < len + A; i++)73src1[i] = src2[i] = '?';74for (int i = 0; i < len; i++)75s1[i] = s2[i] = 'a' + i % 23;76if (delta)77s1[diffpos] += delta;78s1[len] = s2[len] = '\0';7980s1 = tag_buffer (s1, len + 1, fun->test_mte);81s2 = tag_buffer (s2, len + 1, fun->test_mte);82r = fun->fun (s1, s2);83untag_buffer (s1, len + 1, fun->test_mte);84untag_buffer (s2, len + 1, fun->test_mte);8586if ((delta == 0 && r != 0) || (delta > 0 && r <= 0) || (delta < 0 && r >= 0))87{88ERR ("%s(align %d, align %d, %d) failed, returned %d\n", fun->name,89s1align, s2align, len, r);90quoteat ("src1", src1, len + A, diffpos);91quoteat ("src2", src2, len + A, diffpos);92}93}9495int96main ()97{98s1buf = mte_mmap (LEN + 2 * A + 1);99s2buf = mte_mmap (LEN + 2 * A + 1);100int r = 0;101for (int i = 0; funtab[i].name; i++)102{103err_count = 0;104for (int d = 0; d < A; d++)105for (int s = 0; s < A; s++)106{107int n;108test (funtab + i, d, s, 0, -1, 0);109test (funtab + i, d, s, 1, -1, 0);110test (funtab + i, d, s, 1, 0, 1);111test (funtab + i, d, s, 1, 0, -1);112for (n = 2; n < 100; n++)113{114test (funtab + i, d, s, n, -1, 0);115test (funtab + i, d, s, n, n - 1, -1);116test (funtab + i, d, s, n, n / 2, 1);117}118for (; n < LEN; n *= 2)119{120test (funtab + i, d, s, n, -1, 0);121test (funtab + i, d, s, n, n / 2, -1);122}123}124char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";125printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);126if (err_count)127r = -1;128}129return r;130}131132133