From e6b1726587d3a2be8f18c2f6473ff8651cc15ca3 Mon Sep 17 00:00:00 2001
From: Eric Kooistra <kooistra@astron.nl>
Date: Thu, 4 Nov 2021 13:05:02 +0100
Subject: [PATCH] Use s_clip and u_clip to simplify the code of s_round() and
 u_round().

---
 libraries/base/common/src/vhdl/common_pkg.vhd | 26 ++++++++++---------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/libraries/base/common/src/vhdl/common_pkg.vhd b/libraries/base/common/src/vhdl/common_pkg.vhd
index c6ca6f3c92..63a769e7f4 100644
--- a/libraries/base/common/src/vhdl/common_pkg.vhd
+++ b/libraries/base/common/src/vhdl/common_pkg.vhd
@@ -2392,8 +2392,9 @@ PACKAGE BODY common_pkg IS
     CONSTANT c_in_w     : NATURAL := vec'LENGTH;
     CONSTANT c_out_w    : NATURAL := vec'LENGTH - n;
     CONSTANT c_half     : NATURAL := 2**(n-1);
-    CONSTANT c_max      : SIGNED(c_in_w-1 DOWNTO 0) := SIGNED('0' & c_slv1(c_in_w-2 DOWNTO 0)) - c_half;  -- = 2**(c_in_w-1)-1 - c_half
-    CONSTANT c_clip     : SIGNED(c_out_w-1 DOWNTO 0) := SIGNED('0' & c_slv1(c_out_w-2 DOWNTO 0));         -- = 2**(c_out_w-1)-1
+    CONSTANT c_max      : INTEGER := 2**(c_in_w-1)-1 - c_half;
+    CONSTANT c_clip     : INTEGER := 2**(c_out_w-1)-1;
+    CONSTANT s_clip     : SIGNED(c_out_w-1 DOWNTO 0) := TO_SIGNED(c_clip, c_out_w);
     VARIABLE v_in       : SIGNED(c_in_w-1 DOWNTO 0);
     VARIABLE v_fraction : UNSIGNED(n-1 DOWNTO 0);
     VARIABLE v_out      : SIGNED(c_out_w-1 DOWNTO 0);
@@ -2401,7 +2402,7 @@ PACKAGE BODY common_pkg IS
     v_in := SIGNED(vec);
     IF n > 0 THEN
       IF clip = TRUE AND v_in > c_max THEN
-        v_out := c_clip;                                              -- Round clip to maximum positive to avoid wrap to negative
+        v_out := s_clip;  -- Round clip to maximum positive to avoid wrap to negative
       ELSE
         IF even = FALSE THEN
           -- Round half away
@@ -2414,13 +2415,13 @@ PACKAGE BODY common_pkg IS
           -- Round half to even
           v_out := RESIZE_NUM(SHIFT_RIGHT(v_in + c_half, n), c_out_w);  -- Round to nearest using floor(vec + 0.5)
           v_fraction := UNSIGNED(vec(n-1 DOWNTO 0));
-          IF v_fraction = c_half AND v_out(0) = '1' THEN              -- Round half to even, so when odd subtract 1
-            v_out := v_out - 1;                                       -- to make v_out even
+          IF v_fraction = c_half AND v_out(0) = '1' THEN  -- Round half to even, so when odd subtract 1
+            v_out := v_out - 1;                           -- to make v_out even
           END IF;
         END IF;
       END IF;
     ELSE
-      v_out := RESIZE_NUM(v_in, c_out_w);                             -- NOP
+      v_out := RESIZE_NUM(v_in, c_out_w);  -- NOP
     END IF;
     RETURN STD_LOGIC_VECTOR(v_out);
   END;
@@ -2451,8 +2452,9 @@ PACKAGE BODY common_pkg IS
     CONSTANT c_in_w     : NATURAL := vec'LENGTH;
     CONSTANT c_out_w    : NATURAL := vec'LENGTH - n;
     CONSTANT c_half     : NATURAL := 2**(n-1);
-    CONSTANT c_max      : UNSIGNED(c_in_w-1 DOWNTO 0) := UNSIGNED(c_slv1(c_in_w-1 DOWNTO 0)) - c_half;  -- = 2**c_in_w-1 - c_half
-    CONSTANT c_clip     : UNSIGNED(c_out_w-1 DOWNTO 0) := UNSIGNED(c_slv1(c_out_w-1 DOWNTO 0));         -- = 2**c_out_w-1
+    CONSTANT c_max      : NATURAL := 2**c_in_w-1 - c_half;
+    CONSTANT c_clip     : NATURAL := 2**c_out_w-1;
+    CONSTANT u_clip     : UNSIGNED(c_out_w-1 DOWNTO 0) := TO_UNSIGNED(c_clip, c_out_w);
     VARIABLE v_in       : UNSIGNED(c_in_w-1 DOWNTO 0);
     VARIABLE v_fraction : UNSIGNED(n-1 DOWNTO 0);
     VARIABLE v_out      : UNSIGNED(c_out_w-1 DOWNTO 0);
@@ -2460,7 +2462,7 @@ PACKAGE BODY common_pkg IS
     v_in := UNSIGNED(vec);
     IF n > 0 THEN
       IF clip = TRUE AND v_in > c_max THEN
-        v_out := c_clip;                                              -- Round clip to +max to avoid wrap to 0
+        v_out := u_clip;  -- Round clip to +max to avoid wrap to 0
       ELSE
         IF even = FALSE THEN
           -- Round half up
@@ -2469,13 +2471,13 @@ PACKAGE BODY common_pkg IS
           -- Round half to even
           v_out := RESIZE_NUM(SHIFT_RIGHT(v_in + c_half, n), c_out_w);  -- Round to nearest using floor(vec + 0.5)
           v_fraction := UNSIGNED(vec(n-1 DOWNTO 0));
-          IF v_fraction = c_half AND v_out(0) = '1' THEN              -- Round half to even, so when odd subtract 1
-            v_out := v_out - 1;                                       -- to make v_out even
+          IF v_fraction = c_half AND v_out(0) = '1' THEN  -- Round half to even, so when odd subtract 1
+            v_out := v_out - 1;                           -- to make v_out even
           END IF;
         END IF;
       END IF;
     ELSE
-      v_out := RESIZE_NUM(v_in, c_out_w);                             -- NOP
+      v_out := RESIZE_NUM(v_in, c_out_w);  -- NOP
     END IF;
     RETURN STD_LOGIC_VECTOR(v_out);
   END;
-- 
GitLab