/*
* Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see .
*/
#include
#include
int err;
static void __check32(int line, int val, int expect)
{
if (val != expect) {
printf("ERROR at line %d: 0x%08x != 0x%08x\n", line, val, expect);
err++;
}
}
#define check32(RES, EXP) __check32(__LINE__, RES, EXP)
static void __check64(int line, long long val, long long expect)
{
if (val != expect) {
printf("ERROR at line %d: 0x%016llx != 0x%016llx\n", line, val, expect);
err++;
}
}
#define check64(RES, EXP) __check64(__LINE__, RES, EXP)
static uint32_t satub(uint32_t src, int *ovf_result)
{
uint32_t result;
uint32_t usr;
/*
* This instruction can set bit 0 (OVF/overflow) in usr
* Clear the bit first, then return that bit to the caller
*/
asm volatile("r2 = usr\n\t"
"r2 = clrbit(r2, #0)\n\t" /* clear overflow bit */
"usr = r2\n\t"
"%0 = satub(%2)\n\t"
"%1 = usr\n\t"
: "=r"(result), "=r"(usr)
: "r"(src)
: "r2", "usr");
*ovf_result = (usr & 1);
return result;
}
static void test_satub(void)
{
uint32_t result;
int ovf_result;
result = satub(0xfff, &ovf_result);
check32(result, 0xff);
check32(ovf_result, 1);
result = satub(-1, &ovf_result);
check32(result, 0);
check32(ovf_result, 1);
}
static uint64_t vaddubs(uint64_t src1, uint64_t src2, int *ovf_result)
{
uint64_t result;
uint32_t usr;
/*
* This instruction can set bit 0 (OVF/overflow) in usr
* Clear the bit first, then return that bit to the caller
*/
asm volatile("r2 = usr\n\t"
"r2 = clrbit(r2, #0)\n\t" /* clear overflow bit */
"usr = r2\n\t"
"%0 = vaddub(%2, %3):sat\n\t"
"%1 = usr\n\t"
: "=r"(result), "=r"(usr)
: "r"(src1), "r"(src2)
: "r2", "usr");
*ovf_result = (usr & 1);
return result;
}
static void test_vaddubs(void)
{
uint64_t result;
int ovf_result;
result = vaddubs(0xffLL, 0xffLL, &ovf_result);
check64(result, 0xffLL);
check32(ovf_result, 1);
}
int main()
{
test_satub();
test_vaddubs();
puts(err ? "FAIL" : "PASS");
return err;
}