<div dir="ltr">This is a nice improvement over the explicit cast, which is how I've always done it in the past - which is the ugly part of an otherwise great method for flags. Also I use & a lot with enum for clearing bits.</div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Aug 23, 2013 at 3:18 PM, Paul Berry <span dir="ltr"><<a href="mailto:stereotype441@gmail.com" target="_blank">stereotype441@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">(From a suggestion by Francisco Jerez)<br>
<br>
If an enum represents a bitfield of flags, e.g.:<br>
<br>
enum E {<br>
  A = 1,<br>
  B = 2,<br>
  C = 4,<br>
  D = 8,<br>
};<br>
<br>
then C++ normally prohibits statements like this:<br>
<br>
enum E x = A | B;<br>
<br>
because A and B are implicitly converted to ints before OR-ing them,<br>
and an int can't be stored in an enum without a type cast.  C, on the<br>
other hand, allows an int to be implicitly converted to an enum<br>
without casting.<br>
<br>
In the past we've dealt with this situation by storing flag bitfields<br>
as ints.  This avoids ugly casting at the expense of some type safety<br>
that C++ would normally have offered (e.g. we get no warning if we<br>
accidentally use the wrong enum type).<br>
<br>
However, we can get the best of both worlds if we override the |<br>
operator.  The ugly casting is confined to the operator overload, and<br>
we still get the benefit of C++ making sure we don't use the wrong<br>
enum type.<br>
<br>
The disadvantages are that (a) we need an explicit enum value for 0,<br>
and (b) we can't use related operators like |= unless we define<br>
additional overloads.<br>
<br>
So what do folks think?  Is it worth it?<br>
<br>
Cc: Francisco Jerez <<a href="mailto:currojerez@riseup.net">currojerez@riseup.net</a>><br>
---<br>
 src/mesa/drivers/dri/i965/brw_clip.h           |  2 +-<br>
 src/mesa/drivers/dri/i965/brw_clip_util.c      |  2 +-<br>
 src/mesa/drivers/dri/i965/brw_eu.h             | 16 +++++++++++++++-<br>
 src/mesa/drivers/dri/i965/brw_eu_emit.c        |  4 ++--<br>
 src/mesa/drivers/dri/i965/brw_sf_emit.c        | 12 ++++++++----<br>
 src/mesa/drivers/dri/i965/brw_vec4.h           |  2 +-<br>
 src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp |  3 ++-<br>
 7 files changed, 30 insertions(+), 11 deletions(-)<br>
<br>
diff --git a/src/mesa/drivers/dri/i965/brw_clip.h b/src/mesa/drivers/dri/i965/brw_clip.h<br>
index 5af0ad3..41f5c75 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_clip.h<br>
+++ b/src/mesa/drivers/dri/i965/brw_clip.h<br>
@@ -173,7 +173,7 @@ void brw_clip_init_planes( struct brw_clip_compile *c );<br>
<br>
 void brw_clip_emit_vue(struct brw_clip_compile *c,<br>
                       struct brw_indirect vert,<br>
-                       unsigned flags,<br>
+                       enum brw_urb_write_flags flags,<br>
                       GLuint header);<br>
<br>
 void brw_clip_kill_thread(struct brw_clip_compile *c);<br>
