/// <summary> /// Returns multiplier instance for given multiply mode. /// </summary> /// <param name="mode">Multiply mode.</param> /// <returns>Multiplier instance.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="mode" /> is out of range.</exception> public static IMultiplier GetMultiplier(MultiplyMode mode) { switch (mode) { case MultiplyMode.AutoFht: return AutoFhtMultiplier; case MultiplyMode.Classic: return ClassicMultiplier; default: throw new ArgumentOutOfRangeException("mode"); } }
/// <summary> /// Returns multiplier instance for given multiply mode. /// </summary> /// <param name="mode">Multiply mode.</param> /// <returns>Multiplier instance.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="mode" /> is out of range.</exception> static public IMultiplier GetMultiplier(MultiplyMode mode) { switch (mode) { case MultiplyMode.AutoFht: return(AutoFhtMultiplier); case MultiplyMode.Classic: return(ClassicMultiplier); default: throw new ArgumentOutOfRangeException("mode"); } }
/// <summary> /// Returns a specified big integer raised to the specified power. /// </summary> /// <param name="value">Number to raise.</param> /// <param name="power">Power.</param> /// <param name="multiplyMode">Multiply mode set explicitly.</param> /// <returns>Number in given power.</returns> /// <exception cref="ArgumentNullException"><paramref name="value" /> is a null reference.</exception> static public IntX Pow(IntX value, uint power, MultiplyMode multiplyMode) { // Exception if (ReferenceEquals(value, null)) { throw new ArgumentNullException("value"); } // Return one for zero pow if (power == 0) { return(1); } // Return the number itself from a power of one if (power == 1) { return(new IntX(value)); } // Return zero for a zero if (value._length == 0) { return(new IntX()); } // Get first one bit int msb = Bits.Msb(power); // Get multiplier IMultiplier multiplier = MultiplyManager.GetMultiplier(multiplyMode); // Do actual raising IntX res = value; for (uint powerMask = 1U << (msb - 1); powerMask != 0; powerMask >>= 1) { // Always square res = multiplier.Multiply(res, res); // Maybe mul if ((power & powerMask) != 0) { res = multiplier.Multiply(res, value); } } return(res); }
/// <summary> /// Returns multiplier instance for given multiply mode. /// </summary> /// <param name="mode">Multiply mode.</param> /// <returns>Multiplier instance.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="mode" /> is out of range.</exception> public static IMultiplier GetMultiplier(MultiplyMode mode) { // Check value if (!Enum.IsDefined(typeof(MultiplyMode), mode)) { throw new ArgumentOutOfRangeException("mode"); } switch (mode) { case MultiplyMode.AutoFht: return AutoFhtMultiplier; default: return ClassicMultiplier; } }
/// <summary> /// Returns multiplier instance for given multiply mode. /// </summary> /// <param name="mode">Multiply mode.</param> /// <returns>Multiplier instance.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="mode" /> is out of range.</exception> static public IMultiplier GetMultiplier(MultiplyMode mode) { // Check value //if (!Enum.IsDefined(typeof(MultiplyMode), mode)) //{ // throw new ArgumentOutOfRangeException("mode"); //} switch (mode) { case MultiplyMode.AutoFht: return(AutoFhtMultiplier); default: return(ClassicMultiplier); } }
/// <summary> /// Returns a specified big integer raised to the specified power. /// </summary> /// <param name="value">Number to raise.</param> /// <param name="power">Power.</param> /// <param name="multiplyMode">Multiply mode set explicitly.</param> /// <returns>Number in given power.</returns> /// <exception cref="ArgumentNullException"><paramref name="value" /> is a null reference.</exception> static public IntX Pow(IntX value, uint power, MultiplyMode multiplyMode) { // Exception if (ReferenceEquals(value, null)) { throw new ArgumentNullException("value"); } // Return one for zero pow if (power == 0) return 1; // Return the number itself from a power of one if (power == 1) return new IntX(value); // Return zero for a zero if (value._length == 0) return new IntX(); // Get first one bit int msb = Bits.Msb(power); // Get multiplier IMultiplier multiplier = MultiplyManager.GetMultiplier(multiplyMode); // Do actual raising IntX res = value; for (uint powerMask = 1U << (msb - 1); powerMask != 0; powerMask >>= 1) { // Always square res = multiplier.Multiply(res, res); // Maybe mul if ((power & powerMask) != 0) { res = multiplier.Multiply(res, value); } } return res; }