/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 decimal4j (tools4j), Marco Terzer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.decimal4j.jmh.value;

import java.math.RoundingMode;
import java.util.Random;

import org.decimal4j.jmh.state.AbstractValueBenchmarkState;
import org.decimal4j.jmh.state.PowBenchmarkState;
import org.decimal4j.scale.ScaleMetrics;
import org.decimal4j.scale.Scales;
import org.decimal4j.truncate.OverflowMode;

/**
 * Describes the type of benchmark. This information is used when the benchmark
 * state is initialized to constrain the random generation of the test values,
 * for instance to avoid overflows or division by zero.
 */
public enum BenchmarkType {
	Add {
		@Override
		public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first) {
			//avoid overflows
			long second = valueType.random(SignType.ALL);
			while ((first ^ second) >= 0 & (first ^ (first + second)) < 0) {
				//overflow, try again
				second = valueType.random(SignType.ALL);
			}
			return second;
		}
	},
	Subtract {
		@Override
		public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first) {
			//avoid overflows
			long second = valueType.random(SignType.ALL);
			while ((first ^ second) < 0 & (first ^ (first - second)) < 0) {
				//overflow, try again
				second = valueType.random(SignType.ALL);
			}
			return second;
		}
	},
	Square {
		@Override
		public long randomFirst(AbstractValueBenchmarkState benchmarkState, ValueType valueType) {
			//avoid overflows
			final long one = Scales.getScaleMetrics(benchmarkState.scale).getScaleFactor();
			final long max = (long)Math.ceil(Math.sqrt(Long.MAX_VALUE * (double)one));//no long overflow because of sqrt
			final long value = randomLong(Math.min(max, valueType.maxValue));
			
			//positive or negative?
			return RND.nextBoolean() ? -value : value;
		}
		@Override
		public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first) {
			throw new RuntimeException("internal error: square has no second decimal argument");
		}
	},
	Sqrt {
		@Override
		public long randomFirst(AbstractValueBenchmarkState benchmarkState, ValueType valueType) {
			return valueType.random(SignType.POSITIVE);
		}
		@Override
		public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first) {
			throw new RuntimeException("internal error: sqrt has no second decimal argument");
		}
	},
	Multiply {
		@Override
		public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first) {
			//avoid overflows
			final long one = Scales.getScaleMetrics(benchmarkState.scale).getScaleFactor();
			if (-one <= first & first <= one) {
				//no overflow possible
				long value = valueType.random(SignType.ALL);
				while (first < 0 & value == Long.MIN_VALUE) {
					//overflow theoretically possible, hence try again
					value = valueType.random(SignType.ALL);
				}
				return value;
			}
			final long max = (long)Math.ceil(Long.MAX_VALUE * (one / Math.abs((double)first)));
			final long value = randomLong(Math.min(max, valueType.maxValue));
			
			//positive or negative?
			return RND.nextBoolean() ? -value : value;
		}
	},
	Divide {
		@Override
		public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first) {
			//avoid overflows
			final long one = Scales.getScaleMetrics(benchmarkState.scale).getScaleFactor();
			final long min = (long)Math.ceil(one * Math.abs((double)first) / Long.MAX_VALUE);
			final long value = min + randomLong(Math.max(0, valueType.maxValue - min));
			//NOTE: value may be larger than valueType.maxValue, but no overflow seems more important here
			
			//positive or negative?
			return RND.nextBoolean() ? -value : value;
		}
	},
	Avg {
		@Override
		public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first) {
			//no overflow possible
			return valueType.random(SignType.ALL);
		}
	},
	Pow {
		@Override
		public long randomFirst(AbstractValueBenchmarkState benchmarkState, ValueType valueType) {
			//create a base that does not overflow with the given exponent
			final PowBenchmarkState powState = (PowBenchmarkState)benchmarkState;
			final ScaleMetrics scaleMetrics = Scales.getScaleMetrics(benchmarkState.scale);
			final double maxBase = Math.pow(scaleMetrics.getMaxIntegerValue(), 1.0/powState.exponent);
			final double doubleValue = maxBase * Math.random() * Math.signum(Math.random());
			return scaleMetrics.getArithmetic(OverflowMode.CHECKED.getTruncationPolicyFor(RoundingMode.DOWN)).fromDouble(doubleValue);
		}
		@Override
		public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first) {
			throw new RuntimeException("internal error: pow has no second decimal argument");
		}
	},
	ConvertToDouble {
		@Override
		public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first) {
			throw new RuntimeException("internal error: conversion to double has no second decimal argument");
		}
	},
	Double {
		@Override
		public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first) {
			//double arith does not cause exceptions for overflow or div by zero
			return valueType.random(SignType.ALL);
		}
	};
	
	private static final Random RND = new Random();
	public long randomFirst(AbstractValueBenchmarkState benchmarkState, ValueType valueType) {
		return valueType.random(SignType.ALL);
	}
	abstract public long randomSecond(AbstractValueBenchmarkState benchmarkState, ValueType valueType, long first);

	private static long randomLong(long n) {
        if (n <= 0)
            throw new IllegalArgumentException("n must be positive, but was " + n);

        long bits, val;
        do {
            bits = RND.nextLong() >>> 1;
            val = bits % n;
        } while (bits - val + (n-1) < 0);
        return val;
	}
}

@@ -4449,32 +4449,135 @@
 avoid overflows%0A
+%09%09%09//one * first / second %3C= Long.MAX_VALUE %0A%09%09%09//   --%3E        second %3E= one * first / Long.MAX_VALUE%0A
 %09%09%09final long on
@@ -4776,17 +4776,17 @@
 ath.max(
-0
+1
 , valueT

fix value generation for division benchmarks