diff --git a/src/mesa/drivers/dri/i965/brw_clip_util.c b/src/mesa/drivers/dri/i965/brw_clip_util.c<br>
index d5c50d7..24d053e 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_clip_util.c<br>
+++ b/src/mesa/drivers/dri/i965/brw_clip_util.c<br>
@@ -313,7 +313,7 @@ void brw_clip_interp_vertex( struct brw_clip_compile *c,<br>
<br>
 void brw_clip_emit_vue(struct brw_clip_compile *c,<br>
                       struct brw_indirect vert,<br>
-                       unsigned flags,<br>
+                       enum brw_urb_write_flags flags,<br>
                       GLuint header)<br>
 {<br>
    struct brw_compile *p = &c->func;<br>
diff --git a/src/mesa/drivers/dri/i965/brw_eu.h b/src/mesa/drivers/dri/i965/brw_eu.h<br>
index 9053ea2..069b223 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_eu.h<br>
+++ b/src/mesa/drivers/dri/i965/brw_eu.h<br>
@@ -229,6 +229,8 @@ void brw_set_dp_write_message(struct brw_compile *p,<br>
                              GLuint send_commit_msg);<br>
<br>
 enum brw_urb_write_flags {<br>
+   BRW_URB_WRITE_NO_FLAGS = 0,<br>
+<br>
    /**<br>
     * Causes a new URB entry to be allocated, and its address stored in the<br>
     * destination register (gen < 7).<br>
@@ -271,11 +273,23 @@ enum brw_urb_write_flags {<br>
       BRW_URB_WRITE_ALLOCATE | BRW_URB_WRITE_COMPLETE,<br>
 };<br>
<br>
+#ifdef __cplusplus<br>
+/**<br>
+ * Allow brw_urb_write_flags enums to be ORed together (normally C++ wouldn't<br>
+ * allow this without a type cast).<br>
+ */<br>
+inline enum brw_urb_write_flags<br>
+operator|(enum brw_urb_write_flags x, enum brw_urb_write_flags y)<br>
+{<br>
+   return (enum brw_urb_write_flags) ((int) x | (int) y);<br>
+}<br>
+#endif<br>
+<br>
 void brw_urb_WRITE(struct brw_compile *p,<br>
                   struct brw_reg dest,<br>
                   GLuint msg_reg_nr,<br>
                   struct brw_reg src0,<br>
-                   unsigned flags,<br>
+                   enum brw_urb_write_flags flags,<br>
                   GLuint msg_length,<br>
                   GLuint response_length,<br>
                   GLuint offset,<br>
diff --git a/src/mesa/drivers/dri/i965/brw_eu_emit.c b/src/mesa/drivers/dri/i965/brw_eu_emit.c<br>
index b55b57e..ecf8597 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_eu_emit.c<br>
+++ b/src/mesa/drivers/dri/i965/brw_eu_emit.c<br>
@@ -515,7 +515,7 @@ static void brw_set_ff_sync_message(struct brw_compile *p,<br>
<br>
 static void brw_set_urb_message( struct brw_compile *p,<br>
                                 struct brw_instruction *insn,<br>
-                                 unsigned flags,<br>
+                                 enum brw_urb_write_flags flags,<br>
                                 GLuint msg_length,<br>
                                 GLuint response_length,<br>
                                 GLuint offset,<br>
@@ -2213,7 +2213,7 @@ void brw_urb_WRITE(struct brw_compile *p,<br>
                   struct brw_reg dest,<br>
                   GLuint msg_reg_nr,<br>
                   struct brw_reg src0,<br>
-                   unsigned flags,<br>
+                   enum brw_urb_write_flags flags,<br>
                   GLuint msg_length,<br>
                   GLuint response_length,<br>
                   GLuint offset,<br>
diff --git a/src/mesa/drivers/dri/i965/brw_sf_emit.c b/src/mesa/drivers/dri/i965/brw_sf_emit.c<br>
index d329bef..b206797 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_sf_emit.c<br>
+++ b/src/mesa/drivers/dri/i965/brw_sf_emit.c<br>
@@ -491,7 +491,8 @@ void brw_emit_tri_setup(struct brw_sf_compile *c, bool allocate)<br>
                       brw_null_reg(),<br>
                       0,<br>
                       brw_vec8_grf(0, 0), /* r0, will be copied to m0 */<br>
-                       last ? BRW_URB_WRITE_EOT_COMPLETE : 0,<br>
+                       last ? BRW_URB_WRITE_EOT_COMPLETE<br>
+                       : BRW_URB_WRITE_NO_FLAGS,<br>
                       4,       /* msg len */<br>
                       0,       /* response len */<br>
                       i*4,     /* offset */<br>
@@ -562,7 +563,8 @@ void brw_emit_line_setup(struct brw_sf_compile *c, bool allocate)<br>
                       brw_null_reg(),<br>
                       0,<br>
                       brw_vec8_grf(0, 0),<br>
-                       last ? BRW_URB_WRITE_EOT_COMPLETE : 0,<br>
+                       last ? BRW_URB_WRITE_EOT_COMPLETE<br>
+                       : BRW_URB_WRITE_NO_FLAGS,<br>
                       4,       /* msg len */<br>
                       0,       /* response len */<br>
                       i*4,     /* urb destination offset */<br>
@@ -649,7 +651,8 @@ void brw_emit_point_sprite_setup(struct brw_sf_compile *c, bool allocate)<br>
                    brw_null_reg(),<br>
                    0,<br>
                    brw_vec8_grf(0, 0),<br>
-                    last ? BRW_URB_WRITE_EOT_COMPLETE : 0,<br>
+                    last ? BRW_URB_WRITE_EOT_COMPLETE<br>
+                    : BRW_URB_WRITE_NO_FLAGS,<br>
                    4,  /* msg len */<br>
                    0,  /* response len */<br>
                    i*4,        /* urb destination offset */<br>
@@ -706,7 +709,8 @@ void brw_emit_point_setup(struct brw_sf_compile *c, bool allocate)<br>
                       brw_null_reg(),<br>
                       0,<br>
                       brw_vec8_grf(0, 0),<br>
-                       last ? BRW_URB_WRITE_EOT_COMPLETE : 0,<br>
+                       last ? BRW_URB_WRITE_EOT_COMPLETE<br>
+                       : BRW_URB_WRITE_NO_FLAGS,<br>
                       4,       /* msg len */<br>
                       0,       /* response len */<br>
                       i*4,     /* urb destination offset */<br>
diff --git a/src/mesa/drivers/dri/i965/brw_vec4.h b/src/mesa/drivers/dri/i965/brw_vec4.h<br>
index 5d8f0bf..0cb0347 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_vec4.h<br>
+++ b/src/mesa/drivers/dri/i965/brw_vec4.h<br>
@@ -222,7 +222,7 @@ public:<br>
    int target; /**< MRT target. */<br>
    bool shadow_compare;<br>
<br>
-   unsigned urb_write_flags;<br>
+   enum brw_urb_write_flags urb_write_flags;<br>
    bool header_present;<br>
    int mlen; /**< SEND message length */<br>
    int base_mrf; /**< First MRF in the SEND message, if mlen is nonzero. */<br>
diff --git a/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp b/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp<br>
index 72841e7..d0959b7 100644<br>
--- a/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp<br>
+++ b/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp<br>
@@ -2788,7 +2788,8 @@ vec4_vs_visitor::emit_urb_write_opcode(bool complete)<br>
    }<br>
<br>
    vec4_instruction *inst = emit(VS_OPCODE_URB_WRITE);<br>
-   inst->urb_write_flags = complete ? BRW_URB_WRITE_EOT_COMPLETE : 0;<br>
+   inst->urb_write_flags = complete ?<br>
+      BRW_URB_WRITE_EOT_COMPLETE : BRW_URB_WRITE_NO_FLAGS;<br>
<br>
    return inst;<br>
 }<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.8.3.4<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div>