News for hackers. Stuff that matters.
Re:4B If Statements by tarruda on Thursday December 28, 2023 @02:12AM (#38790880)
I wouldn't touch the generated ifs, but would generate a b-tree database which maps integers to the address of the generated code which has the desired "if" comparison. Then the C program starts by querying the b-tree for the address and just assigns that to "isEven" pointer.

Hmm... Nevermind. A really smart way to solve this would be to store the result of the "isEven" computation directly on the b-tree, so the whole problem can be solved with a simple database query!

Re:4B If Statements by ithkuil on Thursday December 28, 2023 @04:21AM (#38791605)
Or make an array with 2 billion uint64 entries all containing this constant:

0xC3C033C340C033

Then cast the array as if it was a uint32_t* add the number you want to check, cast it again as a function pointer of a function returns a int and voilà

It should take 16G of space to encode but it should require only a single page fault to load from disk.

    uint64_t a[] = { [ 0 .. INT_MAX ] = 0xC3C033C340C033 } ; 

    int is_even(uint32_t n) {
      return ((int (*)())((uint32_t *)a + n))();
    }
This uses a GCC extension. If you can't use that or if it doesn't work you can easily create an ELF object file that contains a data section with that number repeated 2 billion times.

Explaination:

That magic number is actually two 32-bit numbers juxtaposed:

The first one is 0xC340C033, which encodes the following instructions:

    xor eax, eax ; 33 C0
    inc eax ; 40
    ret ; C3
This effectively returns 1 in the eax register (which is compatible with the C calling convention)

The second number is similar but it doesn't include the "inc" instruction.

When casting as a int array we end up with an array with alternating function bodies. At even positions we have functions that return 1 (true) and at odd positions it returns 0 (false)

The interesting thing is each of those function bodies fit into a 32-bit number. A single jump to an arbitrary address would require larger array.

Of course, it's silly to do this for even/odd but it can be useful to understand how this works.

Go kids, build your own forths!

(Disclaimer: I haven't tried out any of the above and typed this on my phone while walking in the woods, so I'd be surprised if it actually works without some touches, but the gist of the idea should work)

Edit: fixes

Re:4B If Statements by ithkuil on Thursday December 28, 2023 @04:40AM (#38791720)

    .global a
    .data
    a:
        .fill 0x7FFFFFFF, 8, 0xC3C033C340C033
Re:4B If Statements by glandium on Thursday December 28, 2023 @06:10AM (#38792254)
Or make a lookup table with bits. It'd only be 512MB of 0xaa or 0x55. (but wouldn't use ifs)
Re:4B If Statements by neilv on Thursday December 28, 2023 @02:18AM (#38790911)
Make it a microservice in Kubernetes so that it scales.
Re:4B If Statements by gorbachev on Thursday December 28, 2023 @04:16AM (#38791569)
Why stop there. Create an algorithm that distributes the calculation of each unique number to a different instance. Surely that would scale even better!
Re:4B If Statements by jiggawatts on Thursday December 28, 2023 @02:22AM (#38790929)
I wouldn’t be surprised if there isn’t at least one thing this insane at some FAANG.

“We’ve got to scale the maths engine cluster to handle the load!”

Re:4B If Statements by rplnt on Thursday December 28, 2023 @02:39AM (#38791020)
Re:4B If Statements by vegardlarsen on Thursday December 28, 2023 @02:59AM (#38791107)
The actual implementation of this package is even better.

https://github.com/i-voted-for-trump/is-even/blob/master/ind...

Re:4B If Statements by tarruda on Thursday December 28, 2023 @03:04AM (#38791137)
What is even better is the fact that it has 200k weekly downloads. I thought this was supposed to be a troll package.
Re:4B If Statements by doikor on Thursday December 28, 2023 @03:24AM (#38791253)
It is a troll package. Just a successful one.
Re:4B If Statements by whaleofatw2022 on Thursday December 28, 2023 @11:04AM (#38794789)
Don't fall for the salesman's "it was a joke" play.

Is-thirteen, now THAT is a troll package.

Re:4B If Statements by chupapimunyenyo on Thursday December 28, 2023 @01:03PM (#38796354)
But why do people use this? Is this a dependency in some popular library?
Re:4B If Statements by madarcho on Thursday December 28, 2023 @03:02AM (#38791123)
Well worth a quick trip to the source to see how it's implemented. After all, how would the author get around to packaging 4B if statements, like the OP?
Re:4B If Statements by ezconnect on Thursday December 28, 2023 @03:35AM (#38791325)
He could package it to the size limit and range the numbers it could detect. Add a note to download particular package if the number is out of range of the package and that package is not installed.
Re:4B If Statements by rplnt on Thursday December 28, 2023 @01:15PM (#38796511)
One package should probably handle range of one million. Now it's just 4000 packages to install. You wouldn't even notice that in an average js project.