%mathpiper,def="MathBitCount"

/** This file contains routines for numerical evaluation of elementary functions:
 *  PowerN, ExpN, SinN etc.
 *  It is assumed that the arguments are real (not complex) floating-point or integer numbers. (The {InNumericMode()} flag does not have to be set.)
 *  The result is an exact integer or a floating-point number correct to BuiltinPrecisionGet() digits.
 *
 *  If a better optimized version of these functions is available through the kernel,
 *  then the kernel version will automatically shadow these functions.
 *  These implementations are not necessarily the best optimized versions.
 */

///  MathBitCount: count number of bits in an integer or a float number.
/*  MathBitCount is now implemented through BigNumber::BitCount() */
/*  so this stays here as a reference implementation */
10 # MathBitCount(0) <-- 1;
20 # MathBitCount(_x) _ (x<0) <-- MathBitCount(-x);

30 # MathBitCount(_value) <--
[
	Local(nbits);
	nbits:=0;
  	If(value<1,
	[	// float value < 1, need to multiply by 2
		nbits := 1;
		While(value<1)
		[
			nbits--;
			value := MathMul2Exp(value,1);
		];
	],
	[	// need to divide by 2
		While(value>=1)
		[
			nbits++;
			value := MathMul2Exp(value, -1);
		];
	]);
	nbits;
];

%/mathpiper