linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC/PATCH] linux/kernel.h: Fix DIV_ROUND_CLOSEST to support negative divisors
@ 2016-12-22 10:22 Niklas Söderlund
  2016-12-22 14:48 ` Guenter Roeck
  0 siblings, 1 reply; 2+ messages in thread
From: Niklas Söderlund @ 2016-12-22 10:22 UTC (permalink / raw)
  To: Andrew Morton, Guenter Roeck, linux-kernel
  Cc: linux-renesas-soc, Niklas Söderlund

Add support to DIV_ROUND_CLOSEST for negative divisors if both dividend
and divisor variable types are signed. This should not alter current
behavior for users of the macro as previously negative divisors where
not supported.

Before:

DIV_ROUND_CLOSEST(  59,  4) =  15
DIV_ROUND_CLOSEST(  59, -4) = -14
DIV_ROUND_CLOSEST( -59,  4) = -15
DIV_ROUND_CLOSEST( -59, -4) =  14

After:

DIV_ROUND_CLOSEST(  59,  4) =  15
DIV_ROUND_CLOSEST(  59, -4) = -15
DIV_ROUND_CLOSEST( -59,  4) = -15
DIV_ROUND_CLOSEST( -59, -4) =  15

Signed-off-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
---

Hi,

While working on a thermal driver I encounter a scenario where the 
divisor could be negative, instead of adding local code to handle this I 
though I first try to add support for this in DIV_ROUND_CLOSEST.

Maybe there is a reason why this is not already supported? If so please 
let me know and I can do something locally in the driver and sorry for 
the noise.

 include/linux/kernel.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index bc6ed52a39b9..f1b2f7e1b2f0 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -95,16 +95,18 @@
 )
 
 /*
- * Divide positive or negative dividend by positive divisor and round
- * to closest integer. Result is undefined for negative divisors and
- * for negative dividends if the divisor variable type is unsigned.
+ * Divide positive or negative dividend by positive or negative divisor
+ * and round to closest integer. Result is undefined for negative
+ * divisors if dividends variable type is unsigned and for negative
+ * dividends if the divisor variable type is unsigned.
  */
 #define DIV_ROUND_CLOSEST(x, divisor)(			\
 {							\
 	typeof(x) __x = x;				\
 	typeof(divisor) __d = divisor;			\
 	(((typeof(x))-1) > 0 ||				\
-	 ((typeof(divisor))-1) > 0 || (__x) > 0) ?	\
+	 ((typeof(divisor))-1) > 0 ||			\
+	 (((__x) > 0) == ((__d) > 0))) ?		\
 		(((__x) + ((__d) / 2)) / (__d)) :	\
 		(((__x) - ((__d) / 2)) / (__d));	\
 }							\
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [RFC/PATCH] linux/kernel.h: Fix DIV_ROUND_CLOSEST to support negative divisors
  2016-12-22 10:22 [RFC/PATCH] linux/kernel.h: Fix DIV_ROUND_CLOSEST to support negative divisors Niklas Söderlund
@ 2016-12-22 14:48 ` Guenter Roeck
  0 siblings, 0 replies; 2+ messages in thread
From: Guenter Roeck @ 2016-12-22 14:48 UTC (permalink / raw)
  To: Niklas Söderlund, Andrew Morton, linux-kernel; +Cc: linux-renesas-soc

On 12/22/2016 02:22 AM, Niklas Söderlund wrote:
> Add support to DIV_ROUND_CLOSEST for negative divisors if both dividend
> and divisor variable types are signed. This should not alter current
> behavior for users of the macro as previously negative divisors where
> not supported.
>
> Before:
>
> DIV_ROUND_CLOSEST(  59,  4) =  15
> DIV_ROUND_CLOSEST(  59, -4) = -14
> DIV_ROUND_CLOSEST( -59,  4) = -15
> DIV_ROUND_CLOSEST( -59, -4) =  14
>
> After:
>
> DIV_ROUND_CLOSEST(  59,  4) =  15
> DIV_ROUND_CLOSEST(  59, -4) = -15
> DIV_ROUND_CLOSEST( -59,  4) = -15
> DIV_ROUND_CLOSEST( -59, -4) =  15
>
> Signed-off-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
> ---
>
> Hi,
>
> While working on a thermal driver I encounter a scenario where the
> divisor could be negative, instead of adding local code to handle this I
> though I first try to add support for this in DIV_ROUND_CLOSEST.
>
> Maybe there is a reason why this is not already supported? If so please
> let me know and I can do something locally in the driver and sorry for
> the noise.
>

Only reason is that I could not figure out a way to make it work.
LGTM except for nitpick.

Reviewed-by: Guenter Roeck <linux@roeck-us.net>

>  include/linux/kernel.h | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index bc6ed52a39b9..f1b2f7e1b2f0 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -95,16 +95,18 @@
>  )
>
>  /*
> - * Divide positive or negative dividend by positive divisor and round
> - * to closest integer. Result is undefined for negative divisors and
> - * for negative dividends if the divisor variable type is unsigned.
> + * Divide positive or negative dividend by positive or negative divisor
> + * and round to closest integer. Result is undefined for negative
> + * divisors if dividends variable type is unsigned and for negative

s/dividends/the dividend/

> + * dividends if the divisor variable type is unsigned.
>   */
>  #define DIV_ROUND_CLOSEST(x, divisor)(			\
>  {							\
>  	typeof(x) __x = x;				\
>  	typeof(divisor) __d = divisor;			\
>  	(((typeof(x))-1) > 0 ||				\
> -	 ((typeof(divisor))-1) > 0 || (__x) > 0) ?	\
> +	 ((typeof(divisor))-1) > 0 ||			\
> +	 (((__x) > 0) == ((__d) > 0))) ?		\
>  		(((__x) + ((__d) / 2)) / (__d)) :	\
>  		(((__x) - ((__d) / 2)) / (__d));	\
>  }							\
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-12-22 14:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-22 10:22 [RFC/PATCH] linux/kernel.h: Fix DIV_ROUND_CLOSEST to support negative divisors Niklas Söderlund
2016-12-22 14:48 ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).