treewide: Add and use ADD_MOD macro
Subsystems (12)
PATCH 1/1 treewide: Add and use ADD_MOD macro
Add a macro for the somewhat common use of
(something + value) % value
drivers/net/sundance.c includes a change from
a for(;test;add) loop to while (test) {... add};
because the test uses ADD_MOD
Compile tested only
--- include/linux/kernel.h | 1 + drivers/ata/libata-eh.c | 2 +- drivers/media/video/ov511.c | 2 +- drivers/media/video/usbvideo/usbvideo.c | 2 +- drivers/net/dl2k.c | 8 ++++---- drivers/net/sundance.c | 4 ++-- drivers/pci/dmar.c | 6 +++--- drivers/scsi/3w-9xxx.c | 2 +- drivers/staging/wavelan/wavelan_cs.c | 10 +++++----- drivers/usb/misc/sisusbvga/sisusb_con.c | 4 ++-- drivers/video/console/vgacon.c | 4 ++-- sound/oss/vwsnd.c | 6 +++--- 12 files changed, 26 insertions(+), 25 deletions(-)
include/linux/kernel.h +1 -0
@@ -63,6 +63,7 @@ extern const char linux_proc_banner[]; (((x) + ((__divisor) /2 )) / (__divisor)); \ } \ ) +#define ADD_MOD (x, y) (((x) + (y)) % (y))#define _RET_IP_ (unsigned long)__builtin_return_address(0)#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
drivers/ata/libata-eh.c +1 -1
@@ -416,7 +416,7 @@ static int ata_ering_map(struct ata_ering *ering, rc =map_fn (ent, arg);if (rc)break ; - idx = (idx -1 +ATA_ERING_SIZE ) %ATA_ERING_SIZE ; + idx =ADD_MOD (idx -1 ,ATA_ERING_SIZE ); }while (idx != ering->cursor);return rc;
drivers/media/video/ov511.c +1 -1
@@ -3751,7 +3751,7 @@ ov51x_new_frame(struct usb_ov511 *ov, int framenum)/* If we're not grabbing a frame right now and the other frame is */ /* ready to be grabbed into, then use it instead */ if (ov->curframe ==-1 ) { - newnum = (framenum -1 +OV511_NUMFRAMES ) %OV511_NUMFRAMES ; + newnum =ADD_MOD (framenum -1 ,OV511_NUMFRAMES );if (ov->frame[newnum].grabstate ==FRAME_READY ) framenum = newnum; }else
drivers/media/video/usbvideo/usbvideo.c +1 -1
@@ -1926,7 +1926,7 @@ static int usbvideo_NewFrame(struct uvd *uvd, int framenum)*/ if (!(uvd->flags &FLAGS_SEPARATE_FRAMES )) {/* This copies previous frame into this one to mask losses */ -int prev = (framenum -1 +USBVIDEO_NUMFRAMES ) %USBVIDEO_NUMFRAMES ; +int prev =ADD_MOD (framenum -1 ,USBVIDEO_NUMFRAMES );memmove (frame->data, uvd->frame[prev].data, uvd->max_frame_size); }else {if (uvd->flags &FLAGS_CLEAN_FRAMES ) {
drivers/net/dl2k.c +4 -4
@@ -652,8 +652,8 @@ start_xmit (struct sk_buff *skb, struct net_device *dev)/* Schedule ISR */ writel (10000 , ioaddr + CountDown); np->cur_tx = (np->cur_tx +1 ) %TX_RING_SIZE ; -if ((np->cur_tx - np->old_tx +TX_RING_SIZE ) %TX_RING_SIZE - <TX_QUEUE_LEN -1 && np->speed !=10 ) { +if (ADD_MOD (np->cur_tx - np->old_tx,TX_RING_SIZE ) <TX_QUEUE_LEN -1 && + np->speed !=10 ) {/* do nothing */ }else if (!netif_queue_stopped (dev)) {netif_stop_queue (dev);
@@ -758,8 +758,8 @@ rio_free_tx (struct net_device *dev, int irq)call netif_wake_queue() */ if (netif_queue_stopped (dev) && - ((np->cur_tx - np->old_tx +TX_RING_SIZE ) %TX_RING_SIZE - <TX_QUEUE_LEN -1 || np->speed ==10 )) { + (ADD_MOD (np->cur_tx - np->old_tx,TX_RING_SIZE ) < +TX_QUEUE_LEN -1 || np->speed ==10 )) {netif_wake_queue (dev); } }
drivers/net/sundance.c +2 -2
@@ -1390,8 +1390,7 @@ static void refill_rx (struct net_device *dev)int cnt =0 ;/* Refill the Rx ring buffers. */ -for (;(np->cur_rx - np->dirty_rx +RX_RING_SIZE ) %RX_RING_SIZE >0 ; - np->dirty_rx = (np->dirty_rx +1 ) %RX_RING_SIZE ) { +while (ADD_MOD (np->cur_rx - np->dirty_rx,RX_RING_SIZE ) >0 ) {struct sk_buff *skb; entry = np->dirty_rx %RX_RING_SIZE ;if (np->rx_skbuff[entry] ==NULL ) {
@@ -1410,6 +1409,7 @@ static void refill_rx (struct net_device *dev)cpu_to_le32 (np->rx_buf_sz | LastFrag); np->rx_ring[entry].status =0 ; cnt++; + np->dirty_rx = (np->dirty_rx +1 ) %RX_RING_SIZE ; }return ; }
drivers/pci/dmar.c +3 -3
@@ -889,17 +889,17 @@ static int qi_check_fault(struct intel_iommu *iommu, int index)*/ if (fault &DMA_FSTS_ITE ) { head =readl (iommu->reg +DMAR_IQH_REG ); - head = ((head >>DMAR_IQ_SHIFT ) -1 +QI_LENGTH ) %QI_LENGTH ; + head =ADD_MOD ((head >>DMAR_IQ_SHIFT ) -1 ,QI_LENGTH ); head |=1 ; tail =readl (iommu->reg +DMAR_IQT_REG ); - tail = ((tail >>DMAR_IQ_SHIFT ) -1 +QI_LENGTH ) %QI_LENGTH ; + tail =ADD_MOD ((tail >>DMAR_IQ_SHIFT ) -1 ,QI_LENGTH );writel (DMA_FSTS_ITE , iommu->reg +DMAR_FSTS_REG );do {if (qi->desc_status[head] ==QI_IN_USE ) qi->desc_status[head] =QI_ABORT ; - head = (head -2 +QI_LENGTH ) %QI_LENGTH ; + head =ADD_MOD (head -2 ,QI_LENGTH ); }while (head != tail);if (qi->desc_status[wait_index] ==QI_ABORT )
drivers/scsi/3w-9xxx.c +1 -1
@@ -755,7 +755,7 @@ static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int } tw_ioctl->driver_command.status =0 ; } - event_index = (tw_dev->error_index -1 +TW_Q_LENGTH ) %TW_Q_LENGTH ; + event_index =ADD_MOD (tw_dev->error_index -1 ,TW_Q_LENGTH );memcpy (tw_ioctl->data_buffer, tw_dev->event_queue[event_index],sizeof (TW_Event)); tw_dev->event_queue[event_index]->retrieved =TW_AEN_RETRIEVED ;break ;
drivers/staging/wavelan/wavelan_cs.c +5 -5
@@ -685,7 +685,7 @@ static void wl_update_history(wavepoint_history *wavepoint, unsigned char sigqua wavepoint->last_seq=seq; wavepoint->sigqual[wavepoint->qualptr++]=sigqual; wavepoint->qualptr %=WAVEPOINT_HISTORY ; - ptr=(wavepoint->qualptr-WAVEPOINT_FAST_HISTORY +WAVEPOINT_HISTORY )%WAVEPOINT_HISTORY ; + ptr=ADD_MOD (wavepoint->qualptr-WAVEPOINT_FAST_HISTORY ,WAVEPOINT_HISTORY );for (i=0 ;i<WAVEPOINT_FAST_HISTORY ;i++)/* Update running averages */ {
@@ -2728,7 +2728,7 @@ wv_start_of_frame(struct net_device * dev,int rp;int len; - rp = (rfp -5 +RX_SIZE ) %RX_SIZE ; + rp =ADD_MOD (rfp -5 ,RX_SIZE );outb (rp &0xff ,PIORL (base));outb (((rp >>8 ) &PIORH_MASK ),PIORH (base)); len =inb (PIOP (base));
@@ -2756,7 +2756,7 @@ wv_start_of_frame(struct net_device * dev, }/* Wrap around buffer */ -if (len > ((wrap - (rfp - len) +RX_SIZE ) %RX_SIZE ))/* magic formula ! */ +if (len >ADD_MOD (wrap - (rfp - len),RX_SIZE ))/* magic formula ! */ {#ifdef DEBUG_RX_ERROR printk(KERN_INFO "%s: wv_start_of_frame: wrap around buffer, wrap %d rfp %d len 0x%x\n",
@@ -2765,7 +2765,7 @@ wv_start_of_frame(struct net_device * dev,return (-1 ); } -return ((rp - len +RX_SIZE ) %RX_SIZE ); +return ADD_MOD (rp - len,RX_SIZE ); }/* wv_start_of_frame */ /*------------------------------------------------------------------*/
@@ -2956,7 +2956,7 @@ wv_packet_rcv(struct net_device * dev)* and rp to the beggining of the next one */ /* Read status & length of the frame */ - stat_ptr = (rp -7 +RX_SIZE ) %RX_SIZE ; + stat_ptr =ADD_MOD (rp -7 ,RX_SIZE ); stat_ptr =read_ringbuf (dev, stat_ptr, c,4 ); status = c[0 ] | (c[1 ] <<8 ); len = c[2 ] | (c[3 ] <<8 );
drivers/usb/misc/sisusbvga/sisusb_con.c +2 -2
@@ -749,10 +749,10 @@ sisusbcon_scrolldelta(struct vc_data *c, int lines) } - p = (c->vc_visible_origin - sisusb->scrbuf - ul + we) % we + + p =ADD_MOD (c->vc_visible_origin - sisusb->scrbuf - ul, we) + lines * c->vc_size_row; - st = (c->vc_origin - sisusb->scrbuf - ul + we) % we; + st =ADD_MOD (c->vc_origin - sisusb->scrbuf - ul, we);if (st <2 * margin) margin =0 ;
drivers/video/console/vgacon.c +2 -2
@@ -354,9 +354,9 @@ static int vgacon_scrolldelta(struct vc_data *c, int lines) ul =0 ; we = vga_vram_size; } - p = (c->vc_visible_origin - vga_vram_base - ul + we) % we + + p =ADD_MOD (c->vc_visible_origin - vga_vram_base - ul, we) + lines * c->vc_size_row; - st = (c->vc_origin - vga_vram_base - ul + we) % we; + st =ADD_MOD (c->vc_origin - vga_vram_base - ul, we);if (st <2 * margin) margin =0 ;if (p < margin)
sound/oss/vwsnd.c +3 -3
@@ -1970,7 +1970,7 @@ static void pcm_output(vwsnd_dev_t *devc, int erflown, int nb) hwptr =li_read_hwptr (&wport->chan); swptr =li_read_swptr (&wport->chan); - hw_unavail = (swptr - hwptr + hwsize) % hwsize; + hw_unavail =ADD_MOD (swptr - hwptr, hwsize); hw_avail = (hwmax - hw_unavail) & -fragsize; sw_avail = wport->swb_i_avail & -fragsize;if (sw_avail && swstate ==SW_RUN ) {
@@ -2093,7 +2093,7 @@ static void pcm_input(vwsnd_dev_t *devc, int erflown, int nb) hwptr =li_read_hwptr (&rport->chan); swptr =li_read_swptr (&rport->chan); - hw_avail = (hwptr - swptr + hwsize) % hwsize & -fragsize; + hw_avail =ADD_MOD (hwptr - swptr, hwsize) & -fragsize;if (hw_avail > hwmax) hw_avail = hwmax; sw_avail = rport->swb_i_avail & -fragsize;
@@ -2776,7 +2776,7 @@ static int vwsnd_audio_do_ioctl(struct inode *inode, hwframes = ustmsc.msc - wport->MSC_offset; totalhwbytes = hwframes * fsize; hwptr = totalhwbytes % hwsize; - hwbytes = (swptr - hwptr + hwsize) % hwsize; + hwbytes =ADD_MOD (swptr - hwptr, hwsize); ival += hwbytes / fsize; } }
Quoted 31 lines · On Sun, 28 Mar 2010 14:57:27 -0700 Joe Perches wrote:
Add a macro for the somewhat common use of
(something + value) % value
drivers/net/sundance.cincludes a change from
a for(;test;add) loop to while (test) {... add};
because the test usesADD_MODCompile tested only
Signed-off-by Joe Perches <[email protected]>--- include/linux/kernel.h | 1 + drivers/ata/libata-eh.c | 2 +- drivers/media/video/ov511.c | 2 +- drivers/media/video/usbvideo/usbvideo.c | 2 +- drivers/net/dl2k.c | 8 ++++---- drivers/net/sundance.c | 4 ++-- drivers/pci/dmar.c | 6 +++--- drivers/scsi/3w-9xxx.c | 2 +- drivers/staging/wavelan/wavelan_cs.c | 10 +++++----- drivers/usb/misc/sisusbvga/sisusb_con.c | 4 ++-- drivers/video/console/vgacon.c | 4 ++-- sound/oss/vwsnd.c | 6 +++--- 12 files changed, 26 insertions(+), 25 deletions(-)include/linux/kernel.h +0
-0
include/linux/kernel.h extern const char linux_proc_banner[]; · line 69
Quoted 7 lines
include/linux/kernel.h +1
-0@@ -63,6 +63,7 @@ extern const char linux_proc_banner[]; (((x) + ((__divisor) /2 )) / (__divisor)); \ } \ ) +#define ADD_MOD (x, y) (((x) + (y)) % (y))#define _RET_IP_ (unsigned long)__builtin_return_address(0)#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
It would be better not to evaluate y more than one time.
Also it's not safe for 64-bit 'y' on i386, right?
Looks like it would cause missing reference to __imoddi3 or whatever it is called.
so it can easily be misused IMO.
---
~Randy
Quoted 8 lines · Randy Dunlap
include/linux/kernel.h extern const char linux_proc_banner[]; · line 66
Quoted 6 lines · On Sun, 28 Mar 2010 14:57:27 -0700 Joe Perches wrote:
Add a macro for the somewhat common use of
(something + value) % value
because the test usesADD_MODinclude/linux/kernel.h +1
-0+#define ADD_MOD (x, y) (((x) + (y)) % (y))It would be better not to evaluate y more than one time.
That could be said for nearly all the convenience
macros in kernel.h
Quoted 3 lines
Also it's not safe for 64-bit 'y' on i386, right? Looks like it would cause missing reference to
__imoddi3or whatever it is called.
Which is also true for nearly all the convenience
macros in kernel.h
Quoted 1 line
so it can easily be misused IMO.
etc.
PATCH 1/1 kernel.h: Convert rounding macros to statement expressions, add ADD_MOD
Quoted 9 lines · Joe Perches
Quoted 8 lines · Randy Dunlap
include/linux/kernel.h
Quoted 6 lines · On Sun, 28 Mar 2010 14:57:27 -0700 Joe Perches wrote:
Add a macro for the somewhat common use of
(something + value) % value
because the test usesADD_MODinclude/linux/kernel.h +1
-0+#define ADD_MOD (x, y) (((x) + (y)) % (y))It would be better not to evaluate y more than one time.
Do you prefer something like this?
Convert rounding macros to statement expressions so arguments are only evaluated once.
Add kernel-doc to rounding macros
Add ADD_MOD statement expression for "(x + y) % y"
--- include/linux/kernel.h | 65 +++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 56 insertions(+), 9 deletions(-)
include/linux/kernel.h +56 -9
@@ -55,15 +55,62 @@ extern const char linux_proc_banner[];#define round_down (x, y) ((x) & ~__round_mask(x, y))#define FIELD_SIZEOF (t, f) (sizeof(((t*)0)->f)) -#define DIV_ROUND_UP (n,d) (((n) + (d) - 1) / (d)) -#define roundup (x, y) ((((x) + ((y) - 1)) / (y)) * (y)) -#define DIV_ROUND_CLOSEST (x, divisor)( \ -{ \ - typeof(divisor) __divisor = divisor; \ - (((x) + ((__divisor) / 2)) / (__divisor)); \ -} \ -) -#define ADD_MOD (x, y) (((x) + (y)) % (y)) + +/** +* roundup() - Returns x rounded up to the next whole multiple of y +* @x: dividend +* @y: divisor +* +* ie: roundup(4, 2) is 4, roundup(5, 2) is 6 +* c type conversion rules apply when x and y types differ +*/ +#define roundup (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + (_y - 1) / _y) * _y; \ +}) + +/** +* DIV_ROUND_UP() - Returns x/y rounded up to the next whole number +* @x: dividend +* @y: divisor +* +* ie: DIV_ROUND_UP(4, 2) is 2, DIV_ROUND_UP(5, 2) is 3 +* c type conversion rules apply when x and y types differ +*/ +#define DIV_ROUND_UP (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + _y - 1) / _y; \ +}) + +/** +* DIV_ROUND_CLOSEST() - Returns x/y rounded to the nearest whole number +* @x: dividend +* @y: divisor +* +* ie: DIV_ROUND_CLOSEST(4, 3) is 1, DIV_ROUND_CLOSEST(5, 3) is 2 +* c type conversion rules apply when x and y types differ +*/ +#define DIV_ROUND_CLOSEST (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + (_y / 2)) / _y; \ +}) + +/** +* ADD_MOD() - Returns (x + y) % y +* @x: initial value +* @y: value added to x then used as modulo +* +* ie: ADD_MOD(4, 2) is 0, ADD_MOD(5, 2) is 1 +* c type conversion rules apply when x and y types differ +*/ +#define ADD_MOD (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + _y) % _y; \ +})#define _RET_IP_ (unsigned long)__builtin_return_address(0)#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
Re: [PATCH] kernel.h: Convert rounding macros to statement expressions, add ADD_MOD
Quoted 1 line · On Mon, 29 Mar 2010 14:09:49 -0700 Joe Perches wrote:
Do you prefer something like this?
Yes, thanks.
Quoted 30 lines
Convert rounding macros to statement expressions so arguments are only evaluated once.
Add kernel-doc to rounding macros
AddADD_MODstatement expression for "(x + y) % y"Signed-off-by Joe Perches <[email protected]>--- include/linux/kernel.h | 65 +++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 56 insertions(+), 9 deletions(-)include/linux/kernel.h +3
-9@@ -55,15 +55,62 @@ extern const char linux_proc_banner[];#define round_down (x, y) ((x) & ~__round_mask(x, y))#define FIELD_SIZEOF (t, f) (sizeof(((t*)0)->f)) -#define DIV_ROUND_UP (n,d) (((n) + (d) - 1) / (d)) -#define roundup (x, y) ((((x) + ((y) - 1)) / (y)) * (y)) -#define DIV_ROUND_CLOSEST (x, divisor)( \ -{ \ - typeof(divisor) __divisor = divisor; \ - (((x) + ((__divisor) / 2)) / (__divisor)); \ -} \ -) -#define ADD_MOD (x, y) (((x) + (y)) % (y)) + +/** +* roundup() - Returns x rounded up to the next whole multiple of y
include/linux/kernel.h extern const char linux_proc_banner[]; · line 70
Quoted 10 lines
include/linux/kernel.h +10
-0+* @x: dividend +* @y: divisor +* +* ie: roundup(4, 2) is 4, roundup(5, 2) is 6 +* c type conversion rules apply when x and y types differ +*/ +#define roundup (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + (_y - 1) / _y) * _y; \
Above needs an extra set of parens:
(((x) + (_y - 1)) / _y) * _y; \
Quoted 36 lines
include/linux/kernel.h +36
-0+}) + +/** +* DIV_ROUND_UP() - Returns x/y rounded up to the next whole number +* @x: dividend +* @y: divisor +* +* ie: DIV_ROUND_UP(4, 2) is 2, DIV_ROUND_UP(5, 2) is 3 +* c type conversion rules apply when x and y types differ +*/ +#define DIV_ROUND_UP (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + _y - 1) / _y; \ +}) + +/** +* DIV_ROUND_CLOSEST() - Returns x/y rounded to the nearest whole number +* @x: dividend +* @y: divisor +* +* ie: DIV_ROUND_CLOSEST(4, 3) is 1, DIV_ROUND_CLOSEST(5, 3) is 2 +* c type conversion rules apply when x and y types differ +*/ +#define DIV_ROUND_CLOSEST (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + (_y / 2)) / _y; \ +}) + +/** +* ADD_MOD() - Returns (x + y) % y +* @x: initial value +* @y: value added to x then used as modulo +* +* ie: ADD_MOD(4, 2) is 0, ADD_MOD(5, 2) is 1
include/linux/kernel.h extern const char linux_proc_banner[]; · line 116
Quoted 10 lines
include/linux/kernel.h +7
-0+* c type conversion rules apply when x and y types differ +*/ +#define ADD_MOD (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + _y) % _y; \ +})#define _RET_IP_ (unsigned long)__builtin_return_address(0)#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
---
~Randy
PATCH v2 1/1 kernel.h: Convert rounding macros to statement expressions, add ADD_MOD
Quoted 8 lines · Randy Dunlap
Quoted 1 line
Do you prefer something like this?
Yes, thanks.
Review
Quoted 4 lines
(quoted diff) +4
-0+#define roundup(x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + (_y - 1) / _y) * _y; \Above needs an extra set of parens:
(((x) + (_y - 1)) / _y) * _y; \
Thanks. I think it's clearer matched to the DIV_ROUND_UP style:
(((x) + _y - 1) / _y) * _y;
----------------
Convert rounding macros to statement expressions so arguments are only evaluated once.
Add kernel-doc to rounding macros
Add ADD_MOD statement expression for "(x + y) % y"
--- include/linux/kernel.h | 66 +++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 57 insertions(+), 9 deletions(-)
include/linux/kernel.h +57 -9
@@ -55,15 +55,63 @@ extern const char linux_proc_banner[];#define round_down (x, y) ((x) & ~__round_mask(x, y))#define FIELD_SIZEOF (t, f) (sizeof(((t*)0)->f)) -#define DIV_ROUND_UP (n,d) (((n) + (d) - 1) / (d)) -#define roundup (x, y) ((((x) + ((y) - 1)) / (y)) * (y)) -#define DIV_ROUND_CLOSEST (x, divisor)( \ -{ \ - typeof(divisor) __divisor = divisor; \ - (((x) + ((__divisor) / 2)) / (__divisor)); \ -} \ -) -#define ADD_MOD (x, y) (((x) + (y)) % (y)) + +/** +* roundup() - Returns x rounded up to the next whole multiple of y +* @x: dividend +* @y: divisor +* +* ie: roundup(4, 2) is 4, roundup(5, 2) is 6 +* c type conversion rules apply when x and y types differ +*/ +#define roundup (x, y) \ +({ \ + typeof(y) _y = y; \ + (((x) + _y - 1) / _y) * _y; \ +}) + +/** +* DIV_ROUND_UP() - Returns x/y rounded up to the next whole number +* @x: dividend +* @y: divisor +* +* ie: DIV_ROUND_UP(4, 2) is 2, DIV_ROUND_UP(5, 2) is 3 +* c type conversion rules apply when x and y types differ +*/ +#define DIV_ROUND_UP (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + _y - 1) / _y; \ +}) + +/** +* DIV_ROUND_CLOSEST() - Returns x/y rounded to the nearest whole number +* @x: dividend +* @y: divisor +* +* ie: DIV_ROUND_CLOSEST(4, 3) is 1, DIV_ROUND_CLOSEST(5, 3) is 2 +* Rounds up when .5, ie: DIV_ROUND_CLOSEST(5, 2) is 3 +* c type conversion rules apply when x and y types differ +*/ +#define DIV_ROUND_CLOSEST (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + (_y / 2)) / _y; \ +}) + +/** +* ADD_MOD() - Returns (x + y) % y +* @x: initial value +* @y: value added to x then used as modulo +* +* ie: ADD_MOD(4, 2) is 0, ADD_MOD(5, 2) is 1 +* c type conversion rules apply when x and y types differ +*/ +#define ADD_MOD (x, y) \ +({ \ + typeof(y) _y = y; \ + ((x) + _y) % _y; \ +})#define _RET_IP_ (unsigned long)__builtin_return_address(0)#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
Re: [PATCH V2] kernel.h: Convert rounding macros to statement expressions, add ADD_MOD
Quoted 4 lines · On Mon, 29 Mar 2010 15:18:34 -0700 Joe Perches <[email protected]> wrote:
Convert rounding macros to statement expressions so arguments are only evaluated once.
Add kernel-doc to rounding macros
AddADD_MODstatement expression for "(x + y) % y"
In file included from /usr/src/devel/arch/x86/include/asm/cpumask.h:4,
from /usr/src/devel/arch/x86/include/asm/msr.h:18,
from /usr/src/devel/arch/x86/include/asm/processor.h:21,
from /usr/src/devel/arch/x86/include/asm/atomic.h:6,
from include/linux/crypto.h:20,
from arch/x86/kernel/asm-offsets_64.c:8,
from arch/x86/kernel/asm-offsets.c:4:include/linux/cpumask.h:13: error: braced-group within expression allowed only inside a function include/linux/cpumask.h:13: warning: no semicolon at end of struct or union include/linux/cpumask.h:13: error: syntax error before numeric constant include/linux/cpumask.h:13: warning: type defaults to 'int' in declaration of 'cpumask_t' include/linux/cpumask.h:13: warning: data definition has no type or storage class
...
Some sort of dependency tangle I guess.