[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Weird code

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 24
Thread images: 7

Can anyone figure the purpose of the following code?


int rdrand64_step (uint64_t *rand)
{
unsigned char ok;
asm volatile ("rdrand %0; setc %1"
: "=r" (*rand), "=qm" (ok));
return (int) ok;
}


uint64_t eatPie(double f) {
double fraction = 1.0/f;
double remaining = 1.0;
uint64_t a;
double af;
uint64_t b;
double bf;
uint64_t maxUint64 = 0xFFFFFFFFFFFFFFFF;
uint64_t iterations = 0;

while (remaining >= fraction) {
rdrand64_step(&a);
rdrand64_step(&b);
af = ((double)a/maxUint64);
bf = ((double)b/maxUint64);
af = af * remaining;
bf = bf * remaining;

remaining = af;
if (bf < remaining) {remaining = bf;}

iterations += 1;
}

return iterations;
}


int main(int argc, const char * argv[]) {
uint64_t a;
double af;
uint64_t b;
double bf;
uint64_t maxUint64 = 0xFFFFFFFFFFFFFFFF;


double chance = 1.0 / 360;
double sum = 0.0;
uint64_t result[100];

for (uint64_t i = 0; i < 100; i++) {
result[i] = 0;
}

for (uint64_t i = 0; i < 100000000; i++) {
rdrand64_step(&a);
rdrand64_step(&b);
af = ((double)a/maxUint64);
bf = ((double)b/maxUint64);


if (bf < af) {sum += bf; result[(uint64_t)(bf*100.0)] += 1;}
else {sum += af; result[(uint64_t)(af*100.0)] += 1;}
}
printf("Hello, World! %1.20f \n", sum/1000);

for (uint64_t i = 0; i < 100; i++) {
printf("Hello, World! %llu - %llu \n", i, result[i]);
}



uint64_t iterations = 0;


for (uint64_t i = 0; i < 1000000; i++) {
iterations += eatPie(7.5);
}

printf("Hello, World! %llu \n", iterations);

printf("Hello, World! %1.20f \n", ((double)a/maxUint64));

return 0;
}


Picture unrelated.
>>
>>59074604

its gibberish

whoever made this should get shot
>>
>>59074645
I know its not gibberish. It generates intelligible output.
>>
>>59074604
Bump!

Someone must be able to understand at least a bit about it.

I know that rdrand64_step generates hardware random values.

I think eatPie takes a float and subtracts a portion of pi from it.
>>
>>
File: 1486443624358.gif (2MB, 350x227px) Image search: [Google]
1486443624358.gif
2MB, 350x227px
>>
>>59074734
The first part of the main is calculating a histogram of random numbers, with the added wrinkle that for each step it generates two random numbers and only counts the larger of the two numbers in the histogram. I'm not sure why you would do this...maybe a test of the reliability of your random number generator?

I'm too lazy to walk through the eatPie function, but I'm pretty sure it has nothing to do with pi.
>>
>>59075134
*smaller of the two numbers
>>
File: giphy-37.gif (4MB, 480x270px) Image search: [Google]
giphy-37.gif
4MB, 480x270px
>>59075134
Oh yeah I think I remember what I was doing with it.
I think it was meant to test probabilities. When you add two probability maps or something then you get a certain bias in the outcome and I wanted to see what that looked like with random data.

No idea why I wrote it in such a convoluted manner.

Thanks.
>>
File: 1477589869039.gif (2MB, 500x500px) Image search: [Google]
1477589869039.gif
2MB, 500x500px
>>59075455
>>
>>59074645
This. Needlessly difficult-to-read code and an obvious amateur trying to look sophisticated.
>>
>>59075824
>amateur trying to look sophisticated
wtf bro.

It was some private code I put together to test what probability outcomes would look like.

When I wrote it I was just testing something so I knew what it was doing and didn't put any effort into layout/comprehension. I stumbled upon it just now and couldn't figure what it was about.
>>
>>59075824
Here's one for you. Try to figure out how this code works.

/******************************************************************************/

static int sprp (uint64_t n, uint64_t a)
{
uint64_t m = n - 1, r, y;
unsigned int s = 1, j;

/* assert(n > 2 && (n & 0x1) != 0); */

while ((m & (UINT64_C(1) << s)) == 0) s++;
r = m >> s; /* r, s s.t. 2^s * r = n - 1, r in odd. */

if ((a %= n) == 0) /* else (0 < a < n) */
return (1);

{
unsigned __int128 u = 1, w = a;

while (r != 0)
{
if ((r & 0x1) != 0)
u = (u * w) % n; /* (mul-rdx) */

if ((r >>= 1) != 0)
w = (w * w) % n; /* (sqr-rdx) */
}

if ((y = (uint64_t) u) == 1)
return (1);
}

for (j = 1; j < s && y != m; j++)
{
unsigned __int128 u = y;
u = (u * u) % n; /* (sqr-rdx) */

if ((y = (uint64_t) u) <= 1) /* (n) is composite: */
return (0);
}

return (y == m);
}

/******************************************************************************/

static int is_prime (uint64_t n)
{
const uint32_t sprp32_base[] = /* (Jaeschke) */ {
2, 7, 61, 0};

const uint32_t sprp64_base[] = /* (Sinclair) */ {
2, 325, 9375, 28178, 450775, 9780504, 1795265022, 0};

const uint32_t *sprp_base;

/* assert(n > 1); */

if ((n & 0x1) == 0) /* even: */
return (n == 2);

sprp_base = (n <= UINT32_MAX) ? sprp32_base : sprp64_base;

for (; *sprp_base != 0; sprp_base++)
if (!sprp(n, *sprp_base)) return (0);

return (1); /* prime. */
}

/******************************************************************************/
>>
>>59075963
>2^64
why even bother checking the primality of such tiny numbers? Go bigint or go home
>>
File: 1487044745852.jpg (13KB, 500x300px) Image search: [Google]
1487044745852.jpg
13KB, 500x300px
>>
>>59075455
>>59075912
Write comments next time fag. Especially if it is your code.
>>
>>59074887
Wtf
>>
File: 1487089035662.jpg (40KB, 460x506px) Image search: [Google]
1487089035662.jpg
40KB, 460x506px
>>
Please, this is baby stuff.

Try to parse this, if you want to test your C comprehension:

http://pastebin.com/GQPnUi7d
>>
What is the tripcode algorithm?
>>
>>59079521
>>
>>59079537
>>
>>59079557
>>
>>59078972
Is he okay
Thread posts: 24
Thread images: 7


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.