| // 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 <springbok.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| |
| #include "pw_unit_test/framework.h" |
| #include "softrvv.h" |
| |
| namespace softrvv_vmadd_vnmsub_test { |
| namespace { |
| |
| class SoftRvvVmaddTest : public ::testing::Test { |
| protected: |
| void SetUp() override { } |
| }; |
| |
| class SoftRvvVnmsubTest : public ::testing::Test { |
| protected: |
| void SetUp() override { } |
| }; |
| |
| |
| // Integer multiply-add, overwrite multiplicand |
| // vmadd.vv vd, vs1, vs2, vm # vd[i] = (vs1[i] * vd[i]) + vs2[i] |
| uint32_t vmaddvv_vs1[] = {1, 1, 128, 64, 0}; |
| uint32_t vmaddvv_vs2[] = {0, 1, 1, 2, 32}; |
| const uint32_t vmaddvv_kAVL = sizeof(vmaddvv_vs2) / sizeof(vmaddvv_vs2[0]); |
| uint32_t vmaddvv_vd[] = {0, 1, 2, 4, 8}; |
| int32_t vmaddvv_ref[] = {0, 2, 257, 258, 32}; |
| TEST_F(SoftRvvVmaddTest, VV) { |
| softrvv::vmadd_vv<uint32_t>(vmaddvv_vd, vmaddvv_vs1, vmaddvv_vs2, vmaddvv_kAVL); |
| ASSERT_EQ(memcmp(vmaddvv_vd, vmaddvv_ref, sizeof(vmaddvv_vd)), 0); |
| } |
| |
| // Integer multiply-add, overwrite multiplicand |
| // vmadd.vx vd, rs1, vs2, vm # vd[i] = (x[rs1] * vd[i]) + vs2[i] |
| uint32_t vmaddvx_rs1[] = {170}; |
| uint32_t vmaddvx_vs2[] = {0, 1, 1, 2, 32}; |
| const uint32_t vmaddvx_kAVL = sizeof(vmaddvx_vs2) / sizeof(vmaddvx_vs2[0]); |
| uint32_t vmaddvx_vd[] = {0, 1, 2, 4, 8}; |
| int32_t vmaddvx_ref[] = {0, 171, 341, 682, 1392}; |
| TEST_F(SoftRvvVmaddTest, VX) { |
| softrvv::vmadd_vx<uint32_t>(vmaddvx_vd, vmaddvx_rs1, vmaddvx_vs2, vmaddvx_kAVL); |
| ASSERT_EQ(memcmp(vmaddvx_vd, vmaddvx_ref, sizeof(vmaddvx_vd)), 0); |
| } |
| |
| // Integer multiply-sub, overwrite multiplicand |
| // vnmsub.vv vd, vs1, vs2, vm # vd[i] = -(vs1[i] * vd[i]) + vs2[i] |
| uint32_t vnmsubvv_vs1[] = {220, 24, 1234, 150, 1386}; |
| uint32_t vnmsubvv_vs2[] = { 1, 2, 2, 10, 1000}; |
| const uint32_t vnmsubvv_kAVL = sizeof(vnmsubvv_vs2) / sizeof(vnmsubvv_vs2[0]); |
| uint32_t vnmsubvv_vd[] = { 0, 10, 10, 1000, 1}; |
| int32_t vnmsubvv_ref[] = {1, -238, -12338, -149990, -386}; |
| TEST_F(SoftRvvVnmsubTest, VV) { |
| softrvv::vnmsub_vv<uint32_t>(vnmsubvv_vd, vnmsubvv_vs1, vnmsubvv_vs2, vnmsubvv_kAVL); |
| ASSERT_EQ(memcmp(vnmsubvv_vd, vnmsubvv_ref, sizeof(vnmsubvv_ref)), 0); |
| } |
| |
| // Integer multiply-sub, overwrite minuend |
| // vnmsub.vx vd, rs1, vs2, vm # vd[i] = -(x[rs1] * vd[i]) + vs2[i] |
| uint32_t vnmsubvx_rs1[] = {170}; |
| uint32_t vnmsubvx_vs2[] = { 1, 2, 2, 10, 1000}; |
| const uint32_t vnmsubvx_kAVL = sizeof(vnmsubvx_vs2) / sizeof(vnmsubvx_vs2[0]); |
| uint32_t vnmsubvx_vd[] = {170, 350, 10, 0, 0}; |
| int32_t vnmsubvx_ref[] = {-28899, -59498, -1698, 10, 1000}; |
| TEST_F(SoftRvvVnmsubTest, VX) { |
| softrvv::vnmsub_vx<uint32_t>(vnmsubvx_vd, vnmsubvx_rs1, vnmsubvx_vs2, vnmsubvx_kAVL); |
| ASSERT_EQ(memcmp(vnmsubvx_vd, vnmsubvx_ref, sizeof(vnmsubvx_ref)), 0); |
| } |
| |
| } // namespace |
| } // namespace softrvv_vmacc_vnmsub_test |