[PATCH v4 3/4] Implement -e status=set option

Dmitry V. Levin ldv at altlinux.org
Mon Jun 24 23:20:31 UTC 2019


On Tue, Jun 18, 2019 at 03:33:19PM +0200, Paul Chaignon wrote:
> On Tue, Jun 18, 2019 at 02:50:27AM +0300, Dmitry V. Levin wrote:
> > On Sat, Jun 15, 2019 at 07:32:45PM +0200, Paul Chaignon wrote:
> > [...]
> > > diff --git a/number_set.c b/number_set.c
> > > index 4092ffda..878fad48 100644
> > > --- a/number_set.c
> > > +++ b/number_set.c
> > > @@ -47,6 +47,31 @@ reallocate_number_set(struct number_set *const set, const unsigned int new_nslot
> > >     set->nslots = new_nslots;
> > >  }
> > >
> > > +static unsigned int
> > > +popcount(const unsigned int x)
> > > +{
> > > +   unsigned int count = 0;
> > > +
> > > +#ifdef HAVE___BUILTIN_POPCOUNT
> > > +   count = __builtin_popcount(x);
> > > +#else
> > > +   for (; x; ++count)
> > > +           x &= x - 1;
> > > +#endif
> > > +   return count;
> > > +}
> >
> > We already have something similar in signal.c,
> > please avoid code duplication if possible.
> 
> Would util.c be a good place to move the popcount function common to
> signal and number_set?

Alternatively, you can move it into a header file (either defs.h
or create a new popcount.h file) to make it properly inlined.

> > > +static unsigned int
> > > +get_number_setbit(const struct number_set *const set)
> > > +{
> > > +   unsigned int i;
> > > +   unsigned int count = 0;
> > > +
> > > +   for (i = 0; i < set->nslots; ++i)
> > > +           count += popcount(set->vec[i]);
> > > +   return count;
> > > +}
> >
> > If we could assume sizeof(number_slot_t) == sizeof(uint32_t),
> > this would be equivalent to popcount32(set->vec, set->nslots).
> 
> Should I add a static_assert to ensure this equality remains valid?

Yes, I would add a static_assert.

> If I should, is it preferred to place the static_assert next to the code using
> it (get_number_setbit) or next to the typedef?

I would add it right into get_number_setbit.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20190625/1c80c2ce/attachment.bin>


More information about the Strace-devel mailing list