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)

tests/fixedpoint/Q7Test.lvt

namespace Livt.Math.Tests.FixedPoint

using Livt.Math.FixedPoint

/**
 * Exercises Q7 with a shared fixture instance. Each scenario establishes the inputs and
 * state it needs.
 */
@Test
component Q7Test
{
	// Shared fixture instances, signals, and observation state.
	q7: Q7

	/**
	 * Creates the shared fixture instances (Q7). Tests initialize the state required by their
	 * scenario.
	 */
	new()
	{
		q7 = new Q7()
	}

	/**
	 * Checks that Clamp s boundaries.
	 */
	@Test
	fn ClampsBoundaries()
	{
		q7.Clamp(200)
		assert q7.GetResult() == 127
		q7.Clamp(-200)
		assert q7.GetResult() == -128
		q7.Clamp(12)
		assert q7.GetResult() == 12
	}

	/**
	 * Checks that Add s and subtracts saturating.
	 */
	@Test
	fn AddsAndSubtractsSaturating()
	{
		q7.AddSaturating(120, 20)
		assert q7.GetResult() == 127
		q7.SubSaturating(-120, 20)
		assert q7.GetResult() == -128
		q7.AddSaturating(20, 7)
		assert q7.GetResult() == 27
	}

	/**
	 * Checks that Mul tiplies with rescale.
	 */
	@Test
	fn MultipliesWithRescale()
	{
		q7.Mul(64, 64)
		assert q7.GetResult() == 32
		q7.Mul(127, 127)
		assert q7.GetResult() == 126
		q7.Mul(-128, 127)
		assert q7.GetResult() == -127
	}

	/**
	 * Checks the direct context free helpers case.
	 */
	@Test
	fn DirectContextFreeHelpers()
	{
		var high: int = Q7.ClampValue(200)
		var low: int = Q7.ClampValue(-200)
		var added: int = Q7.AddSaturatingValue(120, 20)
		var subtracted: int = Q7.SubSaturatingValue(10, 3)
		var multiplied: int = Q7.MulValue(64, 64)

		assert high == 127
		assert low == -128
		assert added == 127
		assert subtracted == 7
		assert multiplied == 32
	}
}