Livt packages

Livt.Math

Version 1.7.2

Reusable integer and fixed-point arithmetic, complex numbers, trigonometry, and statistics for FPGA applications written in Livt.

Published files (90)

src/arithmetic/MacUnit.lvt

namespace Livt.Math.Arithmetic

using Livt.Convert

/**
 * Accumulates integer products into a signed 16-bit saturating result.
 */
component MacUnit
{
	// Minimum signed 16-bit accumulator value.
	public const ACC_MIN: int = -32768

	// Maximum signed 16-bit accumulator value.
	public const ACC_MAX: int = 32767

	// Running sum retained until Reset starts another reduction.
	accumulator: int

	// Owned arithmetic worker used by serialized operations.
	conv: Convert

	/**
	 * Initializes conversion support for saturating accumulator updates.
	 */
	new()
	{
		conv = new Convert()
	}

	/**
	 * Adds a * b to the accumulator and clamps the result to int16 range.
	 */
	public fn Accumulate(a: int, b: int)
	{
		var product: int = a * b
		var sum: int = accumulator + product
		accumulator = conv.ToInt16Clamped(sum)
	}

	/**
	 * Returns the current saturated accumulator value.
	 */
	public fn GetResult() int
	{
		return accumulator
	}

	/**
	 * Resets the accumulator to zero.
	 */
	public fn Reset()
	{
		accumulator = 0
	}
}