MulDiv64.h

// MulDiv64.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2019

#pragma once
#ifndef Ceda_cxUtils_MulDiv64_H
#define Ceda_cxUtils_MulDiv64_H

#include "cxUtils.h"
#include "BasicTypes.h"

/*
Example

	int64 r = 0;
	int64 a = 0xaaaaaaaaaaaaaaaa;
	int64 b = 0x5555555555555555;
	int64 c = 0x1000000000000000;
	
	// This will return an incorrect result
	r = a * b / c;
	
	// This will return the correct result
	r = MulDiv64(a, b, c);
*/

namespace ceda
{
    // MulDiv64(a,b,c) = (a*b)/c
    // Used for scaling 64 bit integer values
    // Uses 128 bit intermediate result
    cxUtils_API int64 MulDiv64(int64 a, int64 b, int64 c);

    cxUtils_API uint64 UMulDiv64(uint64 a, uint64 b, uint64 c);
} // namespace ceda

#endif // include guard