#include #include #include "vector.h" /* Transfer a checked element in test memory. */ static bool mem(void *ctx, u64 address, u32 size, bool store, u64 *value) { u8 *bytes = ctx; if (address > 4096 || size > 4096 - address) return false; if (!store) *value = 0; for (u32 j = 0; j < size; j++) { if (store) bytes[address + j] = *value >> (j * 8); else *value |= (u64)bytes[address + j] << (j * 8); } return true; } /* Set one little-endian element in a register group. */ static void put(struct vector_state *v, u32 r, u32 i, u32 bits, u64 n) { u8 *p = &v->reg[r][0] + i * (bits / 8); for (u32 j = 0; j < bits / 8; j++) p[j] = n >> (8 * j); } /* Read one little-endian element in a register group. */ static u64 get(struct vector_state *v, u32 r, u32 i, u32 bits) { u64 n = 0; u8 *p = &v->reg[r][0] + i * (bits / 8); for (u32 j = 0; j < bits / 8; j++) n |= (u64)p[j] << (8 * j); return n; } /* Construct OP-V operands independently of the instruction catalog. */ static u32 alu(u32 fn, u32 form, u32 d, u32 a, u32 b, bool mask) { return fn << 26 | (!mask) << 25 | a << 20 | b << 15 | form << 12 | d << 7 | 0x57; } /* Check rotations, masking and tail preservation for each element width. */ static void rotations(void) { for (u32 bits = 8, sew = 0; bits <= 64; bits *= 2, sew++) { struct vector_state v = { 0 }; u64 x[32] = { 0 }; u8 bytes[4096] = { 0 }; v.vtype = sew << 3; v.vl = 3; u64 mask = bits == 64 ? ~0ULL : (1ULL << bits) - 1; u64 a = 0x81a53c779e0142ffULL & mask; for (u32 i = 0; i < 4; i++) { put(&v, 8, i, bits, 0x55); put(&v, 16, i, bits, a); } for (u32 n = 0; n < 64; n++) { u32 ins = alu(0x14 | (n >> 5), 3, 8, 16, n & 31, false); assert(vector_execute(&v, x, ins, mem, bytes) == VECTOR_OK); u32 k = n & (bits - 1); u64 expected = k ? ((a >> k) | (a << (bits - k))) & mask : a; assert(get(&v, 8, 0, bits) == expected); assert(get(&v, 8, 3, bits) == 0x55); } v.reg[0][0] = 5; put(&v, 8, 1, bits, 0x55); assert( vector_execute(&v, x, alu(0, 0, 8, 16, 16, true), mem, bytes) == VECTOR_OK ); assert(get(&v, 8, 0, bits) == ((a + a) & mask)); assert(get(&v, 8, 1, bits) == 0x55); assert(get(&v, 8, 2, bits) == ((a + a) & mask)); } } /* Check memory restart, fault-only-first loads and segmented addressing. */ static void memory_cases(void) { struct vector_state v = { 0 }; u64 x[32] = { 0 }; u8 bytes[4096] = { 0 }; v.vtype = 16; v.vl = 4; x[10] = 4092; bytes[4092] = 17; u32 load = 0x02056407; /* vle32.v v8, (a0) */ assert(vector_execute(&v, x, load, mem, bytes) == VECTOR_FAULT); assert(v.vstart == 1 && get(&v, 8, 0, 32) == 17); x[10] = 0; bytes[4] = 19; assert(vector_execute(&v, x, load, mem, bytes) == VECTOR_OK); assert(v.vstart == 0 && get(&v, 8, 0, 32) == 17 && get(&v, 8, 1, 32) == 19); x[10] = 4092; assert(vector_execute(&v, x, load | (16 << 20), mem, bytes) == VECTOR_OK); assert(v.vl == 1 && v.vstart == 0); x[10] = 0; v.vl = 2; bytes[0] = 1; bytes[4] = 2; bytes[8] = 3; bytes[12] = 4; assert(vector_execute(&v, x, load | (1u << 29), mem, bytes) == VECTOR_OK); assert(get(&v, 8, 0, 32) == 1 && get(&v, 9, 0, 32) == 2); assert(get(&v, 8, 1, 32) == 3 && get(&v, 9, 1, 32) == 4); } /* Check fixed-point saturation, extension and packed mask reductions. */ static void integer_cases(void) { struct vector_state v = { 0 }; u64 x[32] = { 0 }; u8 bytes[4096] = { 0 }; v.vl = 4; put(&v, 16, 0, 8, 255); put(&v, 24, 0, 8, 1); assert( vector_execute(&v, x, alu(32, 0, 8, 16, 24, false), mem, bytes) == VECTOR_OK ); assert(get(&v, 8, 0, 8) == 255 && v.vxsat == 1); put(&v, 16, 0, 8, 127); put(&v, 24, 0, 8, 1); assert( vector_execute(&v, x, alu(33, 0, 8, 16, 24, false), mem, bytes) == VECTOR_OK ); assert(get(&v, 8, 0, 8) == 127); v.vtype = 16; v.reg[31][0] = 0x80; assert( vector_execute(&v, x, alu(18, 2, 8, 31, 5, false), mem, bytes) == VECTOR_OK ); assert(get(&v, 8, 0, 32) == 0xffffff80); v.vtype = 3; v.vl = 256; memset(v.reg[31], 255, VECTOR_BYTES); assert( vector_execute(&v, x, alu(16, 2, 10, 31, 16, false), mem, bytes) == VECTOR_OK ); assert(x[10] == 256); assert( vector_execute(&v, x, alu(16, 2, 10, 31, 17, false), mem, bytes) == VECTOR_OK ); assert(x[10] == 0); /* Destination alignment is checked before a register group is written. */ struct vector_state before = v; assert( vector_execute(&v, x, alu(0, 0, 31, 16, 24, false), mem, bytes) == VECTOR_ILLEGAL ); assert(memcmp(&before, &v, sizeof(v)) == 0); } /* Check each unary Zvbb operation against fixed element results. */ static void bit_operations(void) { struct vector_state v = { 0 }; u64 x[32] = { 0 }; u8 bytes[4096] = { 0 }; const u32 selectors[] = { 8, 9, 10, 12, 13, 14 }; const u32 expected[] = { 0x808040c0, 0x03020101, 0xc0408080, 7, 0, 5 }; v.vtype = 16; v.vl = 1; put(&v, 16, 0, 32, 0x01010203); for (u32 i = 0; i < 6; i++) { assert( vector_execute( &v, x, alu(18, 2, 8, 16, selectors[i], false), mem, bytes ) == VECTOR_OK ); assert(get(&v, 8, 0, 32) == expected[i]); } v.vtype = 16; put(&v, 16, 0, 32, 1); assert( vector_execute(&v, x, alu(53, 3, 8, 16, 31, false), mem, bytes) == VECTOR_OK ); assert(get(&v, 8, 0, 64) == 1ULL << 31); } /* Mask results and scalar moves occupy one register for every LMUL. */ static void mask_registers(void) { struct vector_state v = { 0 }; u64 x[32] = { 0 }; u8 bytes[4096] = { 0 }; v.vtype = 3; v.vl = 8; v.reg[31][0] = 4; assert( vector_execute(&v, x, alu(20, 2, 3, 31, 1, false), mem, bytes) == VECTOR_OK ); assert(v.reg[3][0] == 3); assert( vector_execute(&v, x, alu(20, 2, 31, 31, 1, false), mem, bytes) == VECTOR_ILLEGAL ); assert( vector_execute(&v, x, alu(20, 2, 8, 8, 16, false), mem, bytes) == VECTOR_ILLEGAL ); x[10] = 23; assert( vector_execute(&v, x, alu(16, 6, 3, 0, 10, false), mem, bytes) == VECTOR_OK ); assert(v.reg[3][0] == 23); } /* Check SIMD register groups, aliases, masks, tails and restart positions. */ static void native_lanes(void) { const u32 lms[] = { 0, 1, 2, 3, 5, 6, 7 }; for (u32 w = 0; w < 4; w++) { u32 bits = 8u << w; u64 mask = bits == 64 ? ~0ULL : (1ULL << bits) - 1; for (u32 lm = 0; lm < sizeof(lms) / sizeof(lms[0]); lm++) { u32 bytes = lms[lm] < 4 ? VECTOR_BYTES << lms[lm] : VECTOR_BYTES >> (8 - lms[lm]); u32 max = bytes * 8 / bits; if (!max) continue; for (u32 k = 0; k < 4; k++) for (u32 mode = 0; mode < 8; mode++) { struct vector_state v = { 0 }; u64 x[32] = { 0 }; for (u32 j = 0; j < sizeof(v.reg); j++) ((u8 *)v.reg)[j] = j * 37 + 129; v.vtype = w * 8 + lms[lm]; v.vl = mode & 1 ? max : max - 1; v.vstart = mode & 2 ? max / 3 : 0; bool masked = mode & 4; u32 d = mode & 1 ? 24 : 8; u32 ops[] = { alu(0, 0, d, 24, 16, masked), alu(37, 2, d, 24, 16, masked), alu(20, 4, d, 24, 10, masked), alu(18, 2, d, 24, 14, masked), }; x[10] = bits - 1; struct vector_state want = v; for (u32 i = v.vstart; i < v.vl; i++) { if (masked && !((v.reg[0][i / 8] >> (i % 8)) & 1)) continue; u64 a = get(&v, 24, i, bits); u64 b = get(&v, 16, i, bits); u64 z = k == 0 ? a + b : k == 1 ? a * b : k == 2 ? (a >> (bits - 1)) | (a << 1) : (u64)__builtin_popcountll(a); put(&want, d, i, bits, z & mask); } want.vstart = 0; assert( vector_execute(&v, x, ops[k], mem, NULL) == VECTOR_OK ); assert(memcmp(&v, &want, sizeof(v)) == 0); } } } } /* Exercise integer vector execution without host image loading. */ int main(void) { native_lanes(); mask_registers(); bit_operations(); rotations(); memory_cases(); integer_cases(); struct vector_state shift = { 0 }; u64 sx[32] = { 0 }; u8 sm[4096] = { 0 }; shift.vtype = 24; shift.vl = 1; put(&shift, 16, 0, 64, 1); assert( vector_execute(&shift, sx, alu(37, 3, 8, 16, 31, false), mem, sm) == VECTOR_OK ); assert(get(&shift, 8, 0, 64) == 1ULL << 31); struct vector_state v = { 0 }; u64 x[32] = { 0 }; u8 bytes[4096] = { 0 }; assert(vector_execute(&v, x, 0xc1027557, mem, bytes) == VECTOR_OK); assert(v.vl == 4 && v.vtype == 16 && x[10] == 4); for (int i = 0; i < 4; i++) { v.reg[9][i * 4] = i + 1; v.reg[10][i * 4] = 10; } assert(vector_execute(&v, x, 0x02950457, mem, bytes) == VECTOR_OK); for (int i = 0; i < 4; i++) assert(v.reg[8][i * 4] == i + 11); assert(vector_execute(&v, x, 0x52943457, mem, bytes) == VECTOR_OK); assert(v.reg[8][0] == 0 && v.reg[8][3] == 1); return 0; }