|
| 1 | +#include <stdbool.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <sys/types.h> |
| 6 | +#include <sys/wait.h> |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | +static bool safe_try_feature(bool (*try_feature)(void)); |
| 10 | + |
| 11 | +static bool any_fails = false; |
| 12 | + |
| 13 | +#if __HAVE_FUNCTION_MULTI_VERSIONING |
| 14 | +#define CHECK(X, BODY) \ |
| 15 | + __attribute__((target(#X))) \ |
| 16 | + static bool try_##X(void) { \ |
| 17 | + do \ |
| 18 | + BODY \ |
| 19 | + while (0); \ |
| 20 | + return true; \ |
| 21 | + } \ |
| 22 | + __attribute__((target_version(#X))) \ |
| 23 | + static void check_##X(void) { \ |
| 24 | + printf("%s\n", #X); \ |
| 25 | + fflush(stdout); \ |
| 26 | + if (!safe_try_feature(try_##X)) { \ |
| 27 | + printf("\tFAIL\n"); \ |
| 28 | + any_fails = true; \ |
| 29 | + } \ |
| 30 | + } \ |
| 31 | + __attribute__((target_version("default"))) \ |
| 32 | + static void check_##X(void) { \ |
| 33 | + printf("%s\n", #X); \ |
| 34 | + fflush(stdout); \ |
| 35 | + if (safe_try_feature(try_##X)) { \ |
| 36 | + printf("\tUPASS\n"); \ |
| 37 | + any_fails = true; \ |
| 38 | + } \ |
| 39 | + } |
| 40 | +#else |
| 41 | +#define CHECK(X, BODY) \ |
| 42 | + static void check_##X(void) { \ |
| 43 | + printf("%s\n", #X); \ |
| 44 | + } |
| 45 | +#endif |
| 46 | + |
| 47 | +CHECK(flagm, { |
| 48 | + asm volatile ( |
| 49 | + "cfinv" "\n" |
| 50 | + "cfinv" "\n" |
| 51 | + ); |
| 52 | +}) |
| 53 | +CHECK(flagm2, { |
| 54 | + asm volatile ( |
| 55 | + "axflag" "\n" |
| 56 | + "xaflag" "\n" |
| 57 | + ); |
| 58 | +}) |
| 59 | +CHECK(dotprod, { |
| 60 | + asm volatile ( |
| 61 | + "udot v0.4S,v1.16B,v2.16B" |
| 62 | + : : : "v0" |
| 63 | + ); |
| 64 | +}) |
| 65 | +CHECK(sha3, { |
| 66 | + asm volatile ( |
| 67 | + "fmov d0, #0" "\n" |
| 68 | + "fmov d1, #0" "\n" |
| 69 | + "eor3 v0.16b, v0.16b, v0.16b, v0.16b" "\n" |
| 70 | + : : : "v0" |
| 71 | + ); |
| 72 | +}) |
| 73 | +CHECK(rdm, { |
| 74 | + asm volatile ( |
| 75 | + "sqrdmlah s0, s1, s2" |
| 76 | + : : : "s0" |
| 77 | + ); |
| 78 | +}) |
| 79 | +CHECK(lse, { |
| 80 | + uint64_t pointee = 0; |
| 81 | + asm volatile ( |
| 82 | + "swp xzr, xzr, [%[pointee]]" |
| 83 | + : : [pointee]"r"(&pointee) |
| 84 | + ); |
| 85 | +}) |
| 86 | +CHECK(sha2, { |
| 87 | + asm volatile ( |
| 88 | + "fmov d0, #0" "\n" |
| 89 | + "fmov d1, #0" "\n" |
| 90 | + "sha256h q0, q0, v0.4s" "\n" |
| 91 | + : : : "v0" |
| 92 | + ); |
| 93 | +}) |
| 94 | +CHECK(sha1, { |
| 95 | + asm volatile ( |
| 96 | + "fmov s0, #0" "\n" |
| 97 | + "sha1h s0, s0" "\n" |
| 98 | + : : : "v0" |
| 99 | + ); |
| 100 | +}) |
| 101 | +CHECK(aes, { |
| 102 | + asm volatile ( |
| 103 | + "fmov d0, #0" "\n" |
| 104 | + "fmov d1, #0" "\n" |
| 105 | + "aesd v0.16B, v0.16B" "\n" |
| 106 | + : : : "v0" |
| 107 | + ); |
| 108 | +}) |
| 109 | +CHECK(pmull, { |
| 110 | + asm volatile ( |
| 111 | + "fmov d0, #0" "\n" |
| 112 | + "pmull v0.1q, v0.1d, v0.1d" "\n" |
| 113 | + : : : "v0" |
| 114 | + ); |
| 115 | +}) |
| 116 | +CHECK(rcpc, { |
| 117 | + int x; |
| 118 | + asm volatile ( |
| 119 | + "ldaprb w0, [%0]" |
| 120 | + : : "r" (&x) : "w0" |
| 121 | + ); |
| 122 | +}) |
| 123 | +CHECK(rcpc2, { |
| 124 | + int x; |
| 125 | + asm volatile ( |
| 126 | + "ldapurb w0, [%0]" |
| 127 | + : : "r" (&x) : "w0" |
| 128 | + ); |
| 129 | +}) |
| 130 | +CHECK(fcma, { |
| 131 | + asm volatile ( |
| 132 | + "fmov d0, #0" "\n" |
| 133 | + "fcadd v0.2s, v0.2s, v0.2s, #90" "\n" |
| 134 | + : : : "v0" |
| 135 | + ); |
| 136 | +}) |
| 137 | +CHECK(jscvt, { |
| 138 | + asm volatile ( |
| 139 | + "fmov d0, #0" "\n" |
| 140 | + "fjcvtzs w1, d0" "\n" |
| 141 | + : : : "w1", "d0" |
| 142 | + ); |
| 143 | +}) |
| 144 | +CHECK(dpb, { |
| 145 | + int x; |
| 146 | + asm volatile ( |
| 147 | + "dc cvap, %0" |
| 148 | + : : "r" (&x) |
| 149 | + ); |
| 150 | +}) |
| 151 | +CHECK(dpb2, { |
| 152 | + int x; |
| 153 | + asm volatile ( |
| 154 | + "dc cvadp, %0" |
| 155 | + : : "r" (&x) |
| 156 | + ); |
| 157 | +}) |
| 158 | +CHECK(bf16, { |
| 159 | + asm volatile ( |
| 160 | + "bfdot v0.4S,v1.8H,v2.8H" |
| 161 | + : : : "v0" |
| 162 | + ); |
| 163 | +}) |
| 164 | +CHECK(i8mm, { |
| 165 | + asm volatile ( |
| 166 | + "sudot v0.4S,v1.16B,v2.4B[0]" |
| 167 | + : : : "v0" |
| 168 | + ); |
| 169 | +}) |
| 170 | +CHECK(dit, { |
| 171 | + asm volatile ( |
| 172 | + "msr DIT, x0" |
| 173 | + : : : "x0" |
| 174 | + ); |
| 175 | +}) |
| 176 | +CHECK(fp16, { |
| 177 | + asm volatile ( |
| 178 | + "fmov h0, #0" "\n" |
| 179 | + : : : "v0" |
| 180 | + ); |
| 181 | +}) |
| 182 | +CHECK(ssbs2, { |
| 183 | + // FIXME: don't know how to test for this |
| 184 | +}) |
| 185 | +CHECK(bti, { |
| 186 | + // FIXME: don't know how to test for this |
| 187 | +}) |
| 188 | +CHECK(simd, { |
| 189 | + asm volatile ( |
| 190 | + "mov v0.B[0], w0" |
| 191 | + : : : |
| 192 | + ); |
| 193 | +}) |
| 194 | +CHECK(fp, { |
| 195 | + asm volatile ( |
| 196 | + "fmov s0, #0" "\n" |
| 197 | + : : : "v0" |
| 198 | + ); |
| 199 | +}) |
| 200 | +CHECK(crc, { |
| 201 | + asm volatile ( "crc32b wzr, wzr, wzr"); |
| 202 | +}) |
| 203 | +CHECK(sme, { |
| 204 | + asm volatile ( |
| 205 | + "rdsvl x0, #1" |
| 206 | + : : : "x0" |
| 207 | + ); |
| 208 | +}) |
| 209 | +CHECK(sme2, { |
| 210 | + asm volatile ( |
| 211 | + "smstart za" "\n" |
| 212 | + "zero { zt0 }" "\n" |
| 213 | + "smstop za" "\n" |
| 214 | + ); |
| 215 | +}) |
| 216 | + |
| 217 | +static bool safe_try_feature(bool (*try_feature)(void)) { |
| 218 | + int child = fork(); |
| 219 | + if (child) { |
| 220 | + int exit_status = -1; |
| 221 | + if (child != waitpid(child, &exit_status, 0)) |
| 222 | + return false; |
| 223 | + return exit_status == 0; |
| 224 | + } else { |
| 225 | + exit(try_feature() ? 0 : 1); |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +int main(int, const char **) { |
| 230 | + check_flagm(); |
| 231 | + check_flagm2(); |
| 232 | + check_dotprod(); |
| 233 | + check_sha3(); |
| 234 | + check_rdm(); |
| 235 | + check_lse(); |
| 236 | + check_sha2(); |
| 237 | + check_sha1(); |
| 238 | + check_aes(); |
| 239 | + check_pmull(); |
| 240 | + check_rcpc(); |
| 241 | + check_rcpc2(); |
| 242 | + check_fcma(); |
| 243 | + check_jscvt(); |
| 244 | + check_dpb(); |
| 245 | + check_dpb2(); |
| 246 | + check_bf16(); |
| 247 | + check_i8mm(); |
| 248 | + check_dit(); |
| 249 | + check_fp16(); |
| 250 | + check_ssbs2(); |
| 251 | + check_bti(); |
| 252 | + check_simd(); |
| 253 | + check_fp(); |
| 254 | + check_crc(); |
| 255 | + check_sme(); |
| 256 | + check_sme2(); |
| 257 | + |
| 258 | + return any_fails ? -1 : 0; |
| 259 | +} |
0 commit comments