Skip to content

Commit 9504034

Browse files
cfriedtnashif
authored andcommitted
sys: util: use BITS_PER_BYTE macro instead of the magic number 8
Obviously, everyone knows that there are 8 bits per byte, so there isn't a lot of magic happening, per se, but it's also helpful to clearly denote where the magic number 8 is referring to the number of bits in a byte. Occasionally, 8 will refer to a field size or offset in a structure, MMR, or word. Occasionally, the number 8 will refer to the number of bytes in a 64-bit value (which should probably be replaced with `sizeof(uint64_t)`). For converting bits to bytes, or vice-versa, let's use `BITS_PER_BYTE` for clarity (or other appropriate `BITS_PER_*` macros). Signed-off-by: Chris Friedt <[email protected]>
1 parent 5032d8e commit 9504034

File tree

14 files changed

+25
-22
lines changed

14 files changed

+25
-22
lines changed

drivers/display/display_mcux_dcnano_lcdif.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ static const struct display_driver_api mcux_dcnano_lcdif_api = {
256256
.get_framebuffer = mcux_dcnano_lcdif_get_framebuffer,
257257
};
258258

259-
#define MCUX_DCNANO_LCDIF_PIXEL_BYTES(n) \
260-
(DISPLAY_BITS_PER_PIXEL(DT_INST_PROP(n, pixel_format)) / 8)
259+
#define MCUX_DCNANO_LCDIF_PIXEL_BYTES(n) \
260+
(DISPLAY_BITS_PER_PIXEL(DT_INST_PROP(n, pixel_format)) / BITS_PER_BYTE)
261261
#define MCUX_DCNANO_LCDIF_FB_SIZE(n) DT_INST_PROP(n, width) * \
262262
DT_INST_PROP(n, height) * MCUX_DCNANO_LCDIF_PIXEL_BYTES(n)
263263

drivers/display/display_mcux_elcdif.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static int mcux_elcdif_set_pixel_format(const struct device *dev,
247247
}
248248

249249
dev_data->pixel_format = pixel_format;
250-
dev_data->pixel_bytes = DISPLAY_BITS_PER_PIXEL(pixel_format) / 8;
250+
dev_data->pixel_bytes = DISPLAY_BITS_PER_PIXEL(pixel_format) / BITS_PER_BYTE;
251251
dev_data->fb_bytes =
252252
config->rgb_mode.panelWidth * config->rgb_mode.panelHeight * dev_data->pixel_bytes;
253253

