[Mesa-dev] [PATCH] mesa: remove _mesa_ffs(), implement ffs() for non-GNU platforms

Brian Paul brianp at vmware.com
Wed Jan 11 12:50:42 PST 2012


Call ffs() and ffsll() everywhere.  Define our own ffs(), ffsll()
functions when the platform doesn't have them.
---
 src/mesa/drivers/dri/i965/brw_wm_emit.c  |   12 ++++++------
 src/mesa/drivers/dri/i965/brw_wm_fp.c    |    4 ++--
 src/mesa/drivers/dri/intel/intel_blit.c  |    2 +-
 src/mesa/drivers/dri/intel/intel_clear.c |    2 +-
 src/mesa/main/arrayobj.c                 |    2 +-
 src/mesa/main/bitset.h                   |    2 +-
 src/mesa/main/buffers.c                  |    4 ++--
 src/mesa/main/ff_fragment_shader.cpp     |    2 +-
 src/mesa/main/ffvertex_prog.c            |    2 +-
 src/mesa/main/imports.c                  |   11 ++++++-----
 src/mesa/main/imports.h                  |   21 ++++++++-------------
 src/mesa/main/shaderapi.c                |    2 +-
 src/mesa/program/prog_print.c            |    4 ++--
 src/mesa/swrast/s_texture.c              |    4 ++--
 14 files changed, 35 insertions(+), 39 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_wm_emit.c b/src/mesa/drivers/dri/i965/brw_wm_emit.c
