HNHacker News
TopNewBestAskShowJobs

hairtuq

85 karma · joined March 29, 2017

submissionscomments
hairtuq··on When Compiler Optimizations Hurt Performance
The mapping after we have the leading 1's count can be done in 3 instructions (in 32-bit math) on either x86 or ARM:

    t = 0x2020c6 >> x
    return (t << (t & 31)) >> 27
hairtuq··on How we made JSON.stringify more than twice as fast
If you want to optimize it a little more, you can combine isLess(s, 0x20) and isChar('"') into isLess(s ^ (kOnes * 0x02), 0x21) (this works since '"' is 0x22).
hairtuq··on A leap year check in three instructions
Thanks, fixed.
hairtuq··on Optimizing with Novel Calendrical Algorithms
The article leaves out optimizing is_leap_year. Here is an interesting version that is correct (in the Proleptic Gregorian calendar) for 0 <= year <= 102499 and takes only 3 instructions:

    ((year * 1073750999) & 3221352463) <= 126976
How this works is surprisingly complex, I am thinking of writing a whole blog post about it...
hairtuq··on Optimizing uint64_t Digit Counting: A Method that Beats Lemire's by up to 143%
Here is a cute variant that doesn't need lzcnt nor tables, but only works up to 99,999:

    (((x + 393206) & (x + 524188)) ^ ((x + 916504) & (x + 514288))) >> 17
This is for integer log 10, but could be adapted for number of digits. It needs a wrapper for 64 bit to invoke it multiple times, but most numbers in a JSON are small, so it might even be competitive; it needs only 4 cycles with enough instruction level parallelism.

I gathered this idea from the output of a superoptimizer, it was fun to figure out how it works. For spoilers, see [1].

[1] https://github.com/rust-lang/rust/blob/master/library/core/s...

hairtuq··on Bitbanging 1D Reversible Automata
The author wonders:

> In theory at least, the compiler can see that rule only has 256 values and create a reduced version of ca1d_rule_apply for each value. Whether it actually does is not of much practical concern when the rendering code is the bottle neck. However it’s interesting to see if the compiler can deduce the best solution or whether anything trips it up.

The compiler is unlikely to get the optimal result here. The core of this is finding the best instruction sequence for a ternary boolean operation encoded in 8 bits; it's the same job needed for emulating the AVX512F "vpternlog" instruction. This can always be done in at most 5 instructions (or 4 if you have andnot/ornot/xornot), but it's not straightforward to do this. Here is some code that calculates optimal instruction sequences (by letting z3 do the heavy lifting): https://github.com/falk-hueffner/ternary-logic-optimization

