Skip to contentSkip to conversation

console: Correct computation of bytes per pixel from bits per pixel

PATCH Applied BALATON Zoltan 4 participants 9 messages Aug 22, 2012 – Aug 24, 2012

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 console: Correct computation of bytes per pixel from bits per pixel

Division with round up is the correct way to compute this even if the only case where division with round down gives incorrect result is probably 15 bpp. This case was explicitely patched up in one of these functions but was unhandled in the other. (I'm not sure about setting 16 bpp for the 15bpp case either but I left that there for now.)

---
 console.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

v2: Use DIV_ROUND_UP and extended commit message

console.c +2 -3
index 4525cc7..9df1701 100644
@@ -1612,7 +1612,7 @@ PixelFormat qemu_different_endianness_pixelformat(int bpp)
     memset(&pf, 0x00, sizeof(PixelFormat));
 
     pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
     pf.depth = bpp == 32 ? 24 : bpp;
 
     switch (bpp) {
@@ -1661,13 +1661,12 @@ PixelFormat qemu_default_pixelformat(int bpp)
     memset(&pf, 0x00, sizeof(PixelFormat));
 
     pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
     pf.depth = bpp == 32 ? 24 : bpp;
 
     switch (bpp) {
         case 15:
             pf.bits_per_pixel = 16;
-            pf.bytes_per_pixel = 2;
             pf.rmask = 0x00007c00;
             pf.gmask = 0x000003E0;
             pf.bmask = 0x0000001F;
Signature · 1 line
1.7.10
#1 lore raw
Quoted 29 lines · BALATON Zoltan

Division with round up is the correct way to compute this even if the only case where division with round down gives incorrect result is probably 15 bpp. This case was explicitely patched up in one of these functions but was unhandled in the other. (I'm not sure about setting 16 bpp for the 15bpp case either but I left that there for now.)

---
 console.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

v2: Use DIV_ROUND_UP and extended commit message

console.c +1 -1
index 4525cc7..9df1701 100644
@@ -1612,7 +1612,7 @@ PixelFormat
 qemu_different_endianness_pixelformat(int bpp)
     memset(&pf, 0x00, sizeof(PixelFormat));
 
     pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
     pf.depth = bpp == 32 ? 24 : bpp;
 
     switch (bpp) {
console.c +0 -0
@@ -1661,13 +1661,12 @@ PixelFormat qemu_default_pixelformat(int bpp)
     memset(&pf, 0x00, sizeof(PixelFormat));

console.c PixelFormat qemu_default_pixelformat(int bpp) · line 1670

Quoted 10 lines
console.c +1 -2
 
     pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
     pf.depth = bpp == 32 ? 24 : bpp;
 
     switch (bpp) {
         case 15:
             pf.bits_per_pixel = 16;
-            pf.bytes_per_pixel = 2;

Removed unintentionally?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#2 lore raw

Am 22.08.2012 17:32, schrieb Jan Kiszka:

Quoted 36 lines · BALATON Zoltan
Quoted 35 lines

Division with round up is the correct way to compute this even if the only case where division with round down gives incorrect result is probably 15 bpp. This case was explicitely patched up in one of these functions but was unhandled in the other. (I'm not sure about setting 16 bpp for the 15bpp case either but I left that there for now.)

---
  console.c |    5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)

v2: Use DIV_ROUND_UP and extended commit message

console.c +1 -1
index 4525cc7..9df1701 100644
@@ -1612,7 +1612,7 @@ PixelFormat
 qemu_different_endianness_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
      switch (bpp) {
console.c +1 -1
@@ -1661,13 +1661,12 @@ PixelFormat qemu_default_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
Quoted 7 lines

console.c PixelFormat qemu_default_pixelformat(int bpp) · line 1670

Quoted 4 lines
console.c +0 -1
      switch (bpp) {
          case 15:
              pf.bits_per_pixel = 16;
-            pf.bytes_per_pixel = 2;

Removed unintentionally?

Jan

15 bpp no longer needs special handling because
the computation above is fixed by the same patch,
so the removal is correct.

Stefan

#3 lore raw
Quoted 43 lines · Stefan Weil

Am 22.08.2012 17:32, schrieb Jan Kiszka:

Quoted 42 lines · BALATON Zoltan
Quoted 29 lines

Division with round up is the correct way to compute this even if the only case where division with round down gives incorrect result is probably 15 bpp. This case was explicitely patched up in one of these functions but was unhandled in the other. (I'm not sure about setting 16 bpp for the 15bpp case either but I left that there for now.)

---
  console.c |    5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)

v2: Use DIV_ROUND_UP and extended commit message

console.c +1 -1
index 4525cc7..9df1701 100644
@@ -1612,7 +1612,7 @@ PixelFormat
 qemu_different_endianness_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
      switch (bpp) {
console.c +0 -0
@@ -1661,13 +1661,12 @@ PixelFormat qemu_default_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));

console.c PixelFormat qemu_default_pixelformat(int bpp) · line 1670

Quoted 10 lines
console.c +1 -2
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
      switch (bpp) {
          case 15:
              pf.bits_per_pixel = 16;
-            pf.bytes_per_pixel = 2;

Removed unintentionally?

Quoted 5 lines
Quoted 1 line

Jan

15 bpp no longer needs special handling because
the computation above is fixed by the same patch,
so the removal is correct.

Ok, actually starting to look at this code: This is apparently a code cleanup, not a bug fix like the subject suggests. No valid input for could have actually triggered the round-up issue. Please declare it as such. Bonus for abort() on the invalid default cases.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#4 lore raw
Quoted 46 lines · Jan Kiszka
Quoted 45 lines · Stefan Weil

Am 22.08.2012 17:32, schrieb Jan Kiszka:

Quoted 43 lines · BALATON Zoltan
Quoted 29 lines

Division with round up is the correct way to compute this even if the only case where division with round down gives incorrect result is probably 15 bpp. This case was explicitely patched up in one of these functions but was unhandled in the other. (I'm not sure about setting 16 bpp for the 15bpp case either but I left that there for now.)

---
  console.c |    5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)

v2: Use DIV_ROUND_UP and extended commit message

console.c +1 -1
index 4525cc7..9df1701 100644
@@ -1612,7 +1612,7 @@ PixelFormat
 qemu_different_endianness_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
      switch (bpp) {
console.c +0 -0
@@ -1661,13 +1661,12 @@ PixelFormat qemu_default_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));

console.c PixelFormat qemu_default_pixelformat(int bpp) · line 1670

Quoted 10 lines
console.c +1 -2
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
      switch (bpp) {
          case 15:
              pf.bits_per_pixel = 16;
-            pf.bytes_per_pixel = 2;

Removed unintentionally?

Jan

Quoted 8 lines
Quoted 3 lines

15 bpp no longer needs special handling because
the computation above is fixed by the same patch,
so the removal is correct.

Ok, actually starting to look at this code: This is apparently a code cleanup, not a bug fix like the subject suggests. No valid input for could have actually triggered the round-up issue. Please declare it as such. Bonus for abort() on the invalid default cases.

Well, do not abort on bpp == 0, the curses case.

Also, qemu_different_endianness_pixelformat is currently only called with 16 or 32 bpp (see vga.c), but only handles 24 and 32. Fishy...

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#5 lore raw
In reply to Jan Kiszka
Quoted 46 lines · Jan Kiszka
Quoted 45 lines · Stefan Weil

Am 22.08.2012 17:32, schrieb Jan Kiszka:

Quoted 43 lines · BALATON Zoltan
Quoted 29 lines

Division with round up is the correct way to compute this even if the only case where division with round down gives incorrect result is probably 15 bpp. This case was explicitely patched up in one of these functions but was unhandled in the other. (I'm not sure about setting 16 bpp for the 15bpp case either but I left that there for now.)

---
  console.c |    5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)

v2: Use DIV_ROUND_UP and extended commit message

console.c +1 -1
index 4525cc7..9df1701 100644
@@ -1612,7 +1612,7 @@ PixelFormat
 qemu_different_endianness_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
      switch (bpp) {
console.c +0 -0
@@ -1661,13 +1661,12 @@ PixelFormat qemu_default_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));

console.c PixelFormat qemu_default_pixelformat(int bpp) · line 1670

Quoted 10 lines
console.c +1 -2
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
      switch (bpp) {
          case 15:
              pf.bits_per_pixel = 16;
-            pf.bytes_per_pixel = 2;

Removed unintentionally?

Jan

Quoted 8 lines
Quoted 3 lines

15 bpp no longer needs special handling because
the computation above is fixed by the same patch,
so the removal is correct.

Ok, actually starting to look at this code: This is apparently a code cleanup, not a bug fix like the subject suggests. No valid input for could have actually triggered the round-up issue. Please declare it as such. Bonus for abort() on the invalid default cases.

I have updated the commit message to "console: Clean up bytes per pixel calculation".

Stefan

#6 lore raw
Quoted 51 lines · Stefan Hajnoczi
Quoted 50 lines · Jan Kiszka
Quoted 48 lines · Stefan Weil

Am 22.08.2012 17:32, schrieb Jan Kiszka:

Quoted 36 lines · BALATON Zoltan
Quoted 35 lines

Division with round up is the correct way to compute this even if the only case where division with round down gives incorrect result is probably 15 bpp. This case was explicitely patched up in one of these functions but was unhandled in the other. (I'm not sure about setting 16 bpp for the 15bpp case either but I left that there for now.)

---
  console.c |    5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)

v2: Use DIV_ROUND_UP and extended commit message

console.c +1 -1
index 4525cc7..9df1701 100644
@@ -1612,7 +1612,7 @@ PixelFormat
 qemu_different_endianness_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
      switch (bpp) {
console.c +1 -1
@@ -1661,13 +1661,12 @@ PixelFormat qemu_default_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
Quoted 7 lines

console.c PixelFormat qemu_default_pixelformat(int bpp) · line 1670

Quoted 4 lines
console.c +0 -1
      switch (bpp) {
          case 15:
              pf.bits_per_pixel = 16;
-            pf.bytes_per_pixel = 2;

Removed unintentionally?

Jan

15 bpp no longer needs special handling because
the computation above is fixed by the same patch,
so the removal is correct.

Quoted 7 lines
Quoted 4 lines

Ok, actually starting to look at this code: This is apparently a code cleanup, not a bug fix like the subject suggests. No valid input for could have actually triggered the round-up issue. Please declare it as such. Bonus for abort() on the invalid default cases.

I have updated the commit message to "console: Clean up bytes per pixel calculation".

I think there was a v3 on the list, didn't check the details yet, though.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#7 lore raw
Quoted 57 lines · Jan Kiszka
Quoted 56 lines · Stefan Hajnoczi
Quoted 54 lines · Jan Kiszka
Quoted 43 lines · Stefan Weil

Am 22.08.2012 17:32, schrieb Jan Kiszka:

Quoted 42 lines · BALATON Zoltan
Quoted 29 lines

Division with round up is the correct way to compute this even if the only case where division with round down gives incorrect result is probably 15 bpp. This case was explicitely patched up in one of these functions but was unhandled in the other. (I'm not sure about setting 16 bpp for the 15bpp case either but I left that there for now.)

---
  console.c |    5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)

v2: Use DIV_ROUND_UP and extended commit message

console.c +1 -1
index 4525cc7..9df1701 100644
@@ -1612,7 +1612,7 @@ PixelFormat
 qemu_different_endianness_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
      switch (bpp) {
console.c +0 -0
@@ -1661,13 +1661,12 @@ PixelFormat qemu_default_pixelformat(int bpp)
      memset(&pf, 0x00, sizeof(PixelFormat));

console.c PixelFormat qemu_default_pixelformat(int bpp) · line 1670

Quoted 10 lines
console.c +1 -2
 
      pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
      pf.depth = bpp == 32 ? 24 : bpp;
 
      switch (bpp) {
          case 15:
              pf.bits_per_pixel = 16;
-            pf.bytes_per_pixel = 2;

Removed unintentionally?

Quoted 5 lines
Quoted 1 line

Jan

15 bpp no longer needs special handling because
the computation above is fixed by the same patch,
so the removal is correct.

Ok, actually starting to look at this code: This is apparently a code cleanup, not a bug fix like the subject suggests. No valid input for could have actually triggered the round-up issue. Please declare it as such. Bonus for abort() on the invalid default cases.

Quoted 4 lines
Quoted 2 lines

I have updated the commit message to "console: Clean up bytes per pixel calculation".

I think there was a v3 on the list, didn't check the details yet, though.

Yes but it just adds more stuff that should be in separate patches. Let's take v2 and additional changes can be done separately.

Stefan

#8 lore raw
In reply to BALATON Zoltan
Quoted 6 lines · BALATON Zoltan

Division with round up is the correct way to compute this even if the only case where division with round down gives incorrect result is probably 15 bpp. This case was explicitely patched up in one of these functions but was unhandled in the other. (I'm not sure about setting 16 bpp for the 15bpp case either but I left that there for now.)

Quoted 6 lines
---
 console.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

v2: Use DIV_ROUND_UP and extended commit message

Thanks, applied to the trivial patches tree:
https://github.com/stefanha/qemu/commits/trivial-patches

Stefan