Live data from Hacker News

4 billion if statements (2023)

andreasjhkarlsson.github.io

121–130 of 183 posts

Re: 4 billion if statements (2023)

#121
> How did I do this? Well I jumped online, using a mix of my early life experience coding emulators and hacking and looked into the x86(-64) architecture manuals to figure out the correct opcodes and format for each instruction. … Just kidding, that’s horrible. I asked ChatGPT

Ok but if you do want to play with writing binary code manually I recommend Casey Muratori's performance course

Re: 4 billion if statements (2023)

#122

God help us if that code ever makes it's way onto npm. isEven is a performant, hand-compiled evenness checker for any 32 bit integer. A single file import that does one job and one job only!

it follows the UNIX philosophy of doing one thing and donig it well.

Re: 4 billion if statements (2023)

#123
post #103

This is time efficient* but rather wasteful of space. The best way to save space is to use a Bloom Filter. If we capture all the even numbers, that would sadly only give us "Definitely not Even" or "Maybe Even". But for just the cost of doubling our space, we can use two Bloom filters! So we can construct one bloom filter capturing even numbers, and another bloom filter capturing odd numbers. Now we have "Definitely…

> But for just the cost of doubling our space, we can use two Bloom filters! We can optimize the hash function to make it more space efficient. Instead of using remainders to locate filter positions, we can use a mersenne prime number mask (like say 31), but in this case I have a feeling the best hash function to use would be to mask with (2^1)-1.

This produced strange results on my ternary computer. I had to use a recursive popcnt instead.

Re: 4 billion if statements (2023)

#124
post #13

Earlier quoted context omitted.

Yeah... I come here to talk about that. Should have been for i in range(0, 2**8, 2): print(" if (number == "+str(i)+")") print(" printf(\"even\\n\");") print(" if (number == "+str(i + 1)+")") print(" printf(\"odd\\n\");") or for i in range(0, 2**8, 2): print(f""" if (number == {i}) puts("even"); if (number == {i + 1}) puts("odd");""")

What happens when you try to compute 2**8+1 ?

If its too large you could just subtract 2*8 and try again.

Re: 4 billion if statements (2023)

#125
post #101

I took an ASIC design class in college, unfortunately with a heavy course load that didn't allow me to focus on it. For our final project we were given a numbered dictionary and asked to design a chip that would accept the characters on a 7 bit interface (ASCII), one character per clock cycle and output the dictionary number on an output interface but I can't remember how wide. We were graded on the size of the resul…

Yep! Something a bit counterintuitive on circuit design is that dedicated transistors will always beat reusing existing components. If we do reuse existing components like ALUs, multipliers, or state machines, we save on chip area but pay the penalty in clock cycles. Your approach was the extreme version of this tradeoff. You essentially unrolled the entire dictionary lookup into pure combinatorial logic (well, with…

It's akin to a compiler unrolling a loop. Uses more RAM (area) but fewer cycles to execute. Hardware synthesis uses many of the same techniques as compilers use to optimize code.

It's a common pitfall for those learning hardware description languages like Verilog, when they think about them like programming languages. If you go "if (calc) res Despite how leaning on the analogy too closely can mislead in that way, the analogy between hardware and software is not a shallow one. A combinatorial circuit is akin to the pure function of functional programming. Anything that can be described as a pure function working on fixed integers or floating point or other discrete data types, can be transformed into a combinatorial circuit. And there are algorithms to do so automatically and often with reasonable efficiency.

Free software synthesis has come a long way in recent years, by the way. There's even several hobbyist projects that can take VHDL or Verilog and produce layouts using TTL chips or even discrete transistor logic with automatic circuit board layout. You can now compile your code directly to circuit board copper masks and a part list.

Re: 4 billion if statements (2023)

#126
Any good engineer knows there is no "best" solution, only tradeoffs.

Save space.

  def even_flip_flop(number):
    even = True
    for _ in range(number):
      even = not even
    return even

Ditto. Sure, this overflows the stack, but you look cool doing it.

  def even_recursive(number):
    return True if number == 0 else not even_recursive(number - 1)

Save time. Just buy more RAM.

  table = [True, False] * 1000  # adjust to your needs
  def even_lookup(number):
    return table[number]

Re: 4 billion if statements (2023)

#127

This is time efficient* but rather wasteful of space. The best way to save space is to use a Bloom Filter. If we capture all the even numbers, that would sadly only give us "Definitely not Even" or "Maybe Even". But for just the cost of doubling our space, we can use two Bloom filters! So we can construct one bloom filter capturing even numbers, and another bloom filter capturing odd numbers. Now we have "Definitely…

How is this time efficient at all? It takes upwards of 40 seconds to compute on large 32bit values. It's a joke post with some interesting bits and details.

You're absolutely right. The obvious solution would have been to create a boolean table containing all the pre-computed answers, and then simply use the integer you are testing as the index of the correct answer in memory. Now your isEven code is just a simple array lookup! Such an obvious improvement, I can't believe the OP didn't see it.

And with a little extra work you can shrink the whole table's size in memory by a factor of eight, but I'll leave that as an exercise for the interested reader.

Post reply on HN