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/fixedpoint/ReciprocalSqrtQ15.lvt

namespace Livt.Math.FixedPoint

using Livt.Math.Sqrt
using Livt.Math.Arithmetic

/**
 * Returns reciprocal square roots scaled by 32768 for positive integers.
 */
component ReciprocalSqrtQ15
{
	// Compile-time dimensions, bounds, and numeric constants.
	public const SCALE: int = 32768
	public const DIVISION_BITS: int = 16

	// Owned arithmetic worker used by serialized operations.
	sqrt: SqrtInt32
	divider: UnsignedDivision<DIVISION_BITS>

	/**
	 * Creates the owned integer square-root worker; reciprocal division shares the generic worker.
	 */
	new()
	{
		this.sqrt = new SqrtInt32()
		divider = new UnsignedDivision<DIVISION_BITS>()
	}

	/**
	 * Returns round(32768 / floor(sqrt(value))); non-positive inputs return zero.
	 */
	public fn Compute(value: int) int
	{
		if (value <= 0) {
			return 0
		}

		var root = this.sqrt.Compute(value)
		var dividend = SCALE + (root >> 1)
		return divider.Divide(dividend as uint, root as uint) as int
	}
}