-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,186 additions
and
887 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) 2021, Thomas Sommer | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
|
||
#include <modm/math/utils/integer_traits.hpp> | ||
#include <modm/architecture/interface/assert.hpp> | ||
|
||
namespace modm { | ||
|
||
/** | ||
* @brief Unsigned integer with arbitrary digits and scaling value on conversion | ||
* between instances with different digits. | ||
* | ||
* @tparam D Number of Digits | ||
* | ||
* @author Thomas Sommer | ||
* @ingroup modm_math | ||
*/ | ||
template<int D> | ||
requires (D > 0) | ||
class ProportionalUnsigned { | ||
public: | ||
static constexpr int Digits = D; | ||
|
||
using T = uint_t<D>::least; | ||
static constexpr T min = 0; | ||
static constexpr T max = bitmask<D>(); | ||
|
||
constexpr ProportionalUnsigned() = default; | ||
|
||
constexpr ProportionalUnsigned(T value) { | ||
if(std::is_constant_evaluated()) | ||
this->value = std::min(value, max); | ||
else { | ||
modm_assert_continue_fail_debug(value <= max, "ProportionalUnsigned", "value out of range"); | ||
// Constructing runtime should a.f.a.p. | ||
this->value = value; | ||
} | ||
} | ||
|
||
// Construct from bigger or equal ProportionalUnsigned | ||
template <int E, std::enable_if_t<(D <= E), void*> = nullptr> | ||
constexpr ProportionalUnsigned(const ProportionalUnsigned<E>& other) | ||
: value(other.value >> (E - D)) {} | ||
|
||
template <int E, std::enable_if_t<(D <= E), void*> = nullptr> | ||
constexpr ProportionalUnsigned(ProportionalUnsigned<E> &&other) | ||
: value(other.value >> (E - D)) {} | ||
|
||
// Construct from smaller ProportionalUnsigned | ||
template <int E, std::enable_if_t<(D > E), void*> = nullptr> | ||
constexpr ProportionalUnsigned(const ProportionalUnsigned<E>& other) | ||
: value(other.value * max / other.max) | ||
{} | ||
|
||
template <int E, std::enable_if_t<(D > E), void*> = nullptr> | ||
constexpr ProportionalUnsigned(ProportionalUnsigned<E> &&other) | ||
: value(other.value * max / other.max) | ||
{} | ||
|
||
/* // Faster construction for D == 1 | ||
constexpr ProportionalUnsigned(const ProportionalUnsigned<1> &other) : value(other.value ? bitmask<D>() : 0){} | ||
// constexpr ProportionalUnsigned(ProportionalUnsigned<1> &&other) : value(other.value ? bitmask<D>() : 0){} | ||
constexpr ProportionalUnsigned& operator=(const ProportionalUnsigned<1> &other) { | ||
value = other.value ? bitmask<D>() : 0; | ||
return *this; | ||
} */ | ||
|
||
// Cast to underlying type. No need to define getters and comparison operators. | ||
// @see https://en.cppreference.com/w/cpp/language/cast_operator | ||
operator T() const | ||
{ return value; } | ||
|
||
// Assign ProportionalUnsigned with more or equal Digits | ||
template <int E, std::enable_if_t<(D <= E), void*> = nullptr> | ||
constexpr void operator=(const ProportionalUnsigned<E>& other) { | ||
value = other.value >> (E - D); | ||
} | ||
|
||
// Assign ProportionalUnsigned with less Digits | ||
template <int E, std::enable_if_t<(D > E), void*> = nullptr> | ||
constexpr void operator=(const ProportionalUnsigned<E>& other) { | ||
value = other.value * max / other.max; | ||
} | ||
|
||
constexpr void setValue(T value) { | ||
if(std::is_constant_evaluated()) | ||
this->value = std::min(value, max); | ||
else { | ||
modm_assert_continue_fail_debug(value <= max, "ProportionalUnsigned", "value out of range"); | ||
// Calling setValue() runtime should a.f.a.p. | ||
this->value = value; | ||
} | ||
} | ||
|
||
protected: | ||
T value{0}; | ||
|
||
private: | ||
template<int> | ||
friend class ProportionalUnsigned; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021, Thomas Sommer | ||
# | ||
# This file is part of the modm project. | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# ----------------------------------------------------------------------------- | ||
|
||
def init(module): | ||
module.name = ":math:proportional_unsigned" | ||
module.description = """ | ||
# Proportional Unsigned | ||
Unsigned integer with arbitrary digits D and proportional scaling between | ||
instances with different digits. F.e. a ProportionalUnsigned<2> with the value | ||
0b11 converted to a to a ProportionalUnsigned<4> becomes the value 0b1111. | ||
It's the baseclass to color::Gray and thus to any other colors too but may have more applications. | ||
""" | ||
|
||
def prepare(module, options): | ||
module.depends( | ||
":utils", | ||
":architecture:assert" | ||
) | ||
return True | ||
|
||
def build(env): | ||
env.outbasepath = "modm/src/modm/math" | ||
env.copy("proportional_unsigned.hpp") |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.