Livt packages

Livt.Net

Version 1.4.1

Ethernet, IPv4, TCP, UDP, ARP, TFTP, and Telnet networking components for Livt hardware.

Published files (163)

src/checksum/ChecksumArithmetic.lvt

namespace Livt.Net

/**
 * Shared network-order arithmetic for non-negative sums that fit int.
 */
class ChecksumArithmetic
{
	/**
	 * Adds the high and low 16-bit halves of a non-negative int; the result may still carry.
	 */
	public static inline fn Fold[](value: int) int
	{
		// The input contract excludes negatives; unsigned power-of-two operations
		// avoid signed quotient/remainder correction hardware.
		var unsignedValue = value as uint
		var low = unsignedValue & (0x0000FFFF as uint)
		return (low + (unsignedValue >> 16)) as int
	}

	/**
	 * Folds a non-negative int twice and returns its 16-bit one's complement.
	 */
	public static inline fn Finish[](value: int) int
	{
		var folded = ChecksumArithmetic.Fold(value)
		folded = ChecksumArithmetic.Fold(folded)
		return 0xFFFF - folded
	}
}