avg

Git Source

Calculates the arithmetic average of x and y using the following formula: $$ avg(x, y) = (x & y) + ((xUint ^ yUint) / 2) $$ In English, this is what this formula does:

  1. AND x and y.
  2. Calculate half of XOR x and y.
  3. Add the two results together. This technique is known as SWAR, which stands for "SIMD within a register". You can read more about it here: https://devblogs.microsoft.com/oldnewthing/20220207-00/?p=106223

*Notes:

  • The result is rounded down.*
function avg(UD50x28 x, UD50x28 y) pure returns (UD50x28 result);

Parameters

NameTypeDescription
xUD50x28The first operand as a UD50x28 number.
yUD50x28The second operand as a UD50x28 number.

Returns

NameTypeDescription
resultUD50x28The arithmetic average as a UD50x28 number.