Skip to content

Commit

Permalink
macro/align: Use ALIGN_UP and ALIGN_DOWN uniformly
Browse files Browse the repository at this point in the history
Signed-off-by: wangmingrong1 <wangmingrong1@xiaomi.com>
  • Loading branch information
W-M-R committed Jan 10, 2025
1 parent e5e9032 commit 3360cfc
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 54 deletions.
2 changes: 1 addition & 1 deletion arch/arm/src/imx9/imx9_lpi2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ static void imx9_lpi2c_setclock(struct imx9_lpi2c_priv_s *priv,
&src_freq);

/* LPI2C output frequency = (Source Clock (Hz)/ 2^prescale) /
* (CLKLO + 1 + CLKHI + 1 + ROUNDDOWN((2 + FILTSCL) / 2^prescale)
* (CLKLO + 1 + CLKHI + 1 + ALIGN_DOWN((2 + FILTSCL)/2^prescale)
*
* Assume CLKLO = 2 * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI / 2
*/
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/src/imxrt/imxrt_lpi2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ static void imxrt_lpi2c_setclock(struct imxrt_lpi2c_priv_s *priv,
#endif

/* LPI2C output frequency = (Source Clock (Hz)/ 2^prescale) /
* (CLKLO + 1 + CLKHI + 1 + ROUNDDOWN((2 + FILTSCL) / 2^prescale)
* (CLKLO + 1 + CLKHI + 1 + ALIGN_DOWN((2 + FILTSCL)/2^prescale)
*
* Assume CLKLO = 2 * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI / 2
*/
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/src/s32k1xx/s32k1xx_lpi2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ static void s32k1xx_lpi2c_setclock(struct s32k1xx_lpi2c_priv_s *priv,
DEBUGASSERT(src_freq != 0);

/* LPI2C output frequency = (Source Clock (Hz)/ 2^prescale) /
* (CLKLO + 1 + CLKHI + 1 + ROUNDDOWN((2 + FILTSCL) / 2^prescale)
* (CLKLO + 1 + CLKHI + 1 + ALIGN_DOWN((2 + FILTSCL)/2^prescale)
*
* Assume CLKLO = 2 * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI / 2
*/
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/src/s32k3xx/s32k3xx_lpi2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ static void s32k3xx_lpi2c_setclock(struct s32k3xx_lpi2c_priv_s *priv,
DEBUGASSERT(src_freq != 0);

/* LPI2C output frequency = (Source Clock (Hz)/ 2^prescale) /
* (CLKLO + 1 + CLKHI + 1 + ROUNDDOWN((2 + FILTSCL) / 2^prescale)
* (CLKLO + 1 + CLKHI + 1 + ALIGN_DOWN((2 + FILTSCL)/2^prescale)
*
* Assume CLKLO = 2 * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI / 2
*/
Expand Down
2 changes: 1 addition & 1 deletion arch/arm64/src/imx9/imx9_lpi2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ static void imx9_lpi2c_setclock(struct imx9_lpi2c_priv_s *priv,
imx9_get_rootclock(priv->config->clk_root, &src_freq);

/* LPI2C output frequency = (Source Clock (Hz)/ 2^prescale) /
* (CLKLO + 1 + CLKHI + 1 + ROUNDDOWN((2 + FILTSCL) / 2^prescale)
* (CLKLO + 1 + CLKHI + 1 + ALIGN_DOWN((2 + FILTSCL)/2^prescale)
*
* Assume CLKLO = 2 * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI / 2
*/
Expand Down
15 changes: 5 additions & 10 deletions binfmt/binfmt_copyactions.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,13 @@
#include <errno.h>

#include <nuttx/kmalloc.h>
#include <nuttx/nuttx.h>
#include <nuttx/binfmt/binfmt.h>

#include "binfmt.h"

#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) && !defined(CONFIG_BINFMT_DISABLE)

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define ROUNDUP(x, y) (((x) + (y) - 1) / (y) * (y))

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -98,8 +93,8 @@ int binfmt_copyactions(FAR const posix_spawn_file_actions_t **copy,

case SPAWN_FILE_ACTION_OPEN:
open = (FAR struct spawn_open_file_action_s *)entry;
size += ROUNDUP(SIZEOF_OPEN_FILE_ACTION_S(strlen(open->path)),
sizeof(FAR void *));
size += ALIGN_UP(SIZEOF_OPEN_FILE_ACTION_S(strlen(open->path)),
sizeof(FAR void *));
break;

default:
Expand Down Expand Up @@ -155,8 +150,8 @@ int binfmt_copyactions(FAR const posix_spawn_file_actions_t **copy,
strcpy(open->path, tmp->path);

buffer = (FAR char *)buffer +
ROUNDUP(SIZEOF_OPEN_FILE_ACTION_S(strlen(tmp->path)),
sizeof(FAR void *));
ALIGN_UP(SIZEOF_OPEN_FILE_ACTION_S(strlen(tmp->path)),
sizeof(FAR void *));
break;

default:
Expand Down
22 changes: 9 additions & 13 deletions libs/libbuiltin/libgcc/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <nuttx/init.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/nuttx.h>
#include <nuttx/spinlock.h>

/****************************************************************************
Expand Down Expand Up @@ -112,11 +113,6 @@

#define MAXARCS (1 << 20)

/* General rounding functions. */

#define ROUNDDOWN(x, y) (((x) / (y)) * (y))
#define ROUNDUP(x, y) ((((x) + (y) - 1) / (y)) * (y))

/* See profil(2) where this is described (incorrectly) */

#define SCALE_1_TO_1 0x10000
Expand Down Expand Up @@ -288,13 +284,13 @@ void moncontrol(int mode)

if (mode)
{
uintptr_t lowpc = ROUNDDOWN((uintptr_t)&_stext,
uintptr_t lowpc = ALIGN_DOWN((uintptr_t)&_stext,
HISTFRACTION * sizeof(HISTCOUNTER));
uintptr_t highpc = ROUNDUP((uintptr_t)&_etext,
HISTFRACTION * sizeof(HISTCOUNTER));
uintptr_t highpc = ALIGN_UP((uintptr_t)&_etext,
HISTFRACTION * sizeof(HISTCOUNTER));
size_t textsize = highpc - lowpc;
size_t kcountsize = ROUNDUP(textsize / HISTFRACTION,
sizeof(*p->kcount));
size_t kcountsize = ALIGN_UP(textsize / HISTFRACTION,
sizeof(*p->kcount));
int scale = kcountsize >= textsize ? SCALE_1_TO_1 :
(float)kcountsize / textsize * SCALE_1_TO_1;
FAR unsigned short *kcount = kmm_zalloc(kcountsize);
Expand Down Expand Up @@ -370,10 +366,10 @@ void monstartup(unsigned long lowpc, unsigned long highpc)
* so the rest of the scaling (here and in gprof) stays in ints.
*/

lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
lowpc = ALIGN_DOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
highpc = ALIGN_UP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
textsize = highpc - lowpc;
fromssize = ROUNDUP(textsize / HASHFRACTION, sizeof(*p->froms));
fromssize = ALIGN_UP(textsize / HASHFRACTION, sizeof(*p->froms));
tolimit = textsize * ARCDENSITY / 100;

if (tolimit < MINARCS)
Expand Down
2 changes: 0 additions & 2 deletions mm/iob/iob.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
* Pre-processor Definitions
****************************************************************************/

#define ROUNDUP(x, y) (((x) + (y) - 1) / (y) * (y))

#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_IOB_DEBUG)
# define ioberr _err
# define iobwarn _warn
Expand Down
3 changes: 2 additions & 1 deletion mm/iob/iob_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#ifdef CONFIG_IOB_ALLOC
# include <nuttx/kmalloc.h>
#endif
#include <nuttx/nuttx.h>
#include <nuttx/mm/iob.h>

#include "iob.h"
Expand Down Expand Up @@ -333,7 +334,7 @@ FAR struct iob_s *iob_alloc_dynamic(uint16_t size)
FAR struct iob_s *iob;
size_t alignsize;

alignsize = ROUNDUP(sizeof(struct iob_s), CONFIG_IOB_ALIGNMENT) + size;
alignsize = ALIGN_UP(sizeof(struct iob_s), CONFIG_IOB_ALIGNMENT) + size;

iob = kmm_memalign(CONFIG_IOB_ALIGNMENT, alignsize);
if (iob)
Expand Down
11 changes: 6 additions & 5 deletions mm/iob/iob_initialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <stdbool.h>

#include <nuttx/nuttx.h>
#include <nuttx/mm/iob.h>

#include "iob.h"
Expand All @@ -39,10 +40,10 @@
/* Fix the I/O Buffer size with specified alignment size */

#ifdef CONFIG_IOB_ALLOC
# define IOB_ALIGN_SIZE ROUNDUP(sizeof(struct iob_s) + CONFIG_IOB_BUFSIZE, \
CONFIG_IOB_ALIGNMENT)
# define IOB_ALIGN_SIZE ALIGN_UP(sizeof(struct iob_s) + CONFIG_IOB_BUFSIZE, \
CONFIG_IOB_ALIGNMENT)
#else
# define IOB_ALIGN_SIZE ROUNDUP(sizeof(struct iob_s), CONFIG_IOB_ALIGNMENT)
# define IOB_ALIGN_SIZE ALIGN_UP(sizeof(struct iob_s), CONFIG_IOB_ALIGNMENT)
#endif

#define IOB_BUFFER_SIZE (IOB_ALIGN_SIZE * CONFIG_IOB_NBUFFERS + \
Expand Down Expand Up @@ -137,8 +138,8 @@ void iob_initialize(void)
* aligned to the CONFIG_IOB_ALIGNMENT memory boundary
*/

buf = ROUNDUP((uintptr_t)g_iob_buffer + offsetof(struct iob_s, io_data),
CONFIG_IOB_ALIGNMENT) - offsetof(struct iob_s, io_data);
buf = ALIGN_UP((uintptr_t)g_iob_buffer + offsetof(struct iob_s, io_data),
CONFIG_IOB_ALIGNMENT) - offsetof(struct iob_s, io_data);

/* Get I/O buffer instance from the start address and add each I/O buffer
* to the free list
Expand Down
33 changes: 15 additions & 18 deletions sched/misc/coredump.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@

#define PROGRAM_ALIGNMENT 64

#define ROUNDUP(x, y) ((x + (y - 1)) / (y)) * (y)
#define ROUNDDOWN(x ,y) (((x) / (y)) * (y))

/****************************************************************************
* Private Types
****************************************************************************/
Expand Down Expand Up @@ -166,8 +163,8 @@ static int elf_emit(FAR struct elf_dumpinfo_s *cinfo,

static int elf_emit_align(FAR struct elf_dumpinfo_s *cinfo)
{
off_t align = ROUNDUP(cinfo->stream->nput,
ELF_PAGESIZE) - cinfo->stream->nput;
off_t align = ALIGN_UP(cinfo->stream->nput,
ELF_PAGESIZE) - cinfo->stream->nput;
unsigned char null[256];
off_t total = align;
off_t ret = 0;
Expand Down Expand Up @@ -258,10 +255,10 @@ static int elf_get_note_size(int stksegs)
{
int total;

total = stksegs * (sizeof(Elf_Nhdr) + ROUNDUP(CONFIG_TASK_NAME_SIZE, 8) +
sizeof(elf_prstatus_t));
total += stksegs * (sizeof(Elf_Nhdr) + ROUNDUP(CONFIG_TASK_NAME_SIZE, 8) +
sizeof(elf_prpsinfo_t));
total = stksegs * (sizeof(Elf_Nhdr) + ALIGN_UP(CONFIG_TASK_NAME_SIZE, 8) +
sizeof(elf_prstatus_t));
total += stksegs * (sizeof(Elf_Nhdr) + ALIGN_UP(CONFIG_TASK_NAME_SIZE, 8) +
sizeof(elf_prpsinfo_t));
return total;
}

Expand Down Expand Up @@ -414,8 +411,8 @@ static void elf_emit_tcb_stack(FAR struct elf_dumpinfo_s *cinfo,
(tcb->stack_base_ptr - tcb->stack_alloc_ptr);
}

sp = ROUNDDOWN(buf, PROGRAM_ALIGNMENT);
len = ROUNDUP(len + (buf - sp), PROGRAM_ALIGNMENT);
sp = ALIGN_DOWN(buf, PROGRAM_ALIGNMENT);
len = ALIGN_UP(len + (buf - sp), PROGRAM_ALIGNMENT);
buf = sp;

elf_emit(cinfo, (FAR void *)buf, len);
Expand Down Expand Up @@ -546,17 +543,17 @@ static void elf_emit_tcb_phdr(FAR struct elf_dumpinfo_s *cinfo,
(tcb->stack_base_ptr - tcb->stack_alloc_ptr);
}

sp = ROUNDDOWN(phdr->p_vaddr, PROGRAM_ALIGNMENT);
phdr->p_filesz = ROUNDUP(phdr->p_filesz +
(phdr->p_vaddr - sp), PROGRAM_ALIGNMENT);
sp = ALIGN_DOWN(phdr->p_vaddr, PROGRAM_ALIGNMENT);
phdr->p_filesz = ALIGN_UP(phdr->p_filesz +
(phdr->p_vaddr - sp), PROGRAM_ALIGNMENT);
phdr->p_vaddr = sp;

phdr->p_type = PT_LOAD;
phdr->p_offset = ROUNDUP(*offset, ELF_PAGESIZE);
phdr->p_offset = ALIGN_UP(*offset, ELF_PAGESIZE);
phdr->p_paddr = phdr->p_vaddr;
phdr->p_memsz = phdr->p_filesz;
phdr->p_flags = PF_X | PF_W | PF_R;
*offset += ROUNDUP(phdr->p_memsz, ELF_PAGESIZE);
*offset += ALIGN_UP(phdr->p_memsz, ELF_PAGESIZE);

elf_emit(cinfo, phdr, sizeof(*phdr));
}
Expand Down Expand Up @@ -608,13 +605,13 @@ static void elf_emit_phdr(FAR struct elf_dumpinfo_s *cinfo,
for (i = 0; i < memsegs; i++)
{
phdr.p_type = PT_LOAD;
phdr.p_offset = ROUNDUP(offset, ELF_PAGESIZE);
phdr.p_offset = ALIGN_UP(offset, ELF_PAGESIZE);
phdr.p_vaddr = cinfo->regions[i].start;
phdr.p_paddr = phdr.p_vaddr;
phdr.p_filesz = cinfo->regions[i].end - cinfo->regions[i].start;
phdr.p_memsz = phdr.p_filesz;
phdr.p_flags = cinfo->regions[i].flags;
offset += ROUNDUP(phdr.p_memsz, ELF_PAGESIZE);
offset += ALIGN_UP(phdr.p_memsz, ELF_PAGESIZE);
elf_emit(cinfo, &phdr, sizeof(phdr));
}
}
Expand Down

0 comments on commit 3360cfc

Please sign in to comment.