From f9511e9755b251f1ed0ccd0fe5a2aaee126d9869 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sat, 24 Aug 2024 05:41:22 -0400 Subject: [PATCH] Internals: Make V3Number::selfTest() --- src/CMakeLists.txt | 2 - src/Makefile_obj.in | 3 - src/V3Number.cpp | 17 +++++ src/V3Number.h | 2 + src/V3Number_test.cpp | 147 ------------------------------------------ src/Verilator.cpp | 1 + 6 files changed, 20 insertions(+), 152 deletions(-) delete mode 100644 src/V3Number_test.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 067aab0e5..3107f1dfe 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -334,8 +334,6 @@ SET(COVERAGE_SOURCES # Note about tests: # VlcMain.cpp #includes the following files: # V3Error.cpp, V3String.cpp, V3Os.cpp and VlcTop.cpp -# V3Number_test.cpp #includes the following files: -# V3FileLine.cpp # # Generated sources and headers for the verilator binary diff --git a/src/Makefile_obj.in b/src/Makefile_obj.in index a524c16a4..d89a952cd 100644 --- a/src/Makefile_obj.in +++ b/src/Makefile_obj.in @@ -355,9 +355,6 @@ $(TGT): $(PREDEP_H) $(OBJS) @echo " Linking $@..." ${LINK} ${LDFLAGS} -o $@ $(OBJS) $(CCMALLOC) ${LIBS} -V3Number_test: V3Number_test.o - ${LINK} ${LDFLAGS} -o $@ $^ ${LIBS} - #### Modules %__gen.cpp: %.cpp $(ASTGEN) $(AST_DEFS) $(DFG_DEFS) diff --git a/src/V3Number.cpp b/src/V3Number.cpp index 996de3898..f57c583cc 100644 --- a/src/V3Number.cpp +++ b/src/V3Number.cpp @@ -2539,3 +2539,20 @@ V3Number& V3Number::opLteN(const V3Number& lhs, const V3Number& rhs) { NUM_ASSERT_STRING_ARGS2(lhs, rhs); return setSingleBits(lhs.toString() <= rhs.toString()); } + +//====================================================================== + +void V3Number::selfTest() { + UINFO(2, __FUNCTION__ << ": " << endl); + FileLine* const fl = new FileLine{FileLine::builtInFilename()}; + V3Number num{fl, 32, 0}; + num.selfTestThis(); +} + +void V3Number::selfTestThis() { + // The self test has a "this" so UASSERT_SELFTEST/errorEndFatal works correctly + UASSERT_SELFTEST(int, log2b(0), 0); + UASSERT_SELFTEST(int, log2b(1), 0); + UASSERT_SELFTEST(int, log2b(0x40000000UL), 30); + UASSERT_SELFTEST(int, log2bQuad(0x4000000000000000ULL), 62); +} diff --git a/src/V3Number.h b/src/V3Number.h index dd3653383..3b18041aa 100644 --- a/src/V3Number.h +++ b/src/V3Number.h @@ -533,6 +533,7 @@ public: ~V3Number() {} private: + void selfTestThis(); void create(AstNode* nodep, const char* sourcep) { init(nodep, 0); m_fileline = nullptr; @@ -663,6 +664,7 @@ public: // STATICS static int log2b(uint32_t num); static int log2bQuad(uint64_t num); + static void selfTest(); // MATH // "this" is the output, as we need the output width before some computations diff --git a/src/V3Number_test.cpp b/src/V3Number_test.cpp deleted file mode 100644 index 45bbc1d82..000000000 --- a/src/V3Number_test.cpp +++ /dev/null @@ -1,147 +0,0 @@ -// -*- mode: C++; c-file-style: "cc-mode" -*- -//************************************************************************* -// DESCRIPTION: Verilator: Netlist (top level) functions -// -// Code available from: https://verilator.org -// -//************************************************************************* -// -// Copyright 2003-2024 by Wilson Snyder. This program is free software; you -// can redistribute it and/or modify it under the terms of either the GNU -// Lesser General Public License Version 3 or the Perl Artistic License -// Version 2.0. -// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 -// -//************************************************************************* - -// CHEAT! -#define V3NUMBER_ASCII_BINARY -#define V3ERROR_NO_GLOBAL_ -#define VL_MT_DISABLED_CODE_UNIT 1 - -#include "config_build.h" -#include "verilatedos.h" - -#include "V3Error.cpp" -#include "V3FileLine.cpp" -#include "V3Number.cpp" -#include "V3Number.h" - -#include -#include - -void test(const string& lhss, const string& op, const string& rhss, const string& exps) { - char* l1 = strdup(lhss.c_str()); - char* r1 = strdup(rhss.c_str()); - char* e1 = strdup(exps.c_str()); - - const FileLine fl = new FileLine{FileLine::builtInFinename()}; - - V3Number lhnum{fl, l1}; - V3Number rhnum{fl, r1}; - V3Number expnum{fl, e1}; - V3Number gotnum{fl, expnum.width()}; - - if (op == "redOr") { - gotnum.opRedOr(lhnum); - } else if (op == "redAnd") { - gotnum.opRedAnd(lhnum); - } else if (op == "redXor") { - gotnum.opRedXor(lhnum); - } else if (op == "concat") { - gotnum.opConcat(lhnum, rhnum); - } else if (op == "repl") { - gotnum.opRepl(lhnum, rhnum); - } else if (op == "~") { - gotnum.opNot(lhnum); - } else if (op == "!") { - gotnum.opLogNot(lhnum); - } else if (op == "negate") { - gotnum.opNegate(lhnum); - } else if (op == "+") { - gotnum.opAdd(lhnum, rhnum); - } else if (op == "-") { - gotnum.opSub(lhnum, rhnum); - } else if (op == "*") { - gotnum.opMul(lhnum, rhnum); - } else if (op == "/") { - gotnum.opDiv(lhnum, rhnum); - } else if (op == "%") { - gotnum.opModDiv(lhnum, rhnum); - } else if (op == "&") { - gotnum.opAnd(lhnum, rhnum); - } else if (op == "|") { - gotnum.opOr(lhnum, rhnum); - } else if (op == "<") { - gotnum.opLt(lhnum, rhnum); - } else if (op == ">") { - gotnum.opGt(lhnum, rhnum); - } else if (op == ">>") { - gotnum.opShiftR(lhnum, rhnum); - } else if (op == "<<") { - gotnum.opShiftL(lhnum, rhnum); - } else if (op == "==") { - gotnum.opEq(lhnum, rhnum); - } else if (op == "===") { - gotnum.opCaseEq(lhnum, rhnum); - } else if (op == "==?") { - gotnum.opWildEq(lhnum, rhnum); - } else if (op == "!=") { - gotnum.opNeq(lhnum, rhnum); - } else if (op == "!==") { - gotnum.opCaseNeq(lhnum, rhnum); - } else if (op == "!=?") { - gotnum.opWildNeq(lhnum, rhnum); - } else if (op == "<=") { - gotnum.opLte(lhnum, rhnum); - } else if (op == ">=") { - gotnum.opGte(lhnum, rhnum); - } else if (op == "&&") { - gotnum.opLogAnd(lhnum, rhnum); - } else if (op == "||") { - gotnum.opLogOr(lhnum, rhnum); - } else { - v3fatalSrc("Bad opcode: " << op); - } - - UINFO(0, "------- Test:\n" - << " " << lhnum << " " << op << endl - << " " << rhnum << endl - << " = " << expnum << endl - << " =? " << gotnum << endl); - - V3Number ok{fl, 1}; - ok.opCaseEq(expnum, gotnum); - if (ok.toUInt() != 1) v3fatalSrc("%Error:Test FAILED"); - - free(l1); - free(r1); - free(e1); -} - -int main() { - UINFO(0, "Test starting\n"); - - test("32'b10", "|", "32'b10", "32'b10"); - test("2'bx0", "|", "2'b10", "2'b10"); - test("32'b0x", "|", "32'b10", "32'b1x"); - test("32'b10", "&", "32'b11", "32'b10"); - test("32'b10", "+", "32'b10", "32'b100"); - test("3'b000", "negate", "", "3'b000"); - test("3'b001", "negate", "", "3'b111"); - test("32'b11", "-", "32'b001", "32'b10"); - test("3'b000", "-", "3'b111", "3'b001"); - test("3'b000", "-", "3'b000", "3'b000"); - test("57'h000000010F0CCE7", "*", "57'h10", "57'h10F0CCE70"); - test("57'h000000010F0CCE7", "*", "57'h0DE34E7FFFFFFFF", "57'h02A9D57EF0F3319"); - test("67'h7FFFFFFFFFFFFFFFF", "*", "67'h4000000003C8A8D6A", "67'h3FFFFFFFFC3757296"); - test("99'h7FFFFFFFFFFFFFFFFFFFFFFFF", "*", "99'h0000000000000000091338A80", - "99'h7FFFFFFFFFFFFFFFF6ECC7580"); - - std::cout << "Test completed\n"; -} - -//################################################################### -// Local Variables: -// compile-command: "make V3Number_test && ./V3Number_test " -// End: diff --git a/src/Verilator.cpp b/src/Verilator.cpp index 81c1b8e40..d6b58ea9c 100644 --- a/src/Verilator.cpp +++ b/src/Verilator.cpp @@ -676,6 +676,7 @@ static void verilate(const string& argString) { VBasicDTypeKwd::selfTest(); if (v3Global.opt.debugSelfTest()) { V3Os::selfTest(); + V3Number::selfTest(); VHashSha256::selfTest(); VSpellCheck::selfTest(); V3Graph::selfTest();