When looking at DSL.java I saw the note regarding your current BitCount
function and had a quick look.
Your comment "severe madness lies ahead" seems about right. The requirement
is known as a Hamming weight (I worried about such things back in the days
of parity bits having to be set for serial links), but you are very limited
by the fact that SQL does not allow temporary values to be generated and
reused within a single SQL statement, so you can't take the product of one
computed column and reuse it - i.e.
SELECT tt * 2 AS value, 1 AS tt
is not possible.
Trying to follow a net based coding example I ended up with
SELECT (r3 * 0x0101010101010101) >> 56 AS output
FROM
(
SELECT (r2 + (r2 >> 4)) & 0x0f0f0f0f0f0f0f0f AS r3
FROM
(
SELECT (r1 & 0x3333333333333333) + (( r1 >> 2) &
0x3333333333333333) AS r2
FROM
(
SELECT MyField - ((MyField >> 1) & 0x5555555555555555) AS r1
FROM
(
....... get the field from somewhere
)
) AS t1
) AS t2
) AS t3
All the sub selecting can be removed by expanding out the maths so you end
up with the following
SELECT ((((((results.myValue - ((results.myValue >> 1) &
0x5555555555555555)) & 0x3333333333333333) +
(((results.myValue - ((results.myValue >> 1) &
0x5555555555555555)) >> 2) & 0x3333333333333333)) +
((((results.myValue - ((results.myValue >> 1) &
0x5555555555555555)) & 0x3333333333333333) +
(((results.myValue - ((results.myValue >> 1) &
0x5555555555555555)) >> 2) & 0x3333333333333333)) >> 4)) &
0x0f0f0f0f0f0f0f0f) * 0x0101010101010101) >> 56 AS output
FROM
(
SELECT 255 AS myValue
) AS results
While both 'should' produce the bit count for a 64 bit integer, there is a
major problem - all the 'quick' code is written with the last multiply
causing a possible overflow (not a problem for C or ASM), so in MYSQL the
above code only works for counting the bits in a byte, it can also be
modified to support 16 and 32 bit ints I think, but you can not do the full
64bits. with full 64bit wide numbers you also start to see problems with
signed numbers.
I think the above shows that what you currently do (n bit checks based on
the size of the field) is the best way to handle a generic solution that
will work across a wide range of systems and all bit widths.
There is also a solution that does not use a multiply, but as it has
additional steps the expanded out version is many times larger and still
suffers from number signing problems.
Roger
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.