sys: util: Replace MIN(MAX(a, b), c) with CLAMP

Replaces all existing variants of value clamping with the MIN and MAX
macros with the CLAMP macro.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
This commit is contained in:
Trond Einar Snekvik 2020-10-27 12:27:25 +01:00 committed by Carles Cufí
commit 86c793af3f
16 changed files with 19 additions and 19 deletions

View file

@ -195,7 +195,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle)
}
ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks;
ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0);
ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS);
uint32_t unannounced = counter_sub(counter(), last_count);

View file

@ -574,7 +574,7 @@ static int i2c_sam0_set_apply_bitrate(const struct device *dev,
/* 2:1 low:high ratio */
baud_high = baud;
baud_high /= 3U;
baud_high = MAX(MIN(baud_high, 255U), 1U);
baud_high = CLAMP(baud_high, 1U, 255U);
baud_low = baud - baud_high;
if (baud_low < 1U && baud_high > 1U) {
--baud_high;
@ -605,7 +605,7 @@ static int i2c_sam0_set_apply_bitrate(const struct device *dev,
/* 2:1 low:high ratio */
baud_high = baud;
baud_high /= 3U;
baud_high = MAX(MIN(baud_high, 255U), 1U);
baud_high = CLAMP(baud_high, 1U, 255U);
baud_low = baud - baud_high;
if (baud_low < 1U && baud_high > 1U) {
--baud_high;

View file

@ -1654,7 +1654,7 @@ static int offload_connect(struct net_context *context,
* AT@SOCKCONN timeout param has minimum value of 30 seconds and
* maximum value of 360 seconds, otherwise an error is generated
*/
timeout_sec = MIN(360, MAX(timeout_sec, 30));
timeout_sec = CLAMP(timeout_sec, 30, 360);
snprintk(buf, sizeof(buf), "AT@SOCKCONN=%d,\"%s\",%d,%d",
sock->socket_id, wncm14a2a_sprint_ip_addr(addr),

View file

@ -94,7 +94,7 @@ static int spi_sam_configure(const struct device *dev,
/* Use the requested or next highest possible frequency */
div = SOC_ATMEL_SAM_MCK_FREQ_HZ / config->frequency;
div = MAX(1, MIN(UINT8_MAX, div));
div = CLAMP(div, 1, UINT8_MAX);
spi_csr |= SPI_CSR_SCBR(div);
regs->SPI_CR = SPI_CR_SPIDIS; /* Disable SPI */

View file

@ -119,7 +119,7 @@ static int spi_sam0_configure(const struct device *dev,
/* Use the requested or next highest possible frequency */
div = (SOC_ATMEL_SAM0_GCLK0_FREQ_HZ / config->frequency) / 2U - 1;
div = MAX(0, MIN(UINT8_MAX, div));
div = CLAMP(div, 0, UINT8_MAX);
/* Update the configuration only if it has changed */
if (regs->CTRLA.reg != ctrla.reg || regs->CTRLB.reg != ctrlb.reg ||

View file

@ -124,7 +124,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle)
#ifdef CONFIG_TICKLESS_KERNEL
ticks = ticks == K_TICKS_FOREVER ? MAX_TICKS : ticks;
ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0);
ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS);
k_spinlock_key_t key = k_spin_lock(&lock);
uint64_t curr = count();

View file

@ -208,7 +208,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle)
#ifdef CONFIG_TICKLESS_KERNEL
ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks;
ticks = MAX(MIN(ticks - 1, (int32_t) MAX_TICKS), 0);
ticks = CLAMP(ticks - 1, 0, (int32_t) MAX_TICKS);
k_spinlock_key_t key = k_spin_lock(&lock);

View file

@ -183,7 +183,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle)
uint32_t delay;
ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks;
ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0);
ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS);
k_spinlock_key_t key = k_spin_lock(&lock);

View file

@ -165,7 +165,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle)
}
ticks = ticks == K_TICKS_FOREVER ? max_ticks : ticks;
ticks = MAX(MIN(ticks - 1, (int32_t)max_ticks), 0);
ticks = CLAMP(ticks - 1, 0, (int32_t)max_ticks);
k_spinlock_key_t key = k_spin_lock(&lock);
uint32_t now = MAIN_COUNTER_REG, cyc, adj;

View file

@ -229,7 +229,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle)
}
ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks;
ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0);
ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS);
uint32_t unannounced = counter_sub(counter(), last_count);

View file

@ -106,7 +106,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle)
}
ticks = ticks == K_TICKS_FOREVER ? MAX_TICKS : ticks;
ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0);
ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS);
k_spinlock_key_t key = k_spin_lock(&lock);
uint64_t now = mtime();

View file

@ -259,7 +259,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle)
#ifdef CONFIG_TICKLESS_KERNEL
ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : ticks;
ticks = MAX(MIN(ticks - 1, (int32_t) MAX_TICKS), 0);
ticks = CLAMP(ticks - 1, 0, (int32_t) MAX_TICKS);
/* Compute number of RTC cycles until the next timeout. */
uint32_t count = rtc_count();

View file

@ -210,7 +210,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle)
* treated identically: it simply indicates the kernel would like the
* next tick announcement as soon as possible.
*/
ticks = MAX(MIN(ticks - 1, (int32_t)LPTIM_TIMEBASE), 1);
ticks = CLAMP(ticks - 1, 1, (int32_t)LPTIM_TIMEBASE);
k_spinlock_key_t key = k_spin_lock(&lock);

View file

@ -73,7 +73,7 @@ void z_clock_set_timeout(int32_t ticks, bool idle)
#if defined(CONFIG_TICKLESS_KERNEL)
ticks = ticks == K_TICKS_FOREVER ? MAX_TICKS : ticks;
ticks = MAX(MIN(ticks - 1, (int32_t)MAX_TICKS), 0);
ticks = CLAMP(ticks - 1, 0, (int32_t)MAX_TICKS);
k_spinlock_key_t key = k_spin_lock(&lock);
uint32_t curr = ccount(), cyc, adj;

View file

@ -74,7 +74,7 @@ static int32_t next_timeout(void)
struct _timeout *to = first();
int32_t ticks_elapsed = elapsed();
int32_t ret = to == NULL ? MAX_WAIT
: MIN(MAX_WAIT, MAX(0, to->dticks - ticks_elapsed));
: CLAMP(to->dticks - ticks_elapsed, 0, MAX_WAIT);
#ifdef CONFIG_TIMESLICING
if (_current_cpu->slice_ticks && _current_cpu->slice_ticks < ret) {

View file

@ -3235,9 +3235,9 @@ static inline void dle_max_time_get(const struct ll_conn *conn,
(!feature_coded_phy && !feature_phy_2m)) {
rx_time = PKT_US(LL_LENGTH_OCTETS_RX_MAX, PHY_1M);
#if defined(CONFIG_BT_CTLR_PHY)
tx_time = MAX(MIN(PKT_US(LL_LENGTH_OCTETS_RX_MAX, PHY_1M),
conn->default_tx_time),
PKT_US(PDU_DC_PAYLOAD_SIZE_MIN, PHY_1M));
tx_time = CLAMP(conn->default_tx_time,
PKT_US(PDU_DC_PAYLOAD_SIZE_MIN, PHY_1M),
PKT_US(LL_LENGTH_OCTETS_RX_MAX, PHY_1M));
#else /* !CONFIG_BT_CTLR_PHY */
tx_time = PKT_US(conn->default_tx_octets, PHY_1M);
#endif /* !CONFIG_BT_CTLR_PHY */