# Class Util

java.lang.Object
org.apfloat.spi.Util

public class Util extends Object
Miscellaneous utility methods.

Version:
  1.16.0
Author:
  Mikko Tommila

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static long`
  `addExact(long x, long y)`
  Returns the sum of the arguments, throwing an exception if the result overflows a long.
  `static long`
  `ifFinite(long x, long y)`
  Returns the argument `y` limited to `Apfloat.INFINITE`.
  `static long`
  `ifFiniteOrZero(long x)`
  If `x` is negative, this is treated as a case of overflow, and `Apfloat.INFINITE` is returned.
  `static int`
  `log2down(int x)`
  Base-2 logarithm rounded down to nearest power of two.
  `static int`
  `log2down(long x)`
  Base-2 logarithm rounded down to nearest power of two.
  `static int`
  `log2up(int x)`
  Base-2 logarithm rounded up to nearest power of two.
  `static int`
  `log2up(long x)`
  Base-2 logarithm rounded up to nearest power of two.
  `static long`
  `max(long x, long... y)`
  Returns the maximum of the arguments.
  `static long`
  `min(long x, long... y)`
  Returns the minimum of the arguments.
  `static long`
  `multiplyExact(long x, long y)`
  Returns the product of the arguments, throwing an exception if the result overflows a long.
  `static long`
  `negateExact(long x)`
  Returns the argument negated, throwing an exception if the result overflows a long.
  `static int`
  `round23down(int x)`
  Round down to nearest power of two or three times a power of two.
  `static long`
  `round23down(long x)`
  Round down to nearest power of two or three times a power of two.
  `static int`
  `round23up(int x)`
  Round up to nearest power of two or three times a power of two.
  `static long`
  `round23up(long x)`
  Round up to nearest power of two or three times a power of two.
  `static int`
  `round2down(int x)`
  Round down to nearest power of two.
  `static long`
  `round2down(long x)`
  Round down to nearest power of two.
  `static int`
  `round2up(int x)`
  Round up to nearest power of two.
  `static long`
  `round2up(long x)`
  Round up to nearest power of two.
  `static int`
  `sqrt4down(int x)`
  Square root rounded down to nearest power of two.
  `static long`
  `sqrt4down(long x)`
  Square root rounded down to nearest power of two.
  `static int`
  `sqrt4up(int x)`
  Square root rounded up to nearest power of two.
  `static long`
  `sqrt4up(long x)`
  Square root rounded up to nearest power of two.
  `static <T> Stream<T>`
  `stream(Iterator<T> iterator)`
  Convert iterator to stream.
  `static long`
  `subtractExact(long x, long y)`
  Returns the difference of the arguments, throwing an exception if the result overflows a long.
  `static int`
  `toIntExact(long x)`
  Returns the argument converted to int, throwing an exception if the result overflows.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### round2down

    public static int round2down(int x)
    Round down to nearest power of two.
    Parameters:
      `x` - The input value, which must be non-negative.
    Returns:
      `x` rounded down to the nearest power of two.

  - ### round2down

    public static long round2down(long x)
    Round down to nearest power of two.
    Parameters:
      `x` - The input value, which must be non-negative.
    Returns:
      `x` rounded down to the nearest power of two.

  - ### round2up

    public static int round2up(int x)
    Round up to nearest power of two.
    Parameters:
      `x` - The input value, which must be non-negative and not greater than 230.
    Returns:
      `x` rounded up to the nearest power of two.

  - ### round2up

    public static long round2up(long x)
    Round up to nearest power of two.
    Parameters:
      `x` - The input value, which must be non-negative and not greater than 262.
    Returns:
      `x` rounded up to the nearest power of two.

  - ### round23down

    public static int round23down(int x)
    Round down to nearest power of two or three times a power of two.
    Parameters:
      `x` - The input value, which must be non-negative.
    Returns:
      `x` rounded down to nearest power of two or three times a power of two.

  - ### round23down

    public static long round23down(long x)
    Round down to nearest power of two or three times a power of two.
    Parameters:
      `x` - The input value, which must be non-negative.
    Returns:
      `x` rounded down to nearest power of two or three times a power of two.

  - ### round23up

    public static int round23up(int x)
    Round up to nearest power of two or three times a power of two.
    Parameters:
      `x` - The input value, which must be non-negative and not greater than 3 * 229.
    Returns:
      `x` rounded up to the nearest power of two or three times a power of two.

  - ### round23up

    public static long round23up(long x)
    Round up to nearest power of two or three times a power of two.
    Parameters:
      `x` - The input value, which must be non-negative and not greater than 3 * 261.
    Returns:
      `x` rounded up to the nearest power of two or three times a power of two.

  - ### sqrt4down

    public static int sqrt4down(int x)
    Square root rounded down to nearest power of two.
    Parameters:
      `x` - The input value, which must be non-negative.
    Returns:
      Square root of `x` rounded down to nearest power of two.

  - ### sqrt4down

    public static long sqrt4down(long x)
    Square root rounded down to nearest power of two.
    Parameters:
      `x` - The input value, which must be non-negative.
    Returns:
      Square root of `x` rounded down to nearest power of two.

  - ### sqrt4up

    public static int sqrt4up(int x)
    Square root rounded up to nearest power of two.
    Parameters:
      `x` - The input value, which must be non-negative.
    Returns:
      Square root of `x` rounded up to nearest power of two.

  - ### sqrt4up

    public static long sqrt4up(long x)
    Square root rounded up to nearest power of two.
    Parameters:
      `x` - The input value, which must be non-negative.
    Returns:
      Square root of `x` rounded up to nearest power of two.

  - ### log2down

    public static int log2down(int x)
    Base-2 logarithm rounded down to nearest power of two.
    Parameters:
      `x` - The input value, which must be positive.
    Returns:
      `log2(x)` rounded down to nearest integer.

  - ### log2down

    public static int log2down(long x)
    Base-2 logarithm rounded down to nearest power of two.
    Parameters:
      `x` - The input value, which must be positive.
    Returns:
      `log2(x)` rounded down to nearest integer.

  - ### log2up

    public static int log2up(int x)
    Base-2 logarithm rounded up to nearest power of two.
    Parameters:
      `x` - The input value, which must be positive.
    Returns:
      `log2(x)` rounded up to nearest integer.

  - ### log2up

    public static int log2up(long x)
    Base-2 logarithm rounded up to nearest power of two.
    Parameters:
      `x` - The input value, which must be positive.
    Returns:
      `log2(x)` rounded up to nearest integer.

  - ### ifFinite

    public static long ifFinite(long x, long y)
    Returns the argument `y` limited to `Apfloat.INFINITE`. In case `x` is `Apfloat.INFINITE`, then `Apfloat.INFINITE` is returned, otherwise `y`. Also if `y` is negative or zero, this is treated as a case of overflow, and `Apfloat.INFINITE` is returned. The return value is thus always positive.
    Parameters:
      `x` - The argument that is tested to be `Apfloat.INFINITE`.
      `y` - The argument that is returned if `x` is not `Apfloat.INFINITE`.
    Returns:
      `(x == Apfloat.INFINITE || y <= 0 ? Apfloat.INFINITE : y)`

  - ### ifFiniteOrZero

    public static long ifFiniteOrZero(long x)
    If `x` is negative, this is treated as a case of overflow, and `Apfloat.INFINITE` is returned. Otherwise returns `x`.
    Parameters:
      `x` - The argument that is tested to be negative.
    Returns:
      `(x < 0 ? Apfloat.INFINITE : x)`
    Since:
      1.16.0

  - ### max

    public static long max(long x, long... y)
    Returns the maximum of the arguments.
    Parameters:
      `x` - The first argument.
      `y` - More arguments.
    Returns:
      The maximum of the arguments

  - ### min

    public static long min(long x, long... y)
    Returns the minimum of the arguments.
    Parameters:
      `x` - The first argument.
      `y` - More arguments.
    Returns:
      The minimum of the arguments

  - ### multiplyExact

    public static long multiplyExact(long x, long y) throws OverflowException
    Returns the product of the arguments, throwing an exception if the result overflows a long.
    Parameters:
      `x` - The first operand.
      `y` - The second operand.
    Returns:
      `x * y`
    Throws:
      `OverflowException` - If the result overflows a long

  - ### addExact

    public static long addExact(long x, long y) throws OverflowException
    Returns the sum of the arguments, throwing an exception if the result overflows a long.
    Parameters:
      `x` - The first operand.
      `y` - The second operand.
    Returns:
      `x + y`
    Throws:
      `OverflowException` - If the result overflows a long

  - ### subtractExact

    public static long subtractExact(long x, long y) throws OverflowException
    Returns the difference of the arguments, throwing an exception if the result overflows a long.
    Parameters:
      `x` - The first operand.
      `y` - The second operand.
    Returns:
      `x - y`
    Throws:
      `OverflowException` - If the result overflows a long

  - ### negateExact

    public static long negateExact(long x) throws OverflowException
    Returns the argument negated, throwing an exception if the result overflows a long.
    Parameters:
      `x` - The operand.
    Returns:
      `-x`
    Throws:
      `OverflowException` - If the result overflows a long
    Since:
      1.16.0

  - ### toIntExact

    public static int toIntExact(long x) throws OverflowException
    Returns the argument converted to int, throwing an exception if the result overflows.
    Parameters:
      `x` - The operand.
    Returns:
      `(int) x`
    Throws:
      `OverflowException` - If the result overflows an int
    Since:
      1.15.0

  - ### stream

    public static <T> Stream<T> stream(Iterator<T> iterator)
    Convert iterator to stream.
    Type Parameters:
      `T` - The type.
    Parameters:
      `iterator` - The iterator.
    Returns:
      The stream.
    Since:
      1.12.0