What have I missed?
Mehrdad via Digitalmars-d
digitalmars-d at puremagic.com
Sat Aug 9 02:34:37 PDT 2014
On Friday, 8 August 2014 at 22:43:38 UTC, Andrei Alexandrescu
wrote:
> On 8/7/14, 12:40 PM, Era Scarecrow wrote:
>> As for being able to find x number of bits that are 0 or 1 in
>> a row, that both sounds easy and hard at the same time (easy
>> if you don't mind it being slow). In my rewrite there was a
>> bulk template I created that was intended to do huge speedups
>> with binary data (when the data was naturally aligned
>> correctly and large enough to use the binary operators on
>> normal types). Some of the unittests and example code also
>> used them in a read-only fashion that could be specialized for
>> finding a certain type of pattern...
>
> A thought: if whatever work on bit arrays you do isn't fast,
> it's probably not worth doing; people who opt for that kind of
> packing are most often performance motivated.
>
> Alignment is often not an issue - you handle the setup/teardown
> misalignments separately and to the bulk 64 bits at a time.
What kind of performance are you looking for? I have some very
basic bit-manipulation code written in C++ that operates on whole
words at a time, not sure if it's what you need but if it is then
it should be trivial to port this to D:
template<class Diff, class It>
It setbits(It i, Diff j, Diff n)
{
typedef typename std::iterator_traits<It>::value_type T;
T ones = static_cast<T>(~T());
Diff const bits = static_cast<Diff>(sizeof(*i) * CHAR_BIT);
i += j / bits;
j %= bits;
if (j)
{
*i |= (ones << j) &
(ones >> max(0, static_cast<int>(bits - j - n)));
++i;
n -= min(n, bits - j);
}
fill_ni, n / bits, ones);
i += static_cast<ptrdiff_t>(n / bits);
n %= bits;
if (n)
{
*i |= ones >> max(0, static_cast<int>(bits - n));
++i;
}
return i;
}
More information about the Digitalmars-d
mailing list