index 270e321..2647a38 100644
--- a/src/mesa/drivers/dri/i965/brw_wm_emit.c
+++ b/src/mesa/drivers/dri/i965/brw_wm_emit.c
@@ -766,7 +766,7 @@ void emit_dp2(struct brw_compile *p,
 	      const struct brw_reg *arg0,
 	      const struct brw_reg *arg1)
 {
-   int dst_chan = _mesa_ffs(mask & WRITEMASK_XYZW) - 1;
+   int dst_chan = ffs(mask & WRITEMASK_XYZW) - 1;
 
    if (!(mask & WRITEMASK_XYZW))
       return; /* Do not emit dead code */
@@ -787,7 +787,7 @@ void emit_dp3(struct brw_compile *p,
 	      const struct brw_reg *arg0,
 	      const struct brw_reg *arg1)
 {
-   int dst_chan = _mesa_ffs(mask & WRITEMASK_XYZW) - 1;
+   int dst_chan = ffs(mask & WRITEMASK_XYZW) - 1;
 
    if (!(mask & WRITEMASK_XYZW))
       return; /* Do not emit dead code */
@@ -809,7 +809,7 @@ void emit_dp4(struct brw_compile *p,
 	      const struct brw_reg *arg0,
 	      const struct brw_reg *arg1)
 {
-   int dst_chan = _mesa_ffs(mask & WRITEMASK_XYZW) - 1;
+   int dst_chan = ffs(mask & WRITEMASK_XYZW) - 1;
 
    if (!(mask & WRITEMASK_XYZW))
       return; /* Do not emit dead code */
@@ -832,7 +832,7 @@ void emit_dph(struct brw_compile *p,
 	      const struct brw_reg *arg0,
 	      const struct brw_reg *arg1)
 {
-   const int dst_chan = _mesa_ffs(mask & WRITEMASK_XYZW) - 1;
+   const int dst_chan = ffs(mask & WRITEMASK_XYZW) - 1;
 
    if (!(mask & WRITEMASK_XYZW))
       return; /* Do not emit dead code */
@@ -882,7 +882,7 @@ void emit_math1(struct brw_wm_compile *c,
 {
    struct brw_compile *p = &c->func;
    struct intel_context *intel = &p->brw->intel;
-   int dst_chan = _mesa_ffs(mask & WRITEMASK_XYZW) - 1;
+   int dst_chan = ffs(mask & WRITEMASK_XYZW) - 1;
    GLuint saturate = ((mask & SATURATE) ?
 		      BRW_MATH_SATURATE_SATURATE :
 		      BRW_MATH_SATURATE_NONE);
@@ -945,7 +945,7 @@ void emit_math2(struct brw_wm_compile *c,
 {
    struct brw_compile *p = &c->func;
    struct intel_context *intel = &p->brw->intel;
-   int dst_chan = _mesa_ffs(mask & WRITEMASK_XYZW) - 1;
+   int dst_chan = ffs(mask & WRITEMASK_XYZW) - 1;
 
    if (!(mask & WRITEMASK_XYZW))
       return; /* Do not emit dead code */
diff --git a/src/mesa/drivers/dri/i965/brw_wm_fp.c b/src/mesa/drivers/dri/i965/brw_wm_fp.c
index 1358e74..b40c501 100644
--- a/src/mesa/drivers/dri/i965/brw_wm_fp.c
+++ b/src/mesa/drivers/dri/i965/brw_wm_fp.c
@@ -159,7 +159,7 @@ static struct prog_dst_register dst_undef( void )
 
 static struct prog_dst_register get_temp( struct brw_wm_compile *c )
 {
-   int bit = _mesa_ffs( ~c->fp_temp );
+   int bit = ffs( ~c->fp_temp );
 
    if (!bit) {
       printf("%s: out of temporaries\n", __FILE__);
@@ -260,7 +260,7 @@ static struct prog_instruction *emit_scalar_insn(struct brw_wm_compile *c,
    if (inst0->DstReg.WriteMask == 0)
       return NULL;
 
-   dst_chan = _mesa_ffs(inst0->DstReg.WriteMask) - 1;
+   dst_chan = ffs(inst0->DstReg.WriteMask) - 1;
    inst = get_fp_inst(c);
    *inst = *inst0;
    inst->DstReg.WriteMask = 1 << dst_chan;
diff --git a/src/mesa/drivers/dri/intel/intel_blit.c b/src/mesa/drivers/dri/intel/intel_blit.c
index 1369e63..e484fd3 100644
--- a/src/mesa/drivers/dri/intel/intel_blit.c
+++ b/src/mesa/drivers/dri/intel/intel_blit.c
@@ -261,7 +261,7 @@ intelClearWithBlit(struct gl_context *ctx, GLbitfield mask)
    /* Loop over all renderbuffers */
    mask &= (1 << BUFFER_COUNT) - 1;
    while (mask) {
-      GLuint buf = _mesa_ffs(mask) - 1;
+      GLuint buf = ffs(mask) - 1;
       bool is_depth_stencil = buf == BUFFER_DEPTH || buf == BUFFER_STENCIL;
       struct intel_renderbuffer *irb;
       int x1, y1, x2, y2;
diff --git a/src/mesa/drivers/dri/intel/intel_clear.c b/src/mesa/drivers/dri/intel/intel_clear.c
index e60fa66..dee4956 100644
--- a/src/mesa/drivers/dri/intel/intel_clear.c
+++ b/src/mesa/drivers/dri/intel/intel_clear.c
@@ -171,7 +171,7 @@ intelClear(struct gl_context *ctx, GLbitfield mask)
     * buffer with it.
     */
    if (mask & (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL)) {
-      int color_bit = _mesa_ffs(mask & BUFFER_BITS_COLOR);
+      int color_bit = ffs(mask & BUFFER_BITS_COLOR);
       if (color_bit != 0) {
 	 tri_mask |= blit_mask & (1 << (color_bit - 1));
 	 blit_mask &= ~(1 << (color_bit - 1));
diff --git a/src/mesa/main/arrayobj.c b/src/mesa/main/arrayobj.c
index 4b3e07b..29bfed8 100644
--- a/src/mesa/main/arrayobj.c
+++ b/src/mesa/main/arrayobj.c
@@ -303,7 +303,7 @@ _mesa_update_array_object_max_element(struct gl_context *ctx,
    GLuint min = ~0u;
 
    while (enabled) {
-      GLint attrib = _mesa_ffsll(enabled) - 1;
+      GLint attrib = ffsll(enabled) - 1;
       enabled &= ~BITFIELD64_BIT(attrib);
       min = update_min(min, &arrayObj->VertexAttrib[attrib]);
    }
diff --git a/src/mesa/main/bitset.h b/src/mesa/main/bitset.h
index c27b4c4..28b3c12 100644
--- a/src/mesa/main/bitset.h
+++ b/src/mesa/main/bitset.h
@@ -88,7 +88,7 @@ __bitset_ffs(const BITSET_WORD *x, int n)
 
    for (i = 0; i < n; i++) {
       if (x[i])
-	 return _mesa_ffs(x[i]) + BITSET_WORDBITS * i;
+	 return ffs(x[i]) + BITSET_WORDBITS * i;
    }
 
    return 0;
diff --git a/src/mesa/main/buffers.c b/src/mesa/main/buffers.c
index adea0f5..46c785f 100644
--- a/src/mesa/main/buffers.c
+++ b/src/mesa/main/buffers.c
@@ -402,7 +402,7 @@ _mesa_drawbuffers(struct gl_context *ctx, GLuint n, const GLenum *buffers,
    if (n == 1) {
       GLuint count = 0, destMask0 = destMask[0];
       while (destMask0) {
-         GLint bufIndex = _mesa_ffs(destMask0) - 1;
+         GLint bufIndex = ffs(destMask0) - 1;
          if (fb->_ColorDrawBufferIndexes[count] != bufIndex) {
             updated_drawbuffers(ctx);
             fb->_ColorDrawBufferIndexes[count] = bufIndex;
@@ -417,7 +417,7 @@ _mesa_drawbuffers(struct gl_context *ctx, GLuint n, const GLenum *buffers,
       GLuint count = 0;
       for (buf = 0; buf < n; buf++ ) {
          if (destMask[buf]) {
-            GLint bufIndex = _mesa_ffs(destMask[buf]) - 1;
+            GLint bufIndex = ffs(destMask[buf]) - 1;
             /* only one bit should be set in the destMask[buf] field */
             ASSERT(_mesa_bitcount(destMask[buf]) == 1);
             if (fb->_ColorDrawBufferIndexes[buf] != bufIndex) {
diff --git a/src/mesa/main/ff_fragment_shader.cpp b/src/mesa/main/ff_fragment_shader.cpp
index 3e736fa..5bfae7b 100644
--- a/src/mesa/main/ff_fragment_shader.cpp
+++ b/src/mesa/main/ff_fragment_shader.cpp
@@ -294,7 +294,7 @@ need_saturate( GLuint mode )
 static GLuint translate_tex_src_bit( GLbitfield bit )
 {
    ASSERT(bit);
-   return _mesa_ffs(bit) - 1;
+   return ffs(bit) - 1;
 }
 
 
diff --git a/src/mesa/main/ffvertex_prog.c b/src/mesa/main/ffvertex_prog.c
index 19d319a..f1ab753 100644
--- a/src/mesa/main/ffvertex_prog.c
+++ b/src/mesa/main/ffvertex_prog.c
@@ -378,7 +378,7 @@ static struct ureg swizzle1( struct ureg reg, int x )
 
 static struct ureg get_temp( struct tnl_program *p )
 {
-   int bit = _mesa_ffs( ~p->temp_in_use );
+   int bit = ffs( ~p->temp_in_use );
    if (!bit) {
       _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
       exit(1);
diff --git a/src/mesa/main/imports.c b/src/mesa/main/imports.c
index 2469e42..b3d56db 100644
--- a/src/mesa/main/imports.c
+++ b/src/mesa/main/imports.c
@@ -458,7 +458,7 @@ _mesa_inv_sqrtf(float n)
  * Find the first bit set in a word.
  */
 int
-_mesa_ffs(int32_t i)
+ffs(int i)
 {
 #if (defined(_WIN32) ) || defined(__IBMC__) || defined(__IBMCPP__)
    register int bit = 0;
@@ -495,23 +495,24 @@ _mesa_ffs(int32_t i)
  *          if no bits set.
  */
 int
-_mesa_ffsll(int64_t val)
+ffsll(long long int val)
 {
    int bit;
 
    assert(sizeof(val) == 8);
 
-   bit = _mesa_ffs((int32_t)val);
+   bit = ffs((int) val);
    if (bit != 0)
       return bit;
 
-   bit = _mesa_ffs((int32_t)(val >> 32));
+   bit = ffs((int) (val >> 32));
    if (bit != 0)
       return 32 + bit;
 
    return 0;
 }
-#endif
+#endif /* __GNUC__ */
+
 
 #if !defined(__GNUC__) ||\
    ((__GNUC__ * 100 + __GNUC_MINOR__) < 304) /* Not gcc 3.4 or later */
diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
index b7e8743..bcf125a 100644
--- a/src/mesa/main/imports.h
+++ b/src/mesa/main/imports.h
@@ -573,10 +573,15 @@ _mesa_init_sqrt_table(void);
 #define ffsll __builtin_ffsll
 #endif
 
-#define _mesa_ffs(i)  ffs(i)
-#define _mesa_ffsll(i)  ffsll(i)
+#else
+
+extern int ffs(int i);
+extern int ffsll(long long int i);
+
+#endif /*__ GNUC__ */
 
-#if ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
+
+#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
 #define _mesa_bitcount(i) __builtin_popcount(i)
 #define _mesa_bitcount_64(i) __builtin_popcountll(i)
 #else
@@ -586,16 +591,6 @@ extern unsigned int
 _mesa_bitcount_64(uint64_t n);
 #endif
 
-#else
-extern int
-_mesa_ffs(int32_t i);
-
-extern int
-_mesa_ffsll(int64_t i);
-
-extern unsigned int
-_mesa_bitcount(unsigned int n);
-#endif
 
 extern GLhalfARB
 _mesa_float_to_half(float f);
diff --git a/src/mesa/main/shaderapi.c b/src/mesa/main/shaderapi.c
index 9372d6d..754c660 100644
--- a/src/mesa/main/shaderapi.c
+++ b/src/mesa/main/shaderapi.c
@@ -935,7 +935,7 @@ validate_samplers(const struct gl_program *prog, char *errMsg)
    while (samplersUsed) {
       GLuint unit;
       gl_texture_index target;
-      GLint sampler = _mesa_ffs(samplersUsed) - 1;
+      GLint sampler = ffs(samplersUsed) - 1;
       assert(sampler >= 0);
       assert(sampler < Elements(prog->SamplerUnits));
       unit = prog->SamplerUnits[sampler];
diff --git a/src/mesa/program/prog_print.c b/src/mesa/program/prog_print.c
index 352a80f..46f1df0 100644
--- a/src/mesa/program/prog_print.c
+++ b/src/mesa/program/prog_print.c
@@ -194,7 +194,7 @@ _mesa_print_vp_inputs(GLbitfield inputs)
 {
    printf("VP Inputs 0x%x: \n", inputs);
    while (inputs) {
-      GLint attr = _mesa_ffs(inputs) - 1;
+      GLint attr = ffs(inputs) - 1;
       const char *name = arb_input_attrib_string(attr,
                                                  GL_VERTEX_PROGRAM_ARB);
       printf("  %d: %s\n", attr, name);
@@ -212,7 +212,7 @@ _mesa_print_fp_inputs(GLbitfield inputs)
 {
    printf("FP Inputs 0x%x: \n", inputs);
    while (inputs) {
-      GLint attr = _mesa_ffs(inputs) - 1;
+      GLint attr = ffs(inputs) - 1;
       const char *name = arb_input_attrib_string(attr,
                                                  GL_FRAGMENT_PROGRAM_ARB);
       printf("  %d: %s\n", attr, name);
diff --git a/src/mesa/swrast/s_texture.c b/src/mesa/swrast/s_texture.c
index ffd78a2..337a52f 100644
--- a/src/mesa/swrast/s_texture.c
+++ b/src/mesa/swrast/s_texture.c
@@ -285,7 +285,7 @@ _swrast_map_textures(struct gl_context *ctx)
 
    /* loop over enabled texture units */
    while (enabledUnits) {
-      GLuint unit = _mesa_ffs(enabledUnits) - 1;
+      GLuint unit = ffs(enabledUnits) - 1;
       struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
       
       _swrast_map_texture(ctx, texObj);
@@ -305,7 +305,7 @@ _swrast_unmap_textures(struct gl_context *ctx)
 
    /* loop over enabled texture units */
    while (enabledUnits) {
-      GLuint unit = _mesa_ffs(enabledUnits) - 1;
+      GLuint unit = ffs(enabledUnits) - 1;
       struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
       
       _swrast_unmap_texture(ctx, texObj);
-- 
1.7.3.4



More information about the mesa-dev mailing list