import { floor } from './math.js';
import { BITS31, BITS32 } from './binary.js';

/**
 * Utility helpers for working with numbers.
 *
 * @module number
 */


const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
const MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER;

const LOWEST_INT32 = 1 << 31;
const HIGHEST_INT32 = BITS31;
const HIGHEST_UINT32 = BITS32;

/* c8 ignore next */
const isInteger = Number.isInteger || (num => typeof num === 'number' && isFinite(num) && floor(num) === num);
const isNaN = Number.isNaN;
const parseInt = Number.parseInt;

/**
 * Count the number of "1" bits in an unsigned 32bit number.
 *
 * Super fun bitcount algorithm by Brian Kernighan.
 *
 * @param {number} n
 */
const countBits = n => {
  n &= BITS32;
  let count = 0;
  while (n) {
    n &= (n - 1);
    count++;
  }
  return count
};

export { HIGHEST_INT32, HIGHEST_UINT32, LOWEST_INT32, MAX_SAFE_INTEGER, MIN_SAFE_INTEGER, countBits, isInteger, isNaN, parseInt };