drivers/display/display_renesas_lcdc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ LOG_MODULE_REGISTER(smartbond_display, CONFIG_DISPLAY_LOG_LEVEL);
4444
(((_val) << LCDC_LCDC_LAYER0_OFFSETX_REG_ ## _field ## _Pos) & \
4545
LCDC_LCDC_LAYER0_OFFSETX_REG_ ## _field ## _Msk)
4646

47-
#define DISPLAY_SMARTBOND_PIXEL_SIZE(inst) \
48-
(DISPLAY_BITS_PER_PIXEL(DT_INST_PROP(inst, pixel_format)) / 8)
47+
#define DISPLAY_SMARTBOND_PIXEL_SIZE(inst) \
48+
(DISPLAY_BITS_PER_PIXEL(DT_INST_PROP(inst, pixel_format)) / BITS_PER_BYTE)
4949

5050
#if CONFIG_DISPLAY_RENESAS_LCDC_BUFFER_PSRAM
5151
#define DISPLAY_BUFFER_LINKER_SECTION \

drivers/mipi_dbi/mipi_dbi_smartbond.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,8 @@ static int mipi_dbi_smartbond_write_display(const struct device *dev,
338338
lcdc_smartbond_mipi_dbi_cfg mipi_dbi_cfg;
339339
uint8_t layer_color = lcdc_smartbond_pixel_to_lcm(pixfmt);
340340

341-
if (desc->width * desc->height * (DISPLAY_BITS_PER_PIXEL(pixfmt) / 8) !=
342-
desc->buf_size) {
341+
if (desc->width * desc->height * (DISPLAY_BITS_PER_PIXEL(pixfmt) / BITS_PER_BYTE) !=
342+
desc->buf_size) {
343343
LOG_ERR("Incorrect buffer size for given width and height");
344344
return -EINVAL;
345345
}

include/zephyr/sys/atomic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ extern "C" {
7373
* @cond INTERNAL_HIDDEN
7474
*/
7575

76-
#define ATOMIC_BITS (sizeof(atomic_val_t) * 8)
76+
#define ATOMIC_BITS (sizeof(atomic_val_t) * BITS_PER_BYTE)
7777
#define ATOMIC_MASK(bit) BIT((unsigned long)(bit) & (ATOMIC_BITS - 1U))
7878
#define ATOMIC_ELEM(addr, bit) ((addr) + ((bit) / ATOMIC_BITS))
7979

include/zephyr/sys/rb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ struct rbnode {
6666
* packed binary tree, plus root... Works out to 59 entries for 32
6767
* bit pointers and 121 at 64 bits.
6868
*/
69-
#define Z_TBITS(t) ((sizeof(t)) < 8 ? 2 : 3)
70-
#define Z_PBITS(t) (8 * sizeof(t))
69+
#define Z_TBITS(t) ((sizeof(t)) < sizeof(uint64_t) ? 2 : 3)
70+
#define Z_PBITS(t) (BITS_PER_BYTE * sizeof(t))
7171
#define Z_MAX_RBTREE_DEPTH (2 * (Z_PBITS(int *) - Z_TBITS(int *) - 1) + 1)
7272

7373
/**

include/zephyr/sys/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <stdint.h>
3131

3232
/** @brief Number of bits that make up a type */
33-
#define NUM_BITS(t) (sizeof(t) * 8)
33+
#define NUM_BITS(t) (sizeof(t) * BITS_PER_BYTE)
3434

3535
#ifdef __cplusplus
3636
extern "C" {

kernel/userspace.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <zephyr/app_memory/app_memdomain.h>
2121
#include <zephyr/sys/libc-hooks.h>
2222
#include <zephyr/sys/mutex.h>
23+
#include <zephyr/sys/util.h>
2324
#include <inttypes.h>
2425
#include <zephyr/linker/linker-defs.h>
2526

@@ -71,7 +72,7 @@ static struct k_spinlock objfree_lock; /* k_object_free */
7172
#endif /* CONFIG_DYNAMIC_OBJECTS */
7273
static struct k_spinlock obj_lock; /* kobj struct data */
7374

74-
#define MAX_THREAD_BITS (CONFIG_MAX_THREAD_BYTES * 8)
75+
#define MAX_THREAD_BITS (CONFIG_MAX_THREAD_BYTES * BITS_PER_BYTE)
7576

7677
#ifdef CONFIG_DYNAMIC_OBJECTS
7778
extern uint8_t _thread_idx_map[CONFIG_MAX_THREAD_BYTES];

samples/subsys/input/draw_touch_events/src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <zephyr/device.h>
1010
#include <zephyr/input/input.h>
1111
#include <zephyr/drivers/display.h>
12+
#include <zephyr/sys/util.h>
1213

1314
LOG_MODULE_REGISTER(sample, LOG_LEVEL_INF);
1415

@@ -25,7 +26,7 @@ LOG_MODULE_REGISTER(sample, LOG_LEVEL_INF);
2526
#define CROSS_DIM (WIDTH / CONFIG_SCREEN_WIDTH_TO_CROSS_DIM)
2627

2728
#define PIXEL_FORMAT (DT_PROP_OR(DT_CHOSEN(zephyr_display), pixel_format, PIXEL_FORMAT_ARGB_8888))
28-
#define BPP ((DISPLAY_BITS_PER_PIXEL(PIXEL_FORMAT)) / 8)
29+
#define BPP ((DISPLAY_BITS_PER_PIXEL(PIXEL_FORMAT)) / BITS_PER_BYTE)
2930

3031
#define BUFFER_SIZE (CROSS_DIM * CROSS_DIM * BPP)
3132
#define REFRESH_RATE 100

subsys/bluetooth/host/gatt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ static bool cf_set_value(struct gatt_cf_cfg *cfg, const uint8_t *value, uint16_t
655655
/* Set the bits for each octet */
656656
for (i = 0U; i < len && i < CF_NUM_BYTES; i++) {
657657
if (i == (CF_NUM_BYTES - 1)) {
658-
cfg->data[i] |= value[i] & BIT_MASK(CF_NUM_BITS % 8);
658+
cfg->data[i] |= value[i] & BIT_MASK(CF_NUM_BITS % BITS_PER_BYTE);
659659
} else {
660660
cfg->data[i] |= value[i];
661661
}

0 commit comments

Comments
 (0)