Ok but if you do want to play with writing binary code manually I recommend Casey Muratori's performance course
4 billion if statements (2023)
121–130 of 183 posts
Re: 4 billion if statements (2023)
#122God 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!
Re: 4 billion if statements (2023)
#123This 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.
Re: 4 billion if statements (2023)
#124Earlier 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 ?
Re: 4 billion if statements (2023)
#125I 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 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)
#126Save 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)
#127This 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.
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.
Re: 4 billion if statements (2023)
#128Re: 4 billion if statements (2023)
#129 not eax
and eax, 1Re: 4 billion if statements (2023)
#130Really if you are not making custom silicon for this problem, you are just wasting our time here aren't you.
"Is it odd or Even?"
"YES"