From ef5545f5bd5079483bf34faeaf1fc3bb17294d02 Mon Sep 17 00:00:00 2001 From: Kevin Poretti Date: Sat, 11 Feb 2023 17:37:37 -0500 Subject: [PATCH] Conversions, assignment, and addition/subtraction --- README.md | 9 ++++ Source/UnrealFixedPoint/Private/Fixed.cpp | 3 ++ Source/UnrealFixedPoint/Public/Fixed.h | 57 +++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 Source/UnrealFixedPoint/Private/Fixed.cpp create mode 100644 Source/UnrealFixedPoint/Public/Fixed.h diff --git a/README.md b/README.md index 00ae41f..a7a4c09 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,15 @@ This library aims to closely match the interface and functionality of the existi - [ ] Fixed point version of FRotator - [ ] FFixedRotator class implementation - [ ] Corresponding FMath and UKismetMathLibrary utility functions +- [ ] Blueprints + - [ ] Expose fixed arithmatic as pure Blueprint nodes + - [ ] Ability to edit fixed nums with float sliders/input fields + - [ ] Automatically do float <> fixed conversion when tweaking fixed point numbers in the editor +- [ ] Plugin config + - [ ] Configure how fixed nums are shown in editor when exposed as UPROPERTY (as floats only, as float and underlying int, as underlying int only) +- [ ] Handling errors and edge cases + - [ ] Overflow + - [ ] Infinity ### What is fixed point math? diff --git a/Source/UnrealFixedPoint/Private/Fixed.cpp b/Source/UnrealFixedPoint/Private/Fixed.cpp new file mode 100644 index 0000000..a29bbf0 --- /dev/null +++ b/Source/UnrealFixedPoint/Private/Fixed.cpp @@ -0,0 +1,3 @@ +// UnrealFixedPoint Copyright Kevin Poretti + +#include "Fixed.h" diff --git a/Source/UnrealFixedPoint/Public/Fixed.h b/Source/UnrealFixedPoint/Public/Fixed.h new file mode 100644 index 0000000..299eb86 --- /dev/null +++ b/Source/UnrealFixedPoint/Public/Fixed.h @@ -0,0 +1,57 @@ +// 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(A * (1 << DecimalBitPosition) + (A >= 0 ? 0.5 : -0.5)); } + FORCEINLINE operator float() const { return static_cast(Data) / (1 << DecimalBitPosition); } + + FORCEINLINE FFixed(double A) { Data = static_cast(A * (1 << DecimalBitPosition) + (A >= 0 ? 0.5 : -0.5)); } + FORCEINLINE operator double() const { return static_cast(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 +{ + +}