Skip to content

Commit e93e47a

Browse files
committed
add tu_div_round_nearest() (only handle positive) to replace DIV_ROUND_CLOSEST() to remove the need of typeof
1 parent 79445c2 commit e93e47a

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed

src/class/cdc/cdc_host.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ static uint32_t ftdi_232bm_baud_base_to_divisor(uint32_t baud, uint32_t base) {
13651365
uint8_t divfrac[8] = {0, 3, 2, 4, 1, 5, 6, 7};
13661366
uint32_t divisor;
13671367
/* divisor shifted 3 bits to the left */
1368-
uint32_t divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud);
1368+
uint32_t divisor3 = tu_div_round_nearest(base, 2 * baud);
13691369
divisor = divisor3 >> 3;
13701370
divisor |= (uint32_t) divfrac[divisor3 & 0x7] << 14;
13711371
/* Deal with special cases for highest baud rates. */
@@ -1387,7 +1387,7 @@ static uint32_t ftdi_2232h_baud_base_to_divisor(uint32_t baud, uint32_t base) {
13871387
uint32_t divisor3;
13881388

13891389
/* hi-speed baud rate is 10-bit sampling instead of 16-bit */
1390-
divisor3 = DIV_ROUND_CLOSEST(8 * base, 10 * baud);
1390+
divisor3 = tu_div_round_nearest(8 * base, 10 * baud);
13911391

13921392
divisor = divisor3 >> 3;
13931393
divisor |= (uint32_t) divfrac[divisor3 & 0x7] << 14;

src/class/cdc/serial/ftdi_sio.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,4 @@ typedef struct ftdi_private {
215215
#define FTDI_NOT_POSSIBLE -1
216216
#define FTDI_REQUESTED -2
217217

218-
// division and round function overtaken from math.h
219-
#define DIV_ROUND_CLOSEST(x, divisor)( \
220-
{ \
221-
typeof(x) __x = x; \
222-
typeof(divisor) __d = divisor; \
223-
(((typeof(x))-1) > 0 || \
224-
((typeof(divisor))-1) > 0 || \
225-
(((__x) > 0) == ((__d) > 0))) ? \
226-
(((__x) + ((__d) / 2)) / (__d)) : \
227-
(((__x) - ((__d) / 2)) / (__d)); \
228-
} \
229-
)
230-
231218
#endif //TUSB_FTDI_SIO_H

src/common/tusb_common.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,31 @@
3434
//--------------------------------------------------------------------+
3535
// Macros Helper
3636
//--------------------------------------------------------------------+
37-
#define TU_ARRAY_SIZE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
37+
#define TU_ARRAY_SIZE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
3838
#define TU_FIELD_SIZE(_type, _field) (sizeof(((_type *)0)->_field))
39-
#define TU_MIN(_x, _y) ( ( (_x) < (_y) ) ? (_x) : (_y) )
40-
#define TU_MAX(_x, _y) ( ( (_x) > (_y) ) ? (_x) : (_y) )
41-
#define TU_DIV_CEIL(n, d) (((n) + (d) - 1) / (d))
39+
#define TU_MIN(_x, _y) ( ( (_x) < (_y) ) ? (_x) : (_y) )
40+
#define TU_MAX(_x, _y) ( ( (_x) > (_y) ) ? (_x) : (_y) )
41+
#define TU_DIV_CEIL(n, d) (((n) + (d) - 1) / (d))
42+
#define TU_DIV_ROUND_NEAREST(v, d) (((v) + (d)/2) / (d) ) // round to nearest integer
4243

43-
#define TU_U16(_high, _low) ((uint16_t) (((_high) << 8) | (_low)))
44-
#define TU_U16_HIGH(_u16) ((uint8_t) (((_u16) >> 8) & 0x00ff))
45-
#define TU_U16_LOW(_u16) ((uint8_t) ((_u16) & 0x00ff))
46-
#define U16_TO_U8S_BE(_u16) TU_U16_HIGH(_u16), TU_U16_LOW(_u16)
47-
#define U16_TO_U8S_LE(_u16) TU_U16_LOW(_u16), TU_U16_HIGH(_u16)
44+
#define TU_U16(_high, _low) ((uint16_t) (((_high) << 8) | (_low)))
45+
#define TU_U16_HIGH(_u16) ((uint8_t) (((_u16) >> 8) & 0x00ff))
46+
#define TU_U16_LOW(_u16) ((uint8_t) ((_u16) & 0x00ff))
47+
#define U16_TO_U8S_BE(_u16) TU_U16_HIGH(_u16), TU_U16_LOW(_u16)
48+
#define U16_TO_U8S_LE(_u16) TU_U16_LOW(_u16), TU_U16_HIGH(_u16)
4849

49-
#define TU_U32_BYTE3(_u32) ((uint8_t) ((((uint32_t) _u32) >> 24) & 0x000000ff)) // MSB
50-
#define TU_U32_BYTE2(_u32) ((uint8_t) ((((uint32_t) _u32) >> 16) & 0x000000ff))
51-
#define TU_U32_BYTE1(_u32) ((uint8_t) ((((uint32_t) _u32) >> 8) & 0x000000ff))
52-
#define TU_U32_BYTE0(_u32) ((uint8_t) (((uint32_t) _u32) & 0x000000ff)) // LSB
50+
#define TU_U32_BYTE3(_u32) ((uint8_t) ((((uint32_t) _u32) >> 24) & 0x000000ff)) // MSB
51+
#define TU_U32_BYTE2(_u32) ((uint8_t) ((((uint32_t) _u32) >> 16) & 0x000000ff))
52+
#define TU_U32_BYTE1(_u32) ((uint8_t) ((((uint32_t) _u32) >> 8) & 0x000000ff))
53+
#define TU_U32_BYTE0(_u32) ((uint8_t) (((uint32_t) _u32) & 0x000000ff)) // LSB
5354

54-
#define U32_TO_U8S_BE(_u32) TU_U32_BYTE3(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE0(_u32)
55-
#define U32_TO_U8S_LE(_u32) TU_U32_BYTE0(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE3(_u32)
55+
#define U32_TO_U8S_BE(_u32) TU_U32_BYTE3(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE0(_u32)
56+
#define U32_TO_U8S_LE(_u32) TU_U32_BYTE0(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE3(_u32)
5657

57-
#define TU_BIT(n) (1UL << (n))
58+
#define TU_BIT(n) (1UL << (n))
5859

5960
// Generate a mask with bit from high (31) to low (0) set, e.g TU_GENMASK(3, 0) = 0b1111
60-
#define TU_GENMASK(h, l) ( (UINT32_MAX << (l)) & (UINT32_MAX >> (31 - (h))) )
61+
#define TU_GENMASK(h, l) ( (UINT32_MAX << (l)) & (UINT32_MAX >> (31 - (h))) )
6162

6263
//--------------------------------------------------------------------+
6364
// Includes
@@ -215,6 +216,8 @@ TU_ATTR_ALWAYS_INLINE static inline bool tu_is_aligned64(uint64_t value) { retur
215216

216217
//------------- Mathematics -------------//
217218
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_div_ceil(uint32_t v, uint32_t d) { return TU_DIV_CEIL(v, d); }
219+
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_div_round_nearest(uint32_t v, uint32_t d) { return TU_DIV_ROUND_NEAREST(v, d); }
220+
218221
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_round_up(uint32_t v, uint32_t f) { return tu_div_ceil(v, f) * f; }
219222

220223
// log2 of a value is its MSB's position

0 commit comments

Comments
 (0)