On Thu, Jul 09, 2009 at 12:54:07PM +0200, David Jander wrote:
> On Thursday 09 July 2009 11:55:11 René Rebe wrote:
> > Peter Hutterer wrote:
> > > On Thu, Jul 09, 2009 at 11:01:54AM +0200, David Jander wrote:
> > >> On Thursday 09 July 2009 02:04:59 Peter Hutterer wrote:
> > >>> This patch adds the following three functions:
> > >>>  num_bytes_for_bits(bits) - the number of bytes needed to hold 'bits'
> > >>>  num_dwords_for_bytes(bytes) - the number of 4-byte units to hold
> > >>> 'bytes' pad_to_dwords(bytes) - the closest multiple of 4 equal to or
> > >>> larger than 'bytes'.
> > >>
> > >> Sorry to make this probably useless comment about naming, but while a
> > >> byte has a defined length (8 bits), the meaning of "word" and "dword" in
> > >> terms of length is undefined. By definition "word" in computing means
> > >> the natural unit of data used by a particular computer design. This
> > >> would be 32 bits on most 32-bit computers, and by consequence a "dword"
> > >> would be 64 bit wide. Your definition of the names "word" and "dword"
> > >> seem to be 16-bit platform-specific... not the most common platform for
> > >> Xorg!
> > >> Please, let's deprecate this flawed naming convention, and not use it in
> > >> new code... it's confusing and just plain wrong when used on
> > >> platform-independent code!
> > >
> > > This can be changed with a simple search+replace, I wouldn't mind
> > > changing it.
> > >
> > > Please suggest a better alternative naming though, the only appropriate
> > > equivalent I can think of is num_4byte_units_for_bytes and similar which
> > > does make the function names a tad long.
> >
> > ...int8..., ...int16..., ...int32..., ...
> 
> I agree. Although in theory 'int8' and 'byte' are of the same length (not 
> necessarily same type).
> 
> If you want to make the names _really_ short, you might consider the 
> convention used in the linux kernel and some other places: u8, s8, u16, s16, 
> u32.... etc, very short and still readable ;-)

New patch below, new names are bits_to_bytes, bits_to_int32, pad_to_int32. I
think these are short enough, not as clumsy as the num_a_from_b and int32 is
more precise than dword.

I don't think we need to worry about the size of a byte anymore...

I won't send all new patches since they are just search+replace, if needed
they are available on
  git://people.freedesktop.org/~whot/xserver.git bytecounting

Thanks for all the comments, I really didn't think of int32 and the like.

Cheers,
  Peter

>From 0042708b708b868a4eac91426beee3dd68820421 Mon Sep 17 00:00:00 2001
From: Peter Hutterer <[email protected]>
Date: Mon, 29 Jun 2009 13:09:57 +1000
Subject: [PATCH] include: introduce byte counting functions.

This patch adds the following three functions:
 bits_to_bytes(bits) - the number of bytes needed to hold 'bits'
 bytes_to_int32(bytes) - the number of 4-byte units to hold 'bytes'
 pad_to_int32(bytes) - the closest multiple of 4 equal to or larger than
                        'bytes'.

All three operations are common in protocol processing and currently the
server has ((foo + 7)/8 + 3)/4 operations all over the place. A common set
of functions reduce the error rate of these (albeit simple) calculations and
improve readability of the code.

The functions do not check for overflow.

Signed-off-by: Peter Hutterer <[email protected]>
---
 include/misc.h |   30 ++++++++++++++++++++++++++++++
 test/input.c   |   39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/include/misc.h b/include/misc.h
index 61dd947..877c682 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -180,6 +180,36 @@ typedef struct _xReq *xReqPtr;
 
 #endif
 
+/**
+ * Calculate the number of bytes needed to hold bits.
+ * @param bits The minimum number of bits needed.
+ * @return The number of bytes needed to hold bits.
+ */
+static inline int
+bits_to_bytes(const int bits) {
+    return ((bits + 7) >> 3);
+}
+/**
+ * Calculate the number of 4-byte units needed to hold the given number of
+ * bytes.
+ * @param bytes The minimum number of bytes needed.
+ * @return The number of 4-byte units needed to hold bytes.
+ */
+static inline int
+bytes_to_int32(const int bytes) {
+    return (((bytes) + 3) >> 2);
+}
+
+/**
+ * Calculate the number of bytes (in multiples of 4) needed to hold bytes.
+ * @param bytes The minimum number of bytes needed.
+ * @return The closest multiple of 4 that is equal or higher than bytes.
+ */
+static inline int
+pad_to_int32(const int bytes) {
+    return (((bytes) + 3) & ~3);
+}
+
 /* some macros to help swap requests, replies, and events */
 
 #define LengthRestB(stuff) \
diff --git a/test/input.c b/test/input.c
index bb32491..e2faaef 100644
--- a/test/input.c
+++ b/test/input.c
@@ -678,6 +678,44 @@ static void dix_grab_matching(void)
     g_assert(rc == TRUE);
 }
 
+static void include_byte_padding_macros(void)
+{
+    int i;
+    g_test_message("Testing bits_to_bytes()");
+
+    /* the macros don't provide overflow protection */
+    for (i = 0; i < INT_MAX - 7; i++)
+    {
+        int expected_bytes;
+        expected_bytes = (i + 7)/8;
+
+        g_assert(bits_to_bytes(i) >= i/8);
+        g_assert((bits_to_bytes(i) * 8) - i <= 7);
+    }
+
+    g_test_message("Testing bytes_to_int32()");
+    for (i = 0; i < INT_MAX - 3; i++)
+    {
+        int expected_4byte;
+        expected_4byte = (i + 3)/4;
+
+        g_assert(bytes_to_int32(i) <= i);
+        g_assert((bytes_to_int32(i) * 4) - i <= 3);
+    }
+
+    g_test_message("Testing pad_to_int32");
+
+    for (i = 0; i < INT_MAX - 3; i++)
+    {
+        int expected_bytes;
+        expected_bytes = ((i + 3)/4) * 4;
+
+        g_assert(pad_to_int32(i) >= i);
+        g_assert(pad_to_int32(i) - i <= 3);
+    }
+
+}
+
 int main(int argc, char** argv)
 {
     g_test_init(&argc, &argv,NULL);
@@ -688,6 +726,7 @@ int main(int argc, char** argv)
     g_test_add_func("/dix/input/check-grab-values", dix_check_grab_values);
     g_test_add_func("/dix/input/xi2-struct-sizes", xi2_struct_sizes);
     g_test_add_func("/dix/input/grab_matching", dix_grab_matching);
+    g_test_add_func("/include/byte_padding_macros", 
include_byte_padding_macros);
 
     return g_test_run();
 }
-- 
1.6.3.rc1.2.g0164.dirty

_______________________________________________
xorg-devel mailing list
[email protected]
http://lists.x.org/mailman/listinfo/xorg-devel

Reply via email to