hairtuq··on Decoding UTF8 with parallel extract
Similarly, the eexpect table can be done in 3 instructions with with t = 0x3c783023f + i; return ((t >> (t & 63)) & 0xf0808088.
hairtuq··on Show HN: Kuboble.com – Minimalistic sliding pieces puzzle game
I wrote a solver for Atomix (https://github.com/falk-hueffner/atomixer), a similar game. Technically, the only difference is that in Atomix the solution location is not predefined, although probably based on level design this game actually feels quite different. Since the solver just tries all solution location anyway, it would be quite simple to adapt it to this game.
hairtuq··on A neat XOR trick
On a related note, almost any task around shuffling or shifting bits can be improved with xor. For example the straightforward way to remove bit i is

    mask = -1 << i;
    return (x & ~mask) | ((x >> 1) & mask);
but with xor we can do it in one less instruction:

    mask = -1 << i;
    return ((x ^ (x >> 1)) & mask) ^ x;
hairtuq··on After Xinjiang Revelations, Germany's Ties to China Are Under the Microscope
It seems you're shifting the goal posts now. The fact that consensus criticisms are not as harsh as you'd like doesn't mean that Israel is "totally off limits". Discussions and criticisms are happening; and the fact that public opinion doesn't match yours can not only be explained by that opinion being suppressed, but also by it being wrong.
hairtuq··on After Xinjiang Revelations, Germany's Ties to China Are Under the Microscope
I don't see that. For example, it has been the official position of the German government for at least 20 years to strongly criticize the Israeli settlements. There's regular critical reports in the press, endless TV discussions and so on. Can you name someone who has actually had to suffer negative consequences for reasonable criticism of Israel?
hairtuq··on Show HN: PyIng – Ingredient parser
I wrote an ingredient parser that is basically just one giant regular expression: https://github.com/falk-hueffner/metric-cooking/blob/master/...

The use case is a bit different (the task includes finding ingredients mentioned in a longer text, and it'll rather not parse something rather than parsing it wrong), but it works fairly OK and can even parse things like "3/4 cup plus 2 tablespoons packed light-brown sugar".

hairtuq··on Solving NP-hard puzzles with the oldest trick in the book
For the quite similar puzzle Atomix, it also seems like the branching factor would be much higher for backward search because upper bounds are weaker, but you can show that on average the branching factor is actually the same [1]. I wonder if the same argument would work here.

[1] http://hueffner.de/falk/hueffner-studienarbeit-atomix.pdf Section 5.5

hairtuq··on Impact of Undefined Behavior on Performance
You don't necessarily need a wider type (which might be slower to work with), you can just calculate (n|1) * ((n+1)/2). Clang does something much more complicated, probably because for it is just a special case of some much more general optimization.
hairtuq··on Counting the number of matching characters in two ASCII strings
Slightly simpler:

    c = x ^ y;
    return popcount(((c >> 1) | 0x8080808080808080) - c) & 0x8080808080808080);
hairtuq··on Counting the number of matching characters in two ASCII strings
Actually, e is the inverse of the xor, so (e & c1) is guaranteed to be c1, and you still need popcount(c1 & ((e & c2) + c3)).
hairtuq··on Bit Twiddling Hacks
The name SWAR is sometimes used (https://en.wikipedia.org/wiki/SWAR).

Indeed, even 8-bit fields can be added in parallel, using the fact that ^ is like a + that does not produce a carry:

    uint64_t signmask = 0x8080808080808080;
    uint64_t sum_without_sign_bits = ((x & ~signmask) + (y & ~signmask));
    uint64_t sum_of_sign_bits = (x ^ y) & signmask;
    return sum_without_sign_bits ^ sum_of_sign_bits;
hairtuq··on Programming with a Read-Eval-Synth Loop [pdf]
There is some work on automatically generating data structure implementations from high-level specifications: https://cozy.uwplse.org/
hairtuq··on Big O, Little N
Since the shift count is implicitly modulo 32, you don't even need the subtraction:

    bool isvowel(char c) {
        return (1u << (c&31)) & 0x208222;
    }
hairtuq··on Turn recipe websites into plain text
I wrote a browser extension that does this (although it will of course occasionally get something wrong): https://github.com/falk-hueffner/metric-cooking
hairtuq··on Programming trick questions
Here's a version that at least for 64 bit might be faster:

  bool isPerfect(uint64_t x) {
        int ctz = __builtin_ctzll(x);
        return ((0x40051056 >> ctz) & 1) && ((x >> ctz) + 1) == (uint64_t(1) << (ctz + 1));
  }
hairtuq··on Show HN: libcodr7 – fundamental collections in the spirit of C
Red-black tree presentations are often more complicated than necessary, mostly because of (premature?) optimizations. A simple presentation can be found here: https://www.cs.tufts.edu/~nr/cs257/archive/chris-okasaki/red...

I haven't checked how this relates to the code in this library.

hairtuq··on Scooping the Loop Snooper: Proof That the Halting Problem Is Undecidable (2000)
But it's not only impossible to decide halting faster than the program would run, but impossible at all, a much stronger statement.
hairtuq··on The Z3 Theorem Prover
That's very unlikely to happen, since finding a counterexample or proving there isn't one is much easier than finding a program candidate, so it'll usually time out in that phase.
hairtuq··on The Z3 Theorem Prover
I wrote a superoptimizer using z3 here: https://github.com/falk-hueffner/sematrope . The idea is to have variables that encode the program and a giant if-then-else statement that represents program execution. Then for a list of input/output pairs you let the solver find a program encoding that yields correct outputs. Next you let the solver find a counterexample where the current program candidate does not give the correct result. If one is found, it is added to the list of input/output pairs, otherwise you have found a correct program.
hairtuq··on Parsing short hexadecimal strings efficiently
I tried using https://github.com/falk-hueffner/sematrope. Best I can get is x - ((x >> 4) ^ 0x33), should be slightly faster. EDIT: unfortunately that works only for upper case letters.
hairtuq··on Nikoli Puzzles
The most fun part is when you get to use the Jordan curve theorem (using the parity of an intersecting ray).
hairtuq··on What's the minimum number of words you'd need to define all other words? (2012)
This will not yield a minimal set; in a cycle, it is only necessary to remove at least one word. The problem is thus to delete the minimum number of vertices to remove all cycles. This is the NP-hard Feedback Vertex Set problem. Here's a paper that solves it for a dictionary (there is some more): https://arxiv.org/abs/0911.5703
hairtuq··on How long is that Bézier?
If you're concerned about numerical inaccuracy of an expression, it might be worthwhile trying Herbie (https://herbie.uwplse.org/), a system that will try to rearrange expressions to minimize numerical error even in corner cases. It usually makes expressions bigger and slower, but since it works by brute force sometimes it even finds simplifications.
hairtuq··on Darwinian data structure selection
There is a tool that synthesize data structures based on the requirements similar as you describe: https://github.com/CozySynthesizer/cozy. It can handle even involved constraints that are not fulfilled by any off-the-shelf data structure. It's not integrated into an SDK though.
Page 1 of 2Next →