Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <fcntl.h>
- #include <unistd.h>
- #define TRUE 1
- #define FALSE 0
- #define KURWA -1
- #define MASK 1
- #define CND_ALICE 2
- #define CND_BOB 3
- #define SET_6BIT 0xFC
- #define CLR_BITS(val) ((val) &= (3))
- uint8_t get_byte(int32_t fd){
- uint8_t retval;
- if(1 != read(fd, &retval, 1)){
- fprintf(stderr, "Kurwa! Nie czytam plik!\n");
- _exit(KURWA);
- }
- return retval;
- }
- void run_test(uint64_t iterations, uint8_t run_orthogonaly, uint8_t do_flush){
- uint64_t count_alice;
- uint64_t count_bob;
- uint64_t i;
- int32_t urandom_fd;
- uint8_t buf;
- uint8_t buf_alice;
- uint8_t buf_bob;
- uint8_t cap_alice;
- uint8_t cap_bob;
- float retval_alice;
- float retval_bob;
- buf_alice = 0;
- buf_bob = 0;
- count_alice = 0;
- count_bob = 0;
- urandom_fd = open("/dev/urandom", O_RDONLY);
- if(KURWA == urandom_fd){
- fprintf(stderr, "Kurwa! Cos nie otworzylem plik!\n");
- _exit(KURWA);
- }
- for(i = 0; i < iterations; ++i){
- buf = get_byte(urandom_fd);
- buf_alice <<= 1;
- buf_alice += buf & MASK;
- CLR_BITS(buf_alice);
- if(TRUE == run_orthogonaly){
- buf = get_byte(urandom_fd);
- }
- buf_bob <<= 1;
- buf_bob += buf & MASK;
- CLR_BITS(buf_bob);
- if(do_flush){
- ++cap_alice;
- ++cap_bob;
- if(1 < cap_alice){
- if(CND_ALICE == buf_alice){
- ++count_alice;
- cap_alice = 0;
- }
- }
- if(1 < cap_bob){
- if(CND_BOB == buf_bob){
- cap_bob = 0;
- ++count_bob;
- }
- }
- }else{
- if(CND_ALICE == buf_alice)
- ++count_alice;
- if(CND_BOB == buf_bob)
- ++count_bob;
- }
- }
- retval_alice = (float)iterations/(float)count_alice;
- retval_bob = (float)iterations/(float)count_bob;
- fprintf(stdout, "Iterations\t= %lu\nOrthogonality\t: %s\nFlushing\t: %s\n",
- iterations,
- run_orthogonaly ? "Enabled" : "Disabled",
- do_flush ? "Enabled" : "Disabled");
- fprintf(stdout, "Alice\t\t= %f\n", retval_alice);
- fprintf(stdout, "Bob\t\t= %f\n\n", retval_bob);
- if(KURWA == close(urandom_fd)){
- fprintf(stderr, "Kurwa! Bled przy zamykaniu plika!\n");
- _exit(KURWA);
- }
- }
- int main(void){
- const uint64_t iterations = 100000;
- run_test(iterations, FALSE, FALSE);
- run_test(iterations, TRUE, FALSE);
- run_test(iterations, FALSE, TRUE );
- run_test(iterations, TRUE, TRUE );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement