All of lore.kernel.org
 help / color / mirror / Atom feed
From: Geert Uytterhoeven <geert@linux-m68k.org>
To: michalj@gmail.com, Rolf Eike Beer <eike-kernel@sf-tec.de>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org,
	Linux Fbdev development list <linux-fbdev@vger.kernel.org>
Subject: abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode search)
Date: Thu, 18 Nov 2010 07:40:19 +0100	[thread overview]
Message-ID: <AANLkTimYDAJr9x00sjGGnDF2vOVXU12qmbnmfuzA5_0h@mail.gmail.com> (raw)

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=UTF-8, Size: 6070 bytes --]

[Don't use the obsolete linux-fbdev-devel address]

On Thu, Nov 18, 2010 at 00:08, Michal Januszewski <michalj@gmail.com> wrote:
> In the framebuffer subsystem the abs() macro is often used as a part of
> the calculation of a Manhattan metric, which in turn is used as a measure
> of similarity between video modes.  The arguments of abs() are sometimes
> unsigned numbers.  This worked fine until commit a49c59c0, which changed
> the definition of abs() to prevent truncation.  As a result of this
> change, in the following piece of code:
>
>  u32 a = 0, b = 1;
>  u32 c = abs(a - b);

Indeed, the difference of 2 numbers is unsigned, as per C.

> 'c' will end up with a value of 0xffffffff instead of the expected 0x1.

This happens on 64-bit only, right?

Anyway, I think commit a49c59c0 is wrong: abs() operates on signed
(32-bit) numbers. For larger (64-bit signed) numbers, we have abs64().

> A problem caused by this change and visible by the end user is that
> framebuffer drivers relying on functions from modedb.c will fail to
> find high resolution video modes similar to that explicitly requested
> by the user if an exact match cannot be found (see e.g.
> https://bugs.gentoo.org/show_bug.cgi?id=296539).
>
> Fix this problem by casting all arguments of abs() to an int prior
> to the macro evaluation in modedb.c and uvesafb.c.
>
> Signed-off-by: Michal Januszewski <michalj@gmail.com>
> ---
> diff --git a/drivers/video/modedb.c b/drivers/video/modedb.c
> index 0a4dbdc..878bea1 100644
> --- a/drivers/video/modedb.c
> +++ b/drivers/video/modedb.c
> @@ -636,8 +636,10 @@ done:
>                        if (refresh_specified && db[i].refresh == refresh) {
>                                return 1;
>                        } else {
> -                               if (abs(db[i].refresh - refresh) < diff) {
> -                                       diff = abs(db[i].refresh - refresh);
> +                               if (abs((int)(db[i].refresh - refresh)) <
> +                                   diff) {
> +                                       diff = abs((int)(db[i].refresh -
> +                                               refresh));
>                                        best = i;
>                                }
>                        }
> @@ -654,8 +656,8 @@ done:
>        for (i = 0; i < dbsize; i++) {
>                DPRINTK("Trying %ix%i\n", db[i].xres, db[i].yres);
>                if (!fb_try_mode(var, info, &db[i], bpp)) {
> -                       tdiff = abs(db[i].xres - xres) +
> -                               abs(db[i].yres - yres);
> +                       tdiff = abs((int)(db[i].xres - xres)) +
> +                               abs((int)(db[i].yres - yres));
>
>                        /*
>                         * Penalize modes with resolutions smaller
> @@ -851,13 +853,13 @@ const struct fb_videomode *fb_find_nearest_mode(const struct fb_videomode *mode,
>                modelist = list_entry(pos, struct fb_modelist, list);
>                cmode = &modelist->mode;
>
> -               d = abs(cmode->xres - mode->xres) +
> -                       abs(cmode->yres - mode->yres);
> +               d = abs((int)(cmode->xres - mode->xres)) +
> +                       abs((int)(cmode->yres - mode->yres));
>                if (diff > d) {
>                        diff = d;
>                        best = cmode;
>                } else if (diff == d) {
> -                       d = abs(cmode->refresh - mode->refresh);
> +                       d = abs((int)(cmode->refresh - mode->refresh));
>                        if (diff_refresh > d) {
>                                diff_refresh = d;
>                                best = cmode;
> diff --git a/drivers/video/uvesafb.c b/drivers/video/uvesafb.c
> index 7b8839e..6621427 100644
> --- a/drivers/video/uvesafb.c
> +++ b/drivers/video/uvesafb.c
> @@ -320,9 +320,9 @@ static int uvesafb_vbe_find_mode(struct uvesafb_par *par,
>        int i, match = -1, h = 0, d = 0x7fffffff;
>
>        for (i = 0; i < par->vbe_modes_cnt; i++) {
> -               h = abs(par->vbe_modes[i].x_res - xres) +
> -                   abs(par->vbe_modes[i].y_res - yres) +
> -                   abs(depth - par->vbe_modes[i].depth);
> +               h = abs((int)(par->vbe_modes[i].x_res - xres)) +
> +                   abs((int)(par->vbe_modes[i].y_res - yres)) +
> +                   abs((int)(depth - par->vbe_modes[i].depth));
>
>                /*
>                 * We have an exact match in terms of resolution
> @@ -1375,7 +1375,7 @@ static int uvesafb_check_var(struct fb_var_screeninfo *var,
>         * which is theoretically incorrect, but which we'll try to handle
>         * here.
>         */
> -       if (depth == 0 || abs(depth - var->bits_per_pixel) >= 8)
> +       if (depth == 0 || abs((int)(depth - var->bits_per_pixel)) >= 8)
>                depth = var->bits_per_pixel;
>
>        match = uvesafb_vbe_find_mode(par, var->xres, var->yres, depth,

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
ÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ\x17¥Šwÿº{.nÇ+‰·¥Š{±þG«éÿŠ{ayº\x1dʇڙë,j\a­¢f£¢·hšïêÿ‘êçz_è®\x03(­éšŽŠÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?™¨è­Ú&£ø§~á¶iO•æ¬z·švØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?–I¥

WARNING: multiple messages have this Message-ID (diff)
From: Geert Uytterhoeven <geert@linux-m68k.org>
To: michalj@gmail.com, Rolf Eike Beer <eike-kernel@sf-tec.de>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org,
	Linux Fbdev development list <linux-fbdev@vger.kernel.org>
Subject: abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode search)
Date: Thu, 18 Nov 2010 06:40:19 +0000	[thread overview]
Message-ID: <AANLkTimYDAJr9x00sjGGnDF2vOVXU12qmbnmfuzA5_0h@mail.gmail.com> (raw)

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="windows-1253", Size: 6031 bytes --]

[Don't use the obsolete linux-fbdev-devel address]

On Thu, Nov 18, 2010 at 00:08, Michal Januszewski <michalj@gmail.com> wrote:
> In the framebuffer subsystem the abs() macro is often used as a part of
> the calculation of a Manhattan metric, which in turn is used as a measure
> of similarity between video modes.  The arguments of abs() are sometimes
> unsigned numbers.  This worked fine until commit a49c59c0, which changed
> the definition of abs() to prevent truncation.  As a result of this
> change, in the following piece of code:
>
>  u32 a = 0, b = 1;
>  u32 c = abs(a - b);

Indeed, the difference of 2 numbers is unsigned, as per C.

> 'c' will end up with a value of 0xffffffff instead of the expected 0x1.

This happens on 64-bit only, right?

Anyway, I think commit a49c59c0 is wrong: abs() operates on signed
(32-bit) numbers. For larger (64-bit signed) numbers, we have abs64().

> A problem caused by this change and visible by the end user is that
> framebuffer drivers relying on functions from modedb.c will fail to
> find high resolution video modes similar to that explicitly requested
> by the user if an exact match cannot be found (see e.g.
> https://bugs.gentoo.org/show_bug.cgi?id)6539).
>
> Fix this problem by casting all arguments of abs() to an int prior
> to the macro evaluation in modedb.c and uvesafb.c.
>
> Signed-off-by: Michal Januszewski <michalj@gmail.com>
> ---
> diff --git a/drivers/video/modedb.c b/drivers/video/modedb.c
> index 0a4dbdc..878bea1 100644
> --- a/drivers/video/modedb.c
> +++ b/drivers/video/modedb.c
> @@ -636,8 +636,10 @@ done:
>                        if (refresh_specified && db[i].refresh = refresh) {
>                                return 1;
>                        } else {
> -                               if (abs(db[i].refresh - refresh) < diff) {
> -                                       diff = abs(db[i].refresh - refresh);
> +                               if (abs((int)(db[i].refresh - refresh)) <
> +                                   diff) {
> +                                       diff = abs((int)(db[i].refresh -
> +                                               refresh));
>                                        best = i;
>                                }
>                        }
> @@ -654,8 +656,8 @@ done:
>        for (i = 0; i < dbsize; i++) {
>                DPRINTK("Trying %ix%i\n", db[i].xres, db[i].yres);
>                if (!fb_try_mode(var, info, &db[i], bpp)) {
> -                       tdiff = abs(db[i].xres - xres) +
> -                               abs(db[i].yres - yres);
> +                       tdiff = abs((int)(db[i].xres - xres)) +
> +                               abs((int)(db[i].yres - yres));
>
>                        /*
>                         * Penalize modes with resolutions smaller
> @@ -851,13 +853,13 @@ const struct fb_videomode *fb_find_nearest_mode(const struct fb_videomode *mode,
>                modelist = list_entry(pos, struct fb_modelist, list);
>                cmode = &modelist->mode;
>
> -               d = abs(cmode->xres - mode->xres) +
> -                       abs(cmode->yres - mode->yres);
> +               d = abs((int)(cmode->xres - mode->xres)) +
> +                       abs((int)(cmode->yres - mode->yres));
>                if (diff > d) {
>                        diff = d;
>                        best = cmode;
>                } else if (diff = d) {
> -                       d = abs(cmode->refresh - mode->refresh);
> +                       d = abs((int)(cmode->refresh - mode->refresh));
>                        if (diff_refresh > d) {
>                                diff_refresh = d;
>                                best = cmode;
> diff --git a/drivers/video/uvesafb.c b/drivers/video/uvesafb.c
> index 7b8839e..6621427 100644
> --- a/drivers/video/uvesafb.c
> +++ b/drivers/video/uvesafb.c
> @@ -320,9 +320,9 @@ static int uvesafb_vbe_find_mode(struct uvesafb_par *par,
>        int i, match = -1, h = 0, d = 0x7fffffff;
>
>        for (i = 0; i < par->vbe_modes_cnt; i++) {
> -               h = abs(par->vbe_modes[i].x_res - xres) +
> -                   abs(par->vbe_modes[i].y_res - yres) +
> -                   abs(depth - par->vbe_modes[i].depth);
> +               h = abs((int)(par->vbe_modes[i].x_res - xres)) +
> +                   abs((int)(par->vbe_modes[i].y_res - yres)) +
> +                   abs((int)(depth - par->vbe_modes[i].depth));
>
>                /*
>                 * We have an exact match in terms of resolution
> @@ -1375,7 +1375,7 @@ static int uvesafb_check_var(struct fb_var_screeninfo *var,
>         * which is theoretically incorrect, but which we'll try to handle
>         * here.
>         */
> -       if (depth = 0 || abs(depth - var->bits_per_pixel) >= 8)
> +       if (depth = 0 || abs((int)(depth - var->bits_per_pixel)) >= 8)
>                depth = var->bits_per_pixel;
>
>        match = uvesafb_vbe_find_mode(par, var->xres, var->yres, depth,

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
ÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ\x17¥Šwÿº{.nÇ+‰·¥Š{±ýöÝzÿâžØ^n‡r¡ö¦zË\x1aëh™¨è­Ú&£ûàz¿äz¹Þ—ú+€Ê+zf£¢·hšˆ§~†­†Ûiÿÿïêÿ‘êçz_è®\x0fæj:+v‰¨þ)ߣøm

             reply	other threads:[~2010-11-18  6:40 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-18  6:40 Geert Uytterhoeven [this message]
2010-11-18  6:40 ` abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode search) Geert Uytterhoeven
2010-11-19 22:07 ` Andrew Morton
2010-11-19 22:07   ` abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode Andrew Morton
2010-11-19 22:28   ` abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode search) Michal Januszewski
2010-11-19 22:28     ` abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode Michal Januszewski
2010-11-19 23:04     ` abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode search) Andrew Morton
2010-11-19 23:04       ` abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode Andrew Morton
2010-11-19 23:19       ` abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode search) Andrew Morton
2010-11-19 23:19         ` abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode Andrew Morton
2010-11-20  8:56         ` abs() vs. abs64() (was: Re: [PATCH] fbdev: fix nearest mode search) Geert Uytterhoeven
2010-11-20  8:56           ` Geert Uytterhoeven

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AANLkTimYDAJr9x00sjGGnDF2vOVXU12qmbnmfuzA5_0h@mail.gmail.com \
    --to=geert@linux-m68k.org \
    --cc=akpm@linux-foundation.org \
    --cc=eike-kernel@sf-tec.de \
    --cc=linux-fbdev@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michalj@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.