Forum Discussion
Altera_Forum
Honored Contributor
17 years agoI'm using a package of extended arithmetic functions for such purposes. Basically I leave it to the compiler to find an appropriate way to realize the intended behaviour. You may want to compare the achieved effectivity with handcoded saturation arithmetic, I'm satisfied with it up to now.
FUNCTION SUM (X1,X2: SIGNED) RETURN SIGNED IS
VARIABLE S:SIGNED(X1'length-1 downto 0);
BEGIN
S:=X1+X2;
IF X1>=0 AND X2>=0 AND S<0 THEN
S:= (others => '1');
S(S'left):='0';
ELSIF X1<0 AND X2<0 AND S>=0 THEN
S(S'left-1 downto 0):= (others => '0');
S(S'left):='1';
END IF;
RETURN S;
END;