1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
19156: core/compiler_hints: add likely() / unlikely() hints r=kfessel a=benpicco



19174: tests/periph_spi clearly say when init succeeds r=benpicco a=jdavid

When the `tests/periph_spi` program succeeds the output can be interpreted as an error happened.
This PR makes it clearer when it does succeed.

### Contribution description

In `tests/periph_spi`:

- Explicitely say that the init operation was successful
- Rephrase the note to avoid misinterpretations 


### Testing procedure

Run the `tests/periph_spi` program.
There is not much to test, just to verify the output, should be like this:

```
2023-01-19 10:42:33,768 # Trying to initialize SPI_DEV(1): mode: 0, clk: 0, cs_port: 0, cs_pin: 0
2023-01-19 10:42:33,777 # (if below the program crashes with a failed assertion, then it means the configuration is not supported)
2023-01-19 10:42:33,779 # Success.
```


### Issues/PRs references

Issue https://github.com/RIOT-OS/RIOT/issues/19025

Co-authored-by: Benjamin Valentin <benjamin.valentin@ml-pa.com>
Co-authored-by: J. David Ibáñez <jdavid.ibp@gmail.com>
This commit is contained in:
bors[bot] 2023-01-19 15:38:47 +00:00 committed by GitHub
commit 71f783f1c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 4 deletions

View File

@ -22,6 +22,8 @@
#ifndef ASSERT_H
#define ASSERT_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -42,6 +44,9 @@ extern "C" {
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#define DEBUG_ASSERT_VERBOSE
#else
/* we should not include custom headers in standard headers */
#define _likely(x) __builtin_expect((uintptr_t)(x), 1)
#endif
/**
@ -109,10 +114,10 @@ __NORETURN void _assert_failure(const char *file, unsigned line);
*
* @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/assert.html
*/
#define assert(cond) ((cond) ? (void)0 : _assert_failure(__FILE__, __LINE__))
#define assert(cond) (_likely(cond) ? (void)0 : _assert_failure(__FILE__, __LINE__))
#else /* DEBUG_ASSERT_VERBOSE */
__NORETURN void _assert_panic(void);
#define assert(cond) ((cond) ? (void)0 : _assert_panic())
#define assert(cond) (_likely(cond) ? (void)0 : _assert_panic())
#endif /* DEBUG_ASSERT_VERBOSE */
#if !defined __cplusplus

View File

@ -21,6 +21,8 @@
#ifndef COMPILER_HINTS_H
#define COMPILER_HINTS_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -130,6 +132,24 @@ extern "C" {
#define IS_CT_CONSTANT(expr) 0
#endif
/**
* @brief Hint to the compiler that the condition @p x is likely taken
*
* @param[in] x Condition that is likely taken
*
* @return result of @p x
*/
#define likely(x) __builtin_expect((uintptr_t)(x), 1)
/**
* @brief Hint to the compiler that the condition @p x is likely not taken
*
* @param[in] x Condition that is unlikely
*
* @return result of @p x
*/
#define unlikely(x) __builtin_expect((uintptr_t)(x), 0)
#ifdef __cplusplus
}
#endif

View File

@ -26,6 +26,7 @@
#define TEST_UTILS_EXPECT_H
#include <stdio.h>
#include "compiler_hints.h"
#include "panic.h"
#ifdef __cplusplus
@ -76,7 +77,7 @@ NORETURN static inline void _expect_failure(const char *file, unsigned line)
* the condition failed in.
*
*/
#define expect(cond) ((cond) ? (void)0 : _expect_failure(__FILE__, __LINE__))
#define expect(cond) (likely(cond) ? (void)0 : _expect_failure(__FILE__, __LINE__))
#ifdef __cplusplus
}

View File

@ -217,9 +217,11 @@ int cmd_init(int argc, char **argv)
else {
printf("Trying to initialize SPI_DEV(%i): mode: %i, clk: %i, cs_port: %i, cs_pin: %i\n",
dev, mode, clk, port, pin);
puts("Note: Failed assertion (crash) means configuration not supported");
puts("(if below the program crashes with a failed assertion, then it means the"
" configuration is not supported)");
spi_acquire(spiconf.dev, spiconf.cs, spiconf.mode, spiconf.clk);
spi_release(spiconf.dev);
puts("Success.");
}
return 0;