diff --git a/libraries/base/common/src/vhdl/common_pkg.vhd b/libraries/base/common/src/vhdl/common_pkg.vhd index ef84bffe9d09ed520781e7108c6212dddc026d18..dd8e2b4c9e2b3864e09fddd1ca1780bee457813b 100644 --- a/libraries/base/common/src/vhdl/common_pkg.vhd +++ b/libraries/base/common/src/vhdl/common_pkg.vhd @@ -205,6 +205,7 @@ PACKAGE common_pkg IS FUNCTION ceil_div( n : UNSIGNED; d: NATURAL) RETURN UNSIGNED; FUNCTION ceil_value( n : UNSIGNED; d: NATURAL) RETURN UNSIGNED; FUNCTION floor_value(n : UNSIGNED; d: NATURAL) RETURN UNSIGNED; + FUNCTION gcd(a, b : NATURAL) RETURN NATURAL; -- greatest common divider FUNCTION slv(n: IN STD_LOGIC) RETURN STD_LOGIC_VECTOR; -- standard logic to 1 element standard logic vector FUNCTION sl( n: IN STD_LOGIC_VECTOR) RETURN STD_LOGIC; -- 1 element standard logic vector to standard logic @@ -719,6 +720,15 @@ PACKAGE BODY common_pkg IS RETURN p(w-1 DOWNTO 0); -- return same width as n END; + FUNCTION gcd(a, b : NATURAL) RETURN NATURAL IS -- greatest common divider + BEGIN + IF b = 0 THEN + RETURN a; + ELSE + RETURN gcd(b, a MOD b); + END IF; + END; + FUNCTION slv(n: IN STD_LOGIC) RETURN STD_LOGIC_VECTOR IS VARIABLE r : STD_LOGIC_VECTOR(0 DOWNTO 0); BEGIN diff --git a/libraries/base/common/tb/vhdl/tb_common_gcd.vhd b/libraries/base/common/tb/vhdl/tb_common_gcd.vhd new file mode 100644 index 0000000000000000000000000000000000000000..2083b0ee588d86468c8a848d55ea37d97e7bed7f --- /dev/null +++ b/libraries/base/common/tb/vhdl/tb_common_gcd.vhd @@ -0,0 +1,50 @@ +------------------------------------------------------------------------------- +-- +-- Copyright (C) 2009 +-- ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/> +-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands +-- +-- 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 3 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 <http://www.gnu.org/licenses/>. +-- +------------------------------------------------------------------------------- + +-- Author: E. Kooistra 2022 +-- Purpose: Test bench for gcd() in common_pkg.vhd +-- Usage: +-- > run -a + +LIBRARY IEEE; +USE IEEE.STD_LOGIC_1164.ALL; +USE IEEE.NUMERIC_STD.ALL; +USE work.common_pkg.ALL; + +ENTITY tb_common_gcd IS +END tb_common_gcd; + +ARCHITECTURE tb OF tb_common_gcd IS + +BEGIN + + ASSERT gcd( 0, 10) = 10 REPORT "Wrong gcd( 0, 10)" SEVERITY ERROR; + ASSERT gcd( 1, 1) = 1 REPORT "Wrong gcd( 1, 1)" SEVERITY ERROR; + ASSERT gcd(10, 1) = 1 REPORT "Wrong gcd(10, 1)" SEVERITY ERROR; + ASSERT gcd(10, 3) = 1 REPORT "Wrong gcd(10, 3)" SEVERITY ERROR; + ASSERT gcd(10, 4) = 2 REPORT "Wrong gcd(10, 4)" SEVERITY ERROR; + ASSERT gcd(10, 5) = 5 REPORT "Wrong gcd(10, 5)" SEVERITY ERROR; + ASSERT gcd(16, 4) = 4 REPORT "Wrong gcd(16, 4)" SEVERITY ERROR; + ASSERT gcd(15, 5) = 5 REPORT "Wrong gcd(15, 5)" SEVERITY ERROR; + ASSERT gcd(17, 17) = 17 REPORT "Wrong gcd(17, 17)" SEVERITY ERROR; + ASSERT gcd(17, 4) = 1 REPORT "Wrong gcd(17, 4)" SEVERITY ERROR; + +END tb;