mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #17702 from benpicco/core/macros/math
core/macros: add math helper macros
This commit is contained in:
commit
4737d8148a
56
core/include/macros/math.h
Normal file
56
core/include/macros/math.h
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2022 ML!PA Consulting GmbH
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup core_macros
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Math helper macros
|
||||
*
|
||||
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
|
||||
*/
|
||||
|
||||
#ifndef MACROS_MATH_H
|
||||
#define MACROS_MATH_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Returns the sign of @p a, either -1 or 1
|
||||
*/
|
||||
#define SIGNOF(a) _Generic(a, unsigned char: 1, \
|
||||
unsigned short: 1, \
|
||||
unsigned int: 1, \
|
||||
unsigned long: 1, \
|
||||
unsigned long long: 1, \
|
||||
default: (long long)(a) < 0 ? -1 : 1)
|
||||
/**
|
||||
* @brief Calculates @p a/ @p b with arithmetic rounding
|
||||
*/
|
||||
#define DIV_ROUND(a, b) (((a) + SIGNOF(a) * (b) / 2) / (b))
|
||||
|
||||
/**
|
||||
* @brief Calculates @p a/ @p b, always rounding up to the
|
||||
* next whole number
|
||||
*/
|
||||
#define DIV_ROUND_UP(a, b) (((a) + SIGNOF(a) * ((b) - SIGNOF(b))) / (b))
|
||||
|
||||
/**
|
||||
* @brief Align @p num with the next multiple of @p chunk
|
||||
*/
|
||||
#define MATH_ALIGN(num, chunk) ((chunk) * DIV_ROUND_UP(num, chunk))
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MACROS_MATH_H */
|
||||
/** @} */
|
52
tests/unittests/tests-core/tests-core-macros.c
Normal file
52
tests/unittests/tests-core/tests-core-macros.c
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (C) 2022 ML!PA Consulting GmbH
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#include "embUnit.h"
|
||||
#include "tests-core.h"
|
||||
#include "macros/math.h"
|
||||
|
||||
static void test_math_signof(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_INT(1, SIGNOF(3));
|
||||
TEST_ASSERT_EQUAL_INT(-1, SIGNOF(-3));
|
||||
TEST_ASSERT_EQUAL_INT(-1, SIGNOF(INT32_MIN));
|
||||
TEST_ASSERT_EQUAL_INT(1, SIGNOF(UINT32_MAX));
|
||||
}
|
||||
|
||||
static void test_math_div_round(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_INT(3, DIV_ROUND(20, 7));
|
||||
TEST_ASSERT_EQUAL_INT(3, DIV_ROUND(21, 7));
|
||||
TEST_ASSERT_EQUAL_INT(3, DIV_ROUND(22, 7));
|
||||
TEST_ASSERT_EQUAL_INT(0, DIV_ROUND(1, UINT32_MAX));
|
||||
}
|
||||
|
||||
static void test_math_div_round_up(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_INT(3, DIV_ROUND_UP(20, 7));
|
||||
TEST_ASSERT_EQUAL_INT(3, DIV_ROUND_UP(21, 7));
|
||||
TEST_ASSERT_EQUAL_INT(4, DIV_ROUND_UP(22, 7));
|
||||
TEST_ASSERT_EQUAL_INT(1, DIV_ROUND_UP(1, UINT32_MAX));
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(1536, MATH_ALIGN(1514, 64));
|
||||
}
|
||||
|
||||
Test *tests_core_macros_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
new_TestFixture(test_math_signof),
|
||||
new_TestFixture(test_math_div_round),
|
||||
new_TestFixture(test_math_div_round_up),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(core_macros_tests, NULL, NULL, fixtures);
|
||||
|
||||
return (Test *)&core_macros_tests;
|
||||
}
|
@ -19,4 +19,5 @@ void tests_core(void)
|
||||
TESTS_RUN(tests_core_byteorder_tests());
|
||||
TESTS_RUN(tests_core_ringbuffer_tests());
|
||||
TESTS_RUN(tests_core_xfa_tests());
|
||||
TESTS_RUN(tests_core_macros_tests());
|
||||
}
|
||||
|
@ -92,6 +92,13 @@ Test *tests_core_ringbuffer_tests(void);
|
||||
*/
|
||||
Test *tests_core_xfa_tests(void);
|
||||
|
||||
/**
|
||||
* @brief Generates tests for macros
|
||||
*
|
||||
* @return embUnit tests if successful, NULL if not.
|
||||
*/
|
||||
Test *tests_core_macros_tests(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user