Skip to content

Commit f3eb719

Browse files
rasmuskleistsalkinium
authored andcommitted
[math] Add BCD conversion algorithms
1 parent 3f2f647 commit f3eb719

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

src/modm/math/utils.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
#include "utils/operator.hpp"
2323
#include "utils/endianness.hpp"
2424
#include "utils/crc.hpp"
25+
#include "utils/bcd.hpp"
2526

2627
#endif // MODM_MATH_UTILS_HPP

src/modm/math/utils/bcd.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2023, Rasmus Kleist Hørlyck Sørensen
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#pragma once
13+
14+
#include <stdint.h>
15+
#include <cstdlib>
16+
17+
namespace modm
18+
{
19+
20+
/// @ingroup modm_math_utils
21+
/// @{
22+
23+
constexpr uint32_t
24+
fromBcd(uint32_t bcd)
25+
{
26+
uint32_t decimal = 0;
27+
for (uint16_t multiplier = 1; bcd; multiplier *= 10)
28+
{
29+
decimal += (bcd & 0b1111) * multiplier;
30+
bcd >>= 4;
31+
}
32+
return decimal;
33+
}
34+
35+
constexpr uint32_t
36+
toBcd(uint32_t decimal)
37+
{
38+
uint32_t bcd = 0;
39+
for (uint16_t shift = 0; decimal; shift += 4)
40+
{
41+
const auto dv = std::div(decimal, 10l);
42+
bcd |= dv.rem << shift;
43+
decimal = dv.quot;
44+
}
45+
return bcd;
46+
}
47+
48+
/// @}
49+
50+
} // namespace modm

test/modm/math/utils/bcd_test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2023, Rasmus Kleist Hørlyck Sørensen
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/math/utils/bcd.hpp>
13+
14+
#include "bcd_test.hpp"
15+
16+
void
17+
BcdTest::testFromBcd()
18+
{
19+
TEST_ASSERT_EQUALS(modm::fromBcd(0x01), 0x01U);
20+
TEST_ASSERT_EQUALS(modm::fromBcd(0x02), 0x02U);
21+
TEST_ASSERT_EQUALS(modm::fromBcd(0x03), 0x03U);
22+
TEST_ASSERT_EQUALS(modm::fromBcd(0x04), 0x04U);
23+
TEST_ASSERT_EQUALS(modm::fromBcd(0x05), 0x05U);
24+
TEST_ASSERT_EQUALS(modm::fromBcd(0x06), 0x06U);
25+
TEST_ASSERT_EQUALS(modm::fromBcd(0x07), 0x07U);
26+
TEST_ASSERT_EQUALS(modm::fromBcd(0x08), 0x08U);
27+
TEST_ASSERT_EQUALS(modm::fromBcd(0x09), 0x09U);
28+
}
29+
30+
void
31+
BcdTest::testToBcd()
32+
{
33+
TEST_ASSERT_EQUALS(modm::toBcd(0x01), 0x01U);
34+
TEST_ASSERT_EQUALS(modm::toBcd(0x02), 0x02U);
35+
TEST_ASSERT_EQUALS(modm::toBcd(0x03), 0x03U);
36+
TEST_ASSERT_EQUALS(modm::toBcd(0x04), 0x04U);
37+
TEST_ASSERT_EQUALS(modm::toBcd(0x05), 0x05U);
38+
TEST_ASSERT_EQUALS(modm::toBcd(0x06), 0x06U);
39+
TEST_ASSERT_EQUALS(modm::toBcd(0x07), 0x07U);
40+
TEST_ASSERT_EQUALS(modm::toBcd(0x08), 0x08U);
41+
TEST_ASSERT_EQUALS(modm::toBcd(0x09), 0x09U);
42+
}

test/modm/math/utils/bcd_test.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2023, Rasmus Kleist Hørlyck Sørensen
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <unittest/testsuite.hpp>
13+
14+
/// @ingroup modm_test_test_math
15+
class BcdTest : public unittest::TestSuite
16+
{
17+
public:
18+
void
19+
testFromBcd();
20+
21+
void
22+
testToBcd();
23+
};

0 commit comments

Comments
 (0)