qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Radim Krčmář" <rkrcmar@redhat.com>
To: qemu-devel@nongnu.org
Cc: Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PATCH v2 4/4] vga: refactor vram_size clamping and rounding
Date: Tue, 17 Feb 2015 17:30:53 +0100	[thread overview]
Message-ID: <1424190653-29139-5-git-send-email-rkrcmar@redhat.com> (raw)
In-Reply-To: <1424190653-29139-1-git-send-email-rkrcmar@redhat.com>

Make the code a bit more obvious.

We don't have min/max, so a general helper for clamp probably isn't
acceptable either.

Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
---
 v2: kept rounding and clamping (was [v1 1/2])

 hw/display/vga.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/hw/display/vga.c b/hw/display/vga.c
index 13f34f950d5a..7e0c01dc57fa 100644
--- a/hw/display/vga.c
+++ b/hw/display/vga.c
@@ -2095,6 +2095,17 @@ static const GraphicHwOps vga_ops = {
     .text_update = vga_update_text,
 };
 
+static inline uint32_t uint_clamp(uint32_t val, uint32_t vmin, uint32_t vmax)
+{
+    if (val < vmin) {
+        return vmin;
+    }
+    if (val > vmax) {
+        return vmax;
+    }
+    return val;
+}
+
 void vga_common_init(VGACommonState *s, Object *obj, bool global_vmstate)
 {
     int i, j, v, b;
@@ -2122,13 +2133,10 @@ void vga_common_init(VGACommonState *s, Object *obj, bool global_vmstate)
         expand4to8[i] = v;
     }
 
-    /* valid range: 1 MB -> 512 MB */
-    s->vram_size = 1024 * 1024;
-    while (s->vram_size < (s->vram_size_mb << 20) &&
-           s->vram_size < (512 << 20)) {
-        s->vram_size <<= 1;
-    }
-    s->vram_size_mb = s->vram_size >> 20;
+    s->vram_size_mb = uint_clamp(s->vram_size_mb, 1, 512);
+    s->vram_size_mb = pow2ceil(s->vram_size_mb);
+    s->vram_size = s->vram_size_mb << 20;
+
     if (!s->vbe_size) {
         s->vbe_size = s->vram_size;
     }
-- 
2.3.0

      parent reply	other threads:[~2015-02-17 16:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-17 16:30 [Qemu-devel] [PATCH v2 0/4] spice: minor improvements Radim Krčmář
2015-02-17 16:30 ` [Qemu-devel] [PATCH v2 1/4] qxl: document minimal video memory for new modes Radim Krčmář
2015-02-17 16:30 ` [Qemu-devel] [PATCH v2 2/4] spice: fix invalid memory access to vga.vram Radim Krčmář
2015-02-17 16:30 ` [Qemu-devel] [PATCH v2 3/4] qxl: refactor rounding up to a nearest power of 2 Radim Krčmář
2015-02-17 16:30 ` Radim Krčmář [this message]

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=1424190653-29139-5-git-send-email-rkrcmar@redhat.com \
    --to=rkrcmar@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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 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).