UnrealFixedMath/Source/UnrealFixedPoint/Public/Fixed.h

58 lines
1.6 KiB
C++

// UnrealFixedPoint Copyright Kevin Poretti
#pragma once
#include "CoreMinimal.h"
#include "Fixed.generated.h"
/**
*
*/
USTRUCT(BlueprintType)
struct UNREALFIXEDPOINT_API FFixed
{
GENERATED_BODY()
public:
// 48 bits for the whole portion, 16 bits for the fractional portion
static constexpr uint8 DecimalBitPosition = 16;
FFixed() : Data(0) { }
// int types <> FFixed conversions
FORCEINLINE FFixed(int64 A) { Data = A << DecimalBitPosition; }
FORCEINLINE operator int64() const { return Data >> DecimalBitPosition; }
/*
int8
int16
FORCEINLINE operator uint8()
*/
// floating point types <> FFixed conversions
FORCEINLINE FFixed(float A) { Data = static_cast<int64>(A * (1 << DecimalBitPosition) + (A >= 0 ? 0.5 : -0.5)); }
FORCEINLINE operator float() const { return static_cast<float>(Data) / (1 << DecimalBitPosition); }
FORCEINLINE FFixed(double A) { Data = static_cast<int64>(A * (1 << DecimalBitPosition) + (A >= 0 ? 0.5 : -0.5)); }
FORCEINLINE operator double() const { return static_cast<double>(Data) / (1 << DecimalBitPosition); }
// Arithmetic
FFixed operator+(const FFixed& Other) const { return Data + Other.Data; }
FFixed operator+=(const FFixed& Other) { Data += Other.Data; return Data; }
FFixed operator-(const FFixed& Other) const { return Data - Other.Data; }
FFixed operator-=(const FFixed& Other) { Data -= Other.Data; return Data; }
// Other overloads
FFixed operator=(const FFixed& Other) { Data = Other.Data; return Data; }
FFixed operator-() const { return -Data; }
private:
int64 Data;
};
namespace FFixedMath
{
}