Add acset actr unit tests

Change-Id: I8331b8f9063989b472646665599263aa748c852d
diff --git a/tests/kelvin_isa/BUILD b/tests/kelvin_isa/BUILD
index 146fff8..dda8199 100644
--- a/tests/kelvin_isa/BUILD
+++ b/tests/kelvin_isa/BUILD
@@ -25,6 +25,28 @@
 )
 
 kelvin_test(
+    name = "acset",
+    srcs = [
+        "acset.cc",
+    ],
+    hw_test_size = "small",
+    deps = [
+        ":kelvin_test",
+    ],
+)
+
+kelvin_test(
+    name = "actr",
+    srcs = [
+        "actr.cc",
+    ],
+    hw_test_size = "small",
+    deps = [
+        ":kelvin_test",
+    ],
+)
+
+kelvin_test(
     name = "getvl_test",
     srcs = [
         "getvl_test.cc",
diff --git a/tests/kelvin_isa/acset.cc b/tests/kelvin_isa/acset.cc
new file mode 100644
index 0000000..55b81e5
--- /dev/null
+++ b/tests/kelvin_isa/acset.cc
@@ -0,0 +1,172 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <cstdio>
+
+#include "tests/kelvin_isa/kelvin_test.h"
+
+template <int bits>
+void test_acset() {
+  vconv_u8_t cmd;
+  cmd.mode = 0;
+  cmd.start = 0;
+  cmd.stop = 7;
+  cmd.sbias1 = 0x56;
+  cmd.sdata1 = false;
+  cmd.sbias2 = 0xbc;
+  cmd.sdata2 = true;
+
+  uint32_t cmdw;
+  memcpy(&cmdw, &cmd, 4);
+
+  // clang-format off
+  const uint8_t ina[8][bits/8] = {
+    { 0xea, 0xe6, 0xaa, 0xb0, 0x5e, 0x46, 0x43, 0x2c,
+      0x58, 0x69, 0xd4, 0x25, 0xf0, 0xe9, 0x74, 0xd5,
+      0x34, 0x16, 0x4c, 0x86, 0xa8, 0x0c, 0xac, 0xa8,
+      0x9f, 0x99, 0x5d, 0xe0, 0x1a, 0x93, 0x65, 0x88, },
+    { 0x4a, 0x3a, 0x57, 0x8b, 0x50, 0x6c, 0x1b, 0x37,
+      0x76, 0x8c, 0x72, 0x55, 0xb3, 0xce, 0xf3, 0x50,
+      0x74, 0x51, 0xb2, 0xb9, 0xb9, 0x76, 0xc4, 0x94,
+      0x29, 0x52, 0x16, 0xa8, 0x68, 0xa7, 0x8e, 0xe2, },
+    { 0x61, 0xe8, 0xb8, 0xae, 0x43, 0x0a, 0xbe, 0xfa,
+      0x0b, 0x32, 0x7a, 0x92, 0x44, 0x3a, 0x60, 0xd3,
+      0x11, 0xc0, 0xb3, 0x8c, 0x7e, 0x5b, 0x9c, 0xe9,
+      0x03, 0x94, 0x7f, 0x10, 0x38, 0xd5, 0xc6, 0xeb, },
+    { 0x5c, 0x79, 0x44, 0xe9, 0xfb, 0x32, 0x00, 0xf0,
+      0x79, 0xed, 0x92, 0x77, 0x93, 0x45, 0xc3, 0x63,
+      0x33, 0x7e, 0x07, 0xc5, 0x07, 0x76, 0x9d, 0xf0,
+      0x4e, 0x6a, 0x67, 0xcc, 0xca, 0xab, 0xd4, 0x24, },
+    { 0x82, 0x86, 0xbb, 0xd9, 0x4b, 0xda, 0x2e, 0xbb,
+      0x8d, 0xbb, 0x82, 0x0e, 0x53, 0xaa, 0xb8, 0xfa,
+      0xa1, 0x75, 0x6e, 0x4a, 0x3c, 0xba, 0xa1, 0x88,
+      0x88, 0x6c, 0x74, 0x5b, 0x09, 0x83, 0x98, 0xdc, },
+    { 0x91, 0x9a, 0x9f, 0xcd, 0x87, 0xd7, 0x74, 0x90,
+      0xa5, 0x25, 0xcf, 0x56, 0x5d, 0x41, 0x56, 0xc2,
+      0x0d, 0xdd, 0xd8, 0x2c, 0x59, 0x1d, 0x1c, 0x66,
+      0x06, 0xe9, 0xd3, 0x51, 0x83, 0x16, 0x65, 0x56, },
+    { 0xf6, 0x95, 0x7c, 0x47, 0xf5, 0x56, 0x53, 0x58,
+      0x87, 0x9c, 0xde, 0xac, 0x76, 0x78, 0x71, 0x86,
+      0x5c, 0xdb, 0x5f, 0x0d, 0xc4, 0x5f, 0x48, 0x61,
+      0x48, 0x6f, 0x77, 0x26, 0x68, 0xf1, 0x39, 0x58, },
+    { 0x32, 0x36, 0x68, 0x29, 0x67, 0xb8, 0x7c, 0xdd,
+      0xb9, 0x17, 0xb0, 0xec, 0x2e, 0xcc, 0xa5, 0x54,
+      0x76, 0x56, 0xc8, 0x0b, 0x77, 0xa1, 0xef, 0xf5,
+      0xcf, 0xd6, 0x84, 0x7a, 0x84, 0x0f, 0x4e, 0x16, },
+  };
+
+  const uint8_t inb[8][bits/8] = {
+    { 0x33, 0xc9, 0xb8, 0xa1, 0xea, 0x2d, 0x2c, 0x18,
+      0x92, 0x98, 0x8e, 0x19, 0xf2, 0xd8, 0x55, 0x92,
+      0x26, 0x4a, 0x8d, 0x3e, 0xb0, 0x01, 0x81, 0x6c,
+      0x3e, 0xcc, 0x8f, 0x9b, 0xf9, 0xde, 0x94, 0x9e, },
+    { 0x7f, 0xa9, 0xa8, 0x3a, 0xa3, 0xef, 0xb7, 0xb5,
+      0x44, 0x93, 0xa1, 0xf7, 0x09, 0x7b, 0xb6, 0x6f,
+      0x98, 0x9e, 0xaa, 0x60, 0xb4, 0xe2, 0x9b, 0x93,
+      0x4b, 0x8f, 0xa7, 0xe4, 0x96, 0xe6, 0xcd, 0x93, },
+    { 0x63, 0xfd, 0x8e, 0xd2, 0xfd, 0xe0, 0x13, 0x5b,
+      0xd7, 0x5f, 0xa0, 0x56, 0x02, 0x29, 0x4e, 0xfa,
+      0x9b, 0x30, 0xa5, 0xdb, 0xb4, 0xe7, 0xb9, 0x52,
+      0x05, 0xda, 0x57, 0xa8, 0xbd, 0x65, 0xfe, 0xf0, },
+    { 0x1c, 0x22, 0x48, 0x3f, 0x5c, 0xae, 0x08, 0x8c,
+      0x40, 0xd4, 0x97, 0xeb, 0xb1, 0x92, 0x50, 0xd4,
+      0x66, 0xac, 0x58, 0x20, 0x3c, 0x92, 0xc0, 0x5c,
+      0x50, 0x89, 0x42, 0x93, 0x7b, 0x8b, 0x0a, 0x33, },
+    { 0x3e, 0x98, 0x0c, 0x1a, 0xcd, 0x6c, 0xd5, 0x26,
+      0xad, 0x94, 0xd0, 0x6a, 0xbd, 0x19, 0x02, 0x42,
+      0xc1, 0x69, 0x20, 0x94, 0xc2, 0x74, 0xb7, 0xbf,
+      0x9f, 0x45, 0xd5, 0x6f, 0x22, 0x92, 0xbe, 0x88, },
+    { 0x4c, 0xbc, 0xed, 0x2f, 0x9e, 0xe5, 0x27, 0xf9,
+      0x0e, 0xd3, 0xb5, 0x74, 0x83, 0x5f, 0x5a, 0xa2,
+      0xaf, 0x0f, 0x49, 0x0a, 0xe3, 0x1b, 0xc7, 0xd1,
+      0xd1, 0x51, 0xa9, 0x86, 0x75, 0xb3, 0xbc, 0xe9, },
+    { 0x9f, 0x2a, 0xe9, 0xfc, 0x4f, 0x10, 0x57, 0x7d,
+      0x14, 0xec, 0x5f, 0x39, 0xa3, 0x0e, 0x92, 0x62,
+      0x86, 0xad, 0xae, 0x38, 0x95, 0x7f, 0xde, 0x30,
+      0x9d, 0xbf, 0xe5, 0xb2, 0xb3, 0xb6, 0xf1, 0x42, },
+    { 0x0c, 0xd7, 0xd4, 0xa8, 0xae, 0x1a, 0xe6, 0x3c,
+      0xf0, 0xd7, 0x4f, 0x36, 0x37, 0xeb, 0x1d, 0xe9,
+      0xc7, 0xe4, 0x82, 0xe4, 0x44, 0x1c, 0x6a, 0xa1,
+      0x96, 0xfe, 0x2e, 0x9d, 0x36, 0xb4, 0x1c, 0x03, },
+  };
+  // clang-format on
+
+  uint32_t ref[bits / 32][bits / 32];
+  uint32_t dut[bits / 32][bits / 32];
+
+  vld_b_x(v0, ina[0]);
+  vld_b_x(v1, ina[1]);
+  vld_b_x(v2, ina[2]);
+  vld_b_x(v3, ina[3]);
+  vld_b_x(v4, ina[4]);
+  vld_b_x(v5, ina[5]);
+  vld_b_x(v6, ina[6]);
+  vld_b_x(v7, ina[7]);
+
+  vld_b_x(v16, inb[0]);
+  vld_b_x(v17, inb[1]);
+  vld_b_x(v18, inb[2]);
+  vld_b_x(v19, inb[3]);
+  vld_b_x(v20, inb[4]);
+  vld_b_x(v21, inb[5]);
+  vld_b_x(v22, inb[6]);
+  vld_b_x(v23, inb[7]);
+
+  // REF.
+  aconv_vxv(v48, v0, cmdw, v16);
+  aconv_vxv(v48, v16, cmdw, v0);
+  vcget(v48);
+  vst_w_x(v48, ref[0]);
+  vst_w_x(v49, ref[1]);
+  vst_w_x(v50, ref[2]);
+  vst_w_x(v51, ref[3]);
+  vst_w_x(v52, ref[4]);
+  vst_w_x(v53, ref[5]);
+  vst_w_x(v54, ref[6]);
+  vst_w_x(v55, ref[7]);
+
+  // DUT.
+  aconv_vxv(v48, v0, cmdw, v16);
+  vcget(v48);
+  acset_v(v48, v48);
+  aconv_vxv(v48, v16, cmdw, v0);
+  vcget(v48);
+  vst_w_x(v48, dut[0]);
+  vst_w_x(v49, dut[1]);
+  vst_w_x(v50, dut[2]);
+  vst_w_x(v51, dut[3]);
+  vst_w_x(v52, dut[4]);
+  vst_w_x(v53, dut[5]);
+  vst_w_x(v54, dut[6]);
+  vst_w_x(v55, dut[7]);
+
+  // Check the results.
+  for (int i = 0; i < 8; ++i) {
+    for (int j = 0; j < 8; ++j) {
+      const uint32_t r = ref[i][j];
+      const uint32_t d = dut[i][j];
+      if (r != d) {
+        printf("**error::test_acset[%d,%d] %08lx %08lx\n", i, j, r, d);
+        exit(-1);
+      }
+    }
+  }
+}
+
+int main() {
+  test_acset<VLEN>();
+  return 0;
+}
diff --git a/tests/kelvin_isa/actr.cc b/tests/kelvin_isa/actr.cc
new file mode 100644
index 0000000..6bed27e
--- /dev/null
+++ b/tests/kelvin_isa/actr.cc
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <cstdio>
+
+#include "tests/kelvin_isa/kelvin_test.h"
+
+template <int ireg>
+void test_256_actr() {
+  uint32_t inp[8][8] __attribute__((aligned(64)));
+  uint32_t ref[8][8] __attribute__((aligned(64)));
+  uint32_t dut[8][8] __attribute__((aligned(64)));
+
+  int value = 0;
+  for (int j = 0; j < 8; ++j) {
+    for (int i = 0; i < 8; ++i) {
+      inp[j][i] = (krand() << 8) | value++;
+    }
+  }
+
+  for (int j = 0; j < 8; ++j) {
+    for (int i = 0; i < 8; ++i) {
+      ref[i][j] = inp[j][i];
+    }
+  }
+
+  if (ireg == 0) {
+    vld_w_x(v0, inp[0]);
+    vld_w_x(v1, inp[1]);
+    vld_w_x(v2, inp[2]);
+    vld_w_x(v3, inp[3]);
+    vld_w_x(v4, inp[4]);
+    vld_w_x(v5, inp[5]);
+    vld_w_x(v6, inp[6]);
+    vld_w_x(v7, inp[7]);
+  }
+  if (ireg == 16) {
+    vld_w_x(v16, inp[0]);
+    vld_w_x(v17, inp[1]);
+    vld_w_x(v18, inp[2]);
+    vld_w_x(v19, inp[3]);
+    vld_w_x(v20, inp[4]);
+    vld_w_x(v21, inp[5]);
+    vld_w_x(v22, inp[6]);
+    vld_w_x(v23, inp[7]);
+  }
+  if (ireg == 32) {
+    vld_w_x(v32, inp[0]);
+    vld_w_x(v33, inp[1]);
+    vld_w_x(v34, inp[2]);
+    vld_w_x(v35, inp[3]);
+    vld_w_x(v36, inp[4]);
+    vld_w_x(v37, inp[5]);
+    vld_w_x(v38, inp[6]);
+    vld_w_x(v39, inp[7]);
+  }
+  if (ireg == 48) {
+    vld_w_x(v48, inp[0]);
+    vld_w_x(v49, inp[1]);
+    vld_w_x(v50, inp[2]);
+    vld_w_x(v51, inp[3]);
+    vld_w_x(v52, inp[4]);
+    vld_w_x(v53, inp[5]);
+    vld_w_x(v54, inp[6]);
+    vld_w_x(v55, inp[7]);
+  }
+
+  if (ireg == 0) {
+    actr_v(v48, v0);
+  }
+  if (ireg == 16) {
+    actr_v(v48, v16);
+  }
+  if (ireg == 32) {
+    actr_v(v48, v32);
+  }
+  if (ireg == 48) {
+    actr_v(v48, v48);
+  }
+  vcget(v48);
+
+  vst_w_x_m(v48, dut[0]);
+  vst_w_x_m(v52, dut[4]);
+
+  for (int j = 0; j < 8; ++j) {
+    for (int i = 0; i < 8; ++i) {
+      if (ref[j][i] != dut[j][i]) {
+        printf("**error actr_v[%d][%d,%d] %08lx %08lx\n", ireg, j, i, ref[j][i],
+               dut[j][i]);
+        exit(-1);
+      }
+    }
+  }
+}
+int main() {
+  test_256_actr<0>();
+  test_256_actr<16>();
+  test_256_actr<32>();
+  test_256_actr<48>();
+
+  return 0;
+}
diff --git a/tests/kelvin_isa/kelvin_test.h b/tests/kelvin_isa/kelvin_test.h
index 91e7385..7ca4932 100644
--- a/tests/kelvin_isa/kelvin_test.h
+++ b/tests/kelvin_isa/kelvin_test.h
@@ -29,6 +29,18 @@
 constexpr int VLENH = VLEN / 16;
 constexpr int VLENW = VLEN / 32;
 
+uint32_t krand(void) {
+  static uint32_t x = 123456789;
+  static uint32_t y = 362436069;
+  static uint32_t z = 521288629;
+  static uint32_t w = 88675123;
+  uint32_t t = x ^ (x << 11);
+  x = y;
+  y = z;
+  z = w;
+  return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
+}
+
 #define getmaxvl(T, d)                 \
   {                                    \
     if (sizeof(T) == 1) getmaxvl_b(d); \