/*******************************************************************************
***  This file is part of Pecunia.                                           ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021, 2023                        ***
***  John Schneiderman <Licencing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Lesser General Public License as published   ***
***  by the Free Software Foundation, either version 3 of the License, or    ***
***  (at your option) any later version.                                     ***
***                                                                          ***
***  This program is distributed in the hope that it will be useful, but     ***
***  WITHOUT ANY WARRANTY; without even the implied warranty of              ***
***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    ***
***  See the GNU Lesser General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Lesser General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*******************************************************************************/
#include "Verification.h"
using std::int32_t;
using std::int64_t;
using std::uint8_t;
using pecunia::precision;
using pecunia::MinorUnit;
using pecunia::FloatingPointBase;
using pecunia::UnitStorage;
using pecunia::internal::VerifiedValue;

#include <cmath>
using std::abs;
using std::ceil;
using std::fabs;
using std::floor;
using std::fmod;
using std::modf;
using std::pow;
#include <cfenv>
using std::feclearexcept;
using std::fetestexcept;
#include <stdexcept>
using std::domain_error;
using std::overflow_error;
using std::underflow_error;
#include <utility>
using std::move;

#include <pecunia/FloatingPoint.h>
using pecunia::FloatingPoint;


VerifiedValue pecunia::internal::verifyAdditionFits(
	const UnitStorage& lhs,
	const UnitStorage& rhs,
	const UnitStorage& maximum,
	const UnitStorage& minimum
)
{
	const UnitStorage maximumDifference{static_cast<UnitStorage>(maximum - rhs)};
	if ((rhs > 0) && (lhs > maximumDifference))
		return VerifiedValue::Overflows;

	const UnitStorage minimumDifference{static_cast<UnitStorage>(minimum - rhs)};
	if ((rhs < 0) && (lhs < minimumDifference))
		return VerifiedValue::Overflows;
	return VerifiedValue::Fits;
}

VerifiedValue pecunia::internal::verifySubtractionFits(
	const UnitStorage& lhs,
	const UnitStorage& rhs,
	const UnitStorage& maximum,
	const UnitStorage& minimum
)
{
	const UnitStorage maximumDifference{static_cast<UnitStorage>(maximum + rhs)};
	if ((rhs < 0) && (lhs > maximumDifference))
		return VerifiedValue::Overflows;

	const UnitStorage minimumDifference{static_cast<UnitStorage>(minimum + rhs)};
	if ((rhs > 0) && (lhs < minimumDifference))
		return VerifiedValue::Overflows;
	return VerifiedValue::Fits;
}

VerifiedValue pecunia::internal::verifyMultiplicationFits(
	const UnitStorage& lhs,
	const FloatingPoint& rhs,
	const UnitStorage& maximum,
	const UnitStorage& minimum,
	const MinorUnit& minorMaximum,
	const uint8_t minorPrecision
)
{
	if ((lhs == 0) || rhs.isEqual(0, minorPrecision))
		return VerifiedValue::Fits;

	if ((lhs == -1) || rhs.isEqual(-1, minorPrecision))
		return VerifiedValue::Fits;

	const auto fpLhs{static_cast<FloatingPointBase>(lhs)};
	const auto fpMaximum{static_cast<FloatingPointBase>(maximum)};
	if (fpLhs > fpMaximum / rhs)
		return VerifiedValue::Overflows;

	const auto fpMinimum{static_cast<FloatingPointBase>(minimum)};
	if (fpLhs < fpMinimum / rhs)
		return VerifiedValue::Overflows;

	if (rhs < 1 && rhs > -1)
	{
		return verifyDivisionFits(
			lhs,
			rhs.reciprocal(),
			maximum,
			minimum,
			minorMaximum,
			minorPrecision
		);
	}
	return VerifiedValue::Fits;
}

VerifiedValue pecunia::internal::verifyDivisionFits(
	const UnitStorage& lhs,
	const FloatingPoint& rhs,
	const UnitStorage& maximum,
	const UnitStorage& minimum,
	const MinorUnit& minorMaximum,
	const uint8_t minorPrecision
)
{
	if (rhs.isEqual(0, minorPrecision))
		return VerifiedValue::Undefined;

	if (rhs < 1 && rhs > -1)
	{
		return verifyMultiplicationFits(
			lhs,
			rhs.reciprocal(),
			maximum,
			minimum,
			minorMaximum,
			minorPrecision
		);
	}

	const auto fpLhs{static_cast<FloatingPointBase>(lhs)};
	if ((lhs > 0) && (lhs <= minorMaximum) && (rhs > fpLhs))
		return VerifiedValue::Underflows;
	return VerifiedValue::Fits;
}

VerifiedValue pecunia::internal::verifyMultiplicationFits(
	const UnitStorage& lhs,
	const UnitStorage& rhs,
	const UnitStorage& maximum,
	const UnitStorage& minimum
)
{
	if ((lhs == 0) || rhs == 0)
		return VerifiedValue::Fits;

	if ((lhs == -1) || rhs == -1)
		return VerifiedValue::Fits;

	if (lhs > maximum / rhs)
		return VerifiedValue::Overflows;

	if (lhs < minimum / rhs)
		return VerifiedValue::Overflows;
	return VerifiedValue::Fits;
}

VerifiedValue pecunia::internal::verifyDivisionFits(
	const UnitStorage& lhs,
	const UnitStorage& rhs,
	const MinorUnit& minorMaximum
)
{
	if (rhs == 0)
		return VerifiedValue::Undefined;

	if (lhs > 0 && lhs <= minorMaximum && rhs > lhs)
		return VerifiedValue::Underflows;
	return VerifiedValue::Fits;
}
