UInt96.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.statistics.descriptive;

  18. import java.math.BigInteger;
  19. import java.nio.ByteBuffer;

  20. /**
  21.  * A mutable 96-bit unsigned integer.
  22.  *
  23.  * <p>This is a specialised class to implement an accumulator of {@code long} values
  24.  * generated by squaring {@code int} values from an array (max observations=2^31).
  25.  *
  26.  * @since 1.1
  27.  */
  28. final class UInt96 {
  29.     /** Mask for the lower 32-bits of a long. */
  30.     private static final long MASK32 = 0xffff_ffffL;

  31.     // Low data is stored using an integer to allow efficient sum-with-carry addition

  32.     /** bits 32-1 (low 32-bits). */
  33.     private int c;
  34.     /** bits 96-33. */
  35.     private long ab;

  36.     /**
  37.      * Create an instance.
  38.      */
  39.     private UInt96() {
  40.         // No-op
  41.     }

  42.     /**
  43.      * Create an instance.
  44.      *
  45.      * @param x Value.
  46.      */
  47.     private UInt96(long x) {
  48.         c = (int) x;
  49.         ab = (int) (x >>> Integer.SIZE);
  50.     }

  51.     /**
  52.      * Create an instance using a direct binary representation.
  53.      * This is package-private for testing.
  54.      *
  55.      * @param hi High 64-bits.
  56.      * @param lo Low 32-bits.
  57.      */
  58.     UInt96(long hi, int lo) {
  59.         this.c = lo;
  60.         this.ab = hi;
  61.     }

  62.     /**
  63.      * Create an instance. The initial value is zero.
  64.      *
  65.      * @return the instance
  66.      */
  67.     static UInt96 create() {
  68.         return new UInt96();
  69.     }

  70.     /**
  71.      * Create an instance of the {@code long} value.
  72.      * The value is assumed to be an unsigned 64-bit integer.
  73.      *
  74.      * @param x Value (must be positive).
  75.      * @return the instance
  76.      */
  77.     static UInt96 of(long x) {
  78.         return new UInt96(x);
  79.     }

  80.     /**
  81.      * Adds the value. It is assumed to be positive, for example the square of an
  82.      * {@code int} value. However no check is performed for a negative value.
  83.      *
  84.      * <p>Note: This addition handles {@value Long#MIN_VALUE} as an unsigned
  85.      * value of 2^63.
  86.      *
  87.      * @param x Value.
  88.      */
  89.     void addPositive(long x) {
  90.         // Sum with carry.
  91.         // Assuming x is positive then x + lo will not overflow 64-bits
  92.         // so we do not have to split x into upper and lower 32-bit values.
  93.         final long s = x + (c & MASK32);
  94.         c = (int) s;
  95.         ab += s >>> Integer.SIZE;
  96.     }

  97.     /**
  98.      * Adds the value.
  99.      *
  100.      * @param x Value.
  101.      */
  102.     void add(UInt96 x) {
  103.         // Avoid issues adding to itself
  104.         final int cc = x.c;
  105.         final long aabb = x.ab;
  106.         // Sum with carry.
  107.         final long s = (cc & MASK32) + (c & MASK32);
  108.         c = (int) s;
  109.         ab += (s >>> Integer.SIZE) + aabb;
  110.     }

  111.     /**
  112.      * Convert to a BigInteger.
  113.      *
  114.      * @return the value
  115.      */
  116.     BigInteger toBigInteger() {
  117.         if (ab != 0) {
  118.             final ByteBuffer bb = ByteBuffer.allocate(Integer.BYTES * 3)
  119.                 .putLong(ab)
  120.                 .putInt(c);
  121.             return new BigInteger(1, bb.array());
  122.         }
  123.         return BigInteger.valueOf(c & MASK32);
  124.     }

  125.     /**
  126.      * Return the lower 32-bits as an {@code int} value.
  127.      *
  128.      * @return bits 32-1
  129.      */
  130.     int lo32() {
  131.         return c;
  132.     }

  133.     /**
  134.      * Return the higher 64-bits as a {@code long} value.
  135.      *
  136.      * @return bits 96-33
  137.      */
  138.     long hi64() {
  139.         return ab;
  140.     }
  141. }