tiny-skia-cpp
A C++20 2D rendering library (port of tiny-skia)
Loading...
Searching...
No Matches
FloatingPoint.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstdint>
7#include <optional>
8
9namespace tiny_skia {
10
13 public:
14 static const NormalizedF32 ZERO;
15 static const NormalizedF32 ONE;
16
17 constexpr NormalizedF32() = default;
18 explicit constexpr NormalizedF32(float value) : value_(value) {}
19
20 static constexpr NormalizedF32 newUnchecked(float value) { return NormalizedF32(value); }
21
23 [[nodiscard]] static std::optional<NormalizedF32> create(float value) { return newFloat(value); }
24
25 [[nodiscard]] static std::optional<NormalizedF32> newFloat(float value);
27 [[nodiscard]] static NormalizedF32 newClamped(float value);
28 [[nodiscard]] static NormalizedF32 fromU8(std::uint8_t value);
29
30 [[nodiscard]] constexpr float get() const { return value_; }
31
32 constexpr bool operator==(const NormalizedF32&) const = default;
33 constexpr bool operator<(const NormalizedF32& o) const { return value_ < o.value_; }
34 constexpr bool operator<=(const NormalizedF32& o) const { return value_ <= o.value_; }
35 constexpr bool operator>(const NormalizedF32& o) const { return value_ > o.value_; }
36 constexpr bool operator>=(const NormalizedF32& o) const { return value_ >= o.value_; }
37
38 [[nodiscard]] static constexpr NormalizedF32 zero() { return NormalizedF32(0.0f); }
39 [[nodiscard]] static constexpr NormalizedF32 one() { return NormalizedF32(1.0f); }
40
41 private:
42 float value_ = 0.0f;
43};
44
45inline constexpr NormalizedF32 NormalizedF32::ZERO = NormalizedF32(0.0f);
46inline constexpr NormalizedF32 NormalizedF32::ONE = NormalizedF32(1.0f);
47
50class NormalizedF32Exclusive {
51 public:
52 static const NormalizedF32Exclusive ANY;
53 static const NormalizedF32Exclusive HALF;
54
55 [[nodiscard]] static std::optional<NormalizedF32Exclusive> create(float v);
56 [[nodiscard]] static NormalizedF32Exclusive newBounded(float v);
57
58 [[nodiscard]] constexpr float get() const { return value_; }
59 [[nodiscard]] NormalizedF32 toNormalized() const;
60
61 constexpr bool operator==(const NormalizedF32Exclusive&) const = default;
62 constexpr bool operator<(const NormalizedF32Exclusive& o) const { return value_ < o.value_; }
63
64 private:
65 explicit constexpr NormalizedF32Exclusive(float v) : value_(v) {}
66 float value_ = 0.5f;
67};
68
69inline constexpr NormalizedF32Exclusive NormalizedF32Exclusive::ANY = NormalizedF32Exclusive(0.5f);
70inline constexpr NormalizedF32Exclusive NormalizedF32Exclusive::HALF = NormalizedF32Exclusive(0.5f);
71
74class NonZeroPositiveF32 {
75 public:
76 [[nodiscard]] static std::optional<NonZeroPositiveF32> create(float v);
77 [[nodiscard]] constexpr float get() const { return value_; }
78
79 private:
80 explicit constexpr NonZeroPositiveF32(float v) : value_(v) {}
81 float value_ = 1.0f;
82};
83
86class FiniteF32 {
87 public:
88 [[nodiscard]] static std::optional<FiniteF32> create(float v);
89 [[nodiscard]] constexpr float get() const { return value_; }
90
91 private:
92 explicit constexpr FiniteF32(float v) : value_(v) {}
93 float value_ = 0.0f;
94};
95
97[[nodiscard]] std::int32_t saturateCastI32(float x);
99[[nodiscard]] std::int32_t saturateCastI32(double x);
101[[nodiscard]] std::int32_t saturateFloorToI32(float x);
103[[nodiscard]] std::int32_t saturateCeilToI32(float x);
105[[nodiscard]] std::int32_t saturateRoundToI32(float x);
107[[nodiscard]] std::int32_t f32As2sCompliment(float x);
108
109} // namespace tiny_skia
A float guaranteed to be in [0, 1].
Definition FloatingPoint.h:12
static NormalizedF32 newClamped(float value)
Clamps to [0,1].
static std::optional< NormalizedF32 > create(float value)
Returns nullopt if outside [0,1] or non-finite.
Definition FloatingPoint.h:23