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/Q15Test.lvt

namespace Livt.Math.Tests.FixedPoint

using Livt.Math.FixedPoint

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

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

	/**
	 * Checks that Clamp s boundaries.
	 */
	@Test
	fn ClampsBoundaries()
	{
		q15.Clamp(40000)
		assert q15.GetResult() == 32767
		q15.Clamp(-40000)
		assert q15.GetResult() == -32768
		q15.Clamp(1234)
		assert q15.GetResult() == 1234
	}

	/**
	 * Checks that Add s and subtracts saturating.
	 */
	@Test
	fn AddsAndSubtractsSaturating()
	{
		q15.AddSaturating(30000, 4000)
		assert q15.GetResult() == 32767
		q15.SubSaturating(-30000, 4000)
		assert q15.GetResult() == -32768
		q15.SubSaturating(10, 3)
		assert q15.GetResult() == 7
	}

	/**
	 * Checks that Mul tiplies with rescale.
	 */
	@Test
	fn MultipliesWithRescale()
	{
		q15.Mul(16384, 16384)
		assert q15.GetResult() == 8192
		q15.Mul(32767, 32767)
		assert q15.GetResult() == 32766
		q15.Mul(-32768, 32767)
		assert q15.GetResult() == -32767
	}

	/**
	 * Checks the direct context free helpers case.
	 */
	@Test
	fn DirectContextFreeHelpers()
	{
		var high: int = Q15.ClampValue(40000)
		var low: int = Q15.ClampValue(-40000)
		var added: int = Q15.AddSaturatingValue(30000, 4000)
		var subtracted: int = Q15.SubSaturatingValue(10, 3)
		var multiplied: int = Q15.MulValue(16384, 16384)

		assert high == 32767
		assert low == -32768
		assert added == 32767
		assert subtracted == 7
		assert multiplied == 8192
	}
}