Road to 0.6.0

- readd the c++ linear allocator for 3ds
- start switching from PD::Vec to std::vector
- Add Color::Hex as constexpr for compiletime color converts
- Add FNV Hasing functions
- Make UI7 ids be able to be generated at compile time
- Added a Throw Function (for whatever)
- Added HexCHar2Int (replaces the lookup table)
- Made u128 fully constexpr
This commit is contained in:
2025-12-10 19:02:54 +01:00
parent 91754558f7
commit f19c947fc3
23 changed files with 262 additions and 120 deletions

View File

@@ -24,6 +24,7 @@ SOFTWARE.
*/
#include <pd/core/common.hpp>
#include <pd/core/strings.hpp>
namespace PD {
class PD_CORE_API Color {
@@ -65,17 +66,34 @@ class PD_CORE_API Color {
b(static_cast<u8>(255.f * b)),
a(static_cast<u8>(255.f * a)) {}
/**
* Constructor for Hex Input
* Constructor for Hex Input (is abel to run at compile time xD)
* @param hex Hex String in `#ffffff` or `#ffffffff` format
*/
Color(const std::string& hex) { Hex(hex); }
constexpr Color(const std::string_view& hex) { Hex(hex); }
/**
* Create Color Object by Hex String
* Create Color Object by Hex String (at compile time btw)
* @param hex Hex String in `#ffffff` or `#ffffffff` format
* @return Color class itself
*/
Color& Hex(const std::string& hex);
constexpr Color& Hex(const std::string_view& hex) {
if (!(hex.length() == 7 || hex.length() == 9)) {
throw "[PD] Color: hex string is not rgb or rgba!";
}
r = PD::Strings::HexChar2Int(hex[1]) * 16 +
PD::Strings::HexChar2Int(hex[2]);
g = PD::Strings::HexChar2Int(hex[3]) * 16 +
PD::Strings::HexChar2Int(hex[4]);
b = PD::Strings::HexChar2Int(hex[5]) * 16 +
PD::Strings::HexChar2Int(hex[6]);
if (hex.length() == 9) {
a = PD::Strings::HexChar2Int(hex[7]) * 16 +
PD::Strings::HexChar2Int(hex[8]);
} else {
a = 255;
}
return *this;
}
/**
* Convert this Color Object to Hex string
* @param rgba [default false] sets if 8 or 6 digit color should be returned
@@ -89,7 +107,7 @@ class PD_CORE_API Color {
* @param p Amount (supports -1.0 to 1.0 for use of sine)
* @return Class Reference
*/
Color& Fade(const Color& color, float p) {
constexpr Color& Fade(const Color& color, float p) {
a = static_cast<u8>((color.a - a) * ((p + 1.f) / 2));
b = static_cast<u8>((color.b - b) * ((p + 1.f) / 2));
g = static_cast<u8>((color.g - g) * ((p + 1.f) / 2));
@@ -101,12 +119,12 @@ class PD_CORE_API Color {
* Get 32Bit Color Value
* @return 32Bit Color Value (ABGR iirc)
*/
u32 Get() const { return (a << 24) | (b << 16) | (g << 8) | r; }
constexpr u32 Get() const { return (a << 24) | (b << 16) | (g << 8) | r; }
/**
* Get The Luminance of the Color
* @return luminance (from 0.0 to 1.0)
*/
float Luminance() const {
constexpr float Luminance() const {
// For Reference https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness
return (0.3 * (r / 255.f) + 0.59 * (g / 255.f) + 0.11 * (b / 255.f));
}
@@ -114,13 +132,13 @@ class PD_CORE_API Color {
* Check if the Color is Light or Dark
* @return true if light
*/
bool IsLight() const { return (Luminance() >= 0.5); }
constexpr bool IsLight() const { return (Luminance() >= 0.5); }
/**
* Operator to cast Color to 32Bit Value
* @return 32Bit Color Value
*/
operator u32() const { return Get(); }
constexpr operator u32() const { return Get(); }
/** Public Access Data section */
u8 r;

View File

@@ -62,6 +62,9 @@ SOFTWARE.
#define PD_BIT(x) (1 << x)
namespace PD {
[[noreturn]] inline void Throw(const std::string& str) {
throw std::runtime_error("[PD] " + str);
}
/** Types */
using u8 = unsigned char;
using u16 = unsigned short;

View File

@@ -25,6 +25,7 @@ SOFTWARE.
#include <pd/core/bit_util.hpp>
#include <pd/core/color.hpp>
#include <pd/core/fnv.hpp>
#include <pd/core/io.hpp>
#include <pd/core/mat.hpp>
#include <pd/core/sl/sl.hpp>

53
include/pd/core/fnv.hpp Normal file
View File

@@ -0,0 +1,53 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7)
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.
*/
#include <pd/core/common.hpp>
namespace PD {
/**
* FNV-1a 32Bit hasing function
* https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
*/
constexpr u32 FNV1A32(std::string_view str) {
u32 ret = 0x811c9dc5; // Offset basis
for (auto& it : str) {
ret ^= it;
ret *= 0x01000193; // Prime
}
return ret;
}
/**
* FNV-1a 64Bit hasing function
* https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
*/
constexpr u64 FNV1A64(std::string_view str) {
u64 ret = 0xcbf29ce484222325; // Offset basis
for (auto& it : str) {
ret ^= it;
ret *= 0x00000100000001b3; // Prime
}
return ret;
}
} // namespace PD

View File

@@ -28,18 +28,18 @@ class u128 {
return u128();
}
u128 operator+(const u128& v) const {
constexpr u128 operator+(const u128& v) const {
u128 ret;
ret.pLow = pLow + v.pLow;
ret.pHigh = pHigh + v.pHigh + (ret.pLow < pLow);
return ret;
}
u128 operator&(const u128& v) const {
constexpr u128 operator&(const u128& v) const {
return u128(pLow & v.pLow, pHigh & v.pHigh);
}
u128 operator<<(u32 s) const {
constexpr u128 operator<<(u32 s) const {
if (s == 0) {
return *this;
}
@@ -52,7 +52,7 @@ class u128 {
return u128(pLow << s, (pHigh << s) | (pLow >> (64 - s)));
}
u128 operator>>(u32 s) const {
constexpr u128 operator>>(u32 s) const {
if (s == 0) {
return *this;
}
@@ -65,23 +65,23 @@ class u128 {
return u128((pLow >> s) | (pHigh << (64 - s)), pHigh >> s);
}
u128& operator|=(const u128& v) {
constexpr u128& operator|=(const u128& v) {
pLow |= v.pLow;
pHigh |= v.pHigh;
return *this;
}
u128 operator|(const u128& v) const {
constexpr u128 operator|(const u128& v) const {
return u128(pLow | v.pLow, pHigh | v.pHigh);
}
u128& operator&=(const u128& v) {
constexpr u128& operator&=(const u128& v) {
pLow &= v.pLow;
pHigh &= v.pHigh;
return *this;
}
u128 operator~() const { return u128(~pLow, ~pHigh); }
constexpr u128 operator~() const { return u128(~pLow, ~pHigh); }
/**
* Old why to make if checks possible
@@ -92,7 +92,7 @@ class u128 {
// return pLow & v.pLow || pHigh & v.pHigh;
// }
bool operator==(const u128& v) const {
constexpr bool operator==(const u128& v) const {
return pLow == v.pLow && pHigh == v.pHigh;
}
@@ -100,12 +100,14 @@ class u128 {
* Use explicit here to make sure it is only for checking and not for
* some error leading implicit bool assignments...
*/
explicit operator bool() const { return pLow != 0 || pHigh != 0; }
constexpr explicit operator bool() const { return pLow != 0 || pHigh != 0; }
/** Deprecated way to handle `flag & SomeFlag` */
bool Has(const u128& v) const { return pLow & v.pLow || pHigh & v.pHigh; }
constexpr bool Has(const u128& v) const {
return pLow & v.pLow || pHigh & v.pHigh;
}
bool operator!=(const u128& v) const { return !(*this == v); }
constexpr bool operator!=(const u128& v) const { return !(*this == v); }
};
} // namespace PD

View File

@@ -30,6 +30,13 @@ namespace PD {
* Set of String Utillity Functions
*/
namespace Strings {
constexpr int HexChar2Int(char c) {
/** Imagine mat hat ne lookup table dafür verwendet :/ */
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
if (c >= 'A' && c <= 'F') return 10 + (c - 'a');
return -1; // Error
}
/**
* Check if a String ends with a specific extension
* @param str Input string

View File

@@ -30,7 +30,7 @@ namespace PD {
* Class to calculate Maximum/Minimum and Average Timings
*/
class TimeStats {
public:
public:
/**
* Constructor taking a lengh for the List
* @param l Lengh of the data list
@@ -55,7 +55,8 @@ class TimeStats {
* @return Average
*/
u64 GetAverage() {
if (!num_val) return 0.f;
if (!num_val)
return 0.f;
u64 res = 0;
for (int i = 0; i < num_val; i++) {
res += val[smart_idx(i)];
@@ -68,7 +69,8 @@ class TimeStats {
* @return Minimum value
*/
u64 GetMin() {
if (!num_val) return 0.f;
if (!num_val)
return 0.f;
u64 res = std::numeric_limits<u64>::max();
for (int i = 0; i < num_val; i++) {
res = std::min(val[smart_idx(i)], res);
@@ -81,7 +83,8 @@ class TimeStats {
* @return Max Value
*/
u64 GetMax() {
if (!num_val) return 0.f;
if (!num_val)
return 0.f;
u64 res = 0;
for (int i = 0; i < num_val; i++) {
res = std::max(val[smart_idx(i)], res);
@@ -119,7 +122,7 @@ class TimeStats {
*/
const size_t GetNumValues() { return num_val; }
private:
private:
/**
* Get the Next Position to write to
* @param c current position
@@ -149,9 +152,9 @@ namespace TT {
* Data Structure for a TimeTrace Result
*/
class Res {
public:
public:
/** Constructore that Inits a protocol at size of 60 frames */
Res(): start(0), end(0) { protocol = TimeStats::New(60); }
Res() : start(0), end(0) { protocol = TimeStats::New(60); }
~Res() = default;
PD_SHARED(Res);
@@ -201,7 +204,7 @@ class Res {
*/
TimeStats::Ref GetProtocol() { return protocol; }
private:
private:
/** Trace ID */
std::string id;
/** Start time */
@@ -237,7 +240,7 @@ PD_CORE_API void End(const std::string &id);
* ```
*/
class Scope {
public:
public:
/**
* Constructor requiring a Name for the Trace
* @param id Name of the Trace
@@ -251,9 +254,9 @@ class Scope {
*/
~Scope() { End(ID); }
private:
private:
/** Trace Name/ID */
std::string ID;
};
} // namespace TT
} // namespace PD
} // namespace TT
} // namespace PD