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/complex/ComplexQ15Test.lvt

namespace Livt.Math.Tests.Complex

using Livt.Math.Complex
using Livt.Math.FixedPoint

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

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

	/**
	 * Checks that Set s and clamps values.
	 */
	@Test
	fn SetsAndClampsValues()
	{
		value.Set(40000, -40000)

		assert value.GetReal() == 32767
		assert value.GetImag() == -32768
	}

	/**
	 * Checks that Add s and subtracts.
	 */
	@Test
	fn AddsAndSubtracts()
	{
		value.Set(1000, -2000)
		value.Add(300, 500)

		assert value.GetResultReal() == 1300
		assert value.GetResultImag() == -1500

		value.Sub(300, 500)

		assert value.GetResultReal() == 700
		assert value.GetResultImag() == -2500
	}

	/**
	 * Checks that Mul tiplies by one.
	 */
	@Test
	fn MultipliesByOne()
	{
		value.Set(16384, 0)
		value.Mul(32767, 0)

		assert value.GetResultReal() == 16383
		assert value.GetResultImag() == 0
	}

	/**
	 * Checks that Mul tiplies by imaginary unit.
	 */
	@Test
	fn MultipliesByImaginaryUnit()
	{
		value.Set(16384, 0)
		value.Mul(0, 32767)

		assert value.GetResultReal() == 0
		assert value.GetResultImag() == 16383
	}

	/**
	 * Checks that the component rotates simple quarter turn.
	 */
	@Test
	fn RotatesSimpleQuarterTurn()
	{
		value.Set(16384, 16384)
		value.MulByTwiddle(0, 32767)

		assert value.GetResultReal() == -16383
		assert value.GetResultImag() == 16383
	}

	/**
	 * Checks the direct context free helpers case.
	 */
	@Test
	fn DirectContextFreeHelpers()
	{
		var high: int = ComplexQ15.ClampPart(40000)
		var low: int = ComplexQ15.ClampPart(-40000)
		var addReal: int = ComplexQ15.AddReal(32000, 2000)
		var addImag: int = ComplexQ15.AddImag(-32000, -2000)
		var subReal: int = ComplexQ15.SubReal(-32000, 2000)
		var subImag: int = ComplexQ15.SubImag(32000, -2000)
		var mulRealOne: int = ComplexQ15.MulReal(16384, 0, 32767, 0)
		var mulImagOne: int = ComplexQ15.MulImag(16384, 0, 0, 32767)
		var mulRealQuarter: int = ComplexQ15.MulReal(16384, 16384, 0, 32767)
		var mulImagQuarter: int = ComplexQ15.MulImag(16384, 16384, 0, 32767)

		assert high == 32767
		assert low == -32768
		assert addReal == 32767
		assert addImag == -32768
		assert subReal == -32768
		assert subImag == 32767
		assert mulRealOne == 16383
		assert mulImagOne == 16383
		assert mulRealQuarter == -16383
		assert mulImagQuarter == 16383
	}
}