void apply_mark_flagstates(struct apply_handle *h) { int i; struct fsm_state *fsm; /* Create bitarray with those states that have a flag symbol on an arc */ /* This is needed to decide whether we can perform a binary search. */ if (!h->has_flags || h->flag_lookup == NULL) { return; } if (h->flagstates) { xxfree(h->flagstates); } h->flagstates = xxcalloc(BITNSLOTS(h->last_net->statecount), sizeof(uint8_t)); fsm = h->last_net->states; for (i=0; (fsm+i)->state_no != -1; i++) { if ((fsm+i)->target == -1) { continue; } if ((h->flag_lookup+(fsm+i)->in)->type) { BITSET(h->flagstates,(fsm+i)->state_no); } if ((h->flag_lookup+(fsm+i)->out)->type) { BITSET(h->flagstates,(fsm+i)->state_no); } } }
main() { char bitarray[BITNSLOTS(47)]; BITSET(bitarray, 23); BITCLEAR(bitarray, 14); if(BITTEST(bitarray, 35)) printf("yep\n"); else printf("nope\n"); sieve(); return 0; }
// Main function int main() { int i; int debug; int delay; int uio0_fd; const char *uio0_d = "/dev/uio0"; void *uio0_ptr; printf("-- Example program begins --\n"); // Debug debug = 0; if (debug) { printf("[debug] CHAR_BIT: %d\n", CHAR_BIT); printf("[debug] BITNSLOTS(8): %d\n", BITNSLOTS(8)); for (i=0; i<CHAR_BIT; i++) { dest_register[0] = 0x0; printf("[debug] BITSET(register,%d): 0x%02x\n", i, BITSET(dest_register,i)); } printf("[debug] sizeof uint640_t: %d\n", sizeof(struct uint640_t)); } // Open the UIO devices uio0_fd = open(uio0_d, O_RDWR); if (uio0_fd < 1) { printf("Invalid UIO device file:%s.\n", uio0_d); exit(-1); } // mmap() the UIO devices uio0_ptr = mmap(NULL, UIO0_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, uio0_fd, 0); if (uio0_ptr == MAP_FAILED) { printf("mmap call failure.\n"); exit(-1); } // Write debug = 0; if (debug) { destination = (unsigned *) uio0_ptr; dest_register[0] = 0xFF; printf("\n"); printf("Writing 0x%02x (0b%s).\n", dest_register[0], byte_to_binary(dest_register[0])); gpio_write(destination, 0, dest_register[0]); for (delay = 0; delay < LED_DELAY; delay++); printf("Write finish\n"); } // Write debug = 0; if (debug) { destination = (unsigned *) uio0_ptr; dest_register[0] = 0x0; int test_data[8] = {0, 255, 127, 128, 13, 82, 148, 223}; for (i=0; i<8; i++) { printf("\n"); printf("Writing 0x%02x (0b%s) by bit-banging via 4 lines, MSB first.\n", test_data[i], byte_to_binary(test_data[i])); printf("line 0 : sr_out\n"); printf("line 1 : sr_en\n"); printf("line 2 : sr_ck\n"); printf("line 3 : sr_in\n"); printf("lines 4-7: not connected\n"); send_8bit_serial_data(test_data[i]); printf("Write finish\n"); } } // Write { destination = (unsigned *) uio0_ptr; dest_register[0] = 0x0; struct uint640_t control_register; //control_register.data[0] = 0x00000000; //control_register.data[1] = 0x11111111; //control_register.data[2] = 0x22222222; //control_register.data[3] = 0x33333333; //control_register.data[4] = 0x44444444; //control_register.data[5] = 0x55555555; //control_register.data[6] = 0x66666666; //control_register.data[7] = 0x77777777; //control_register.data[8] = 0x88888888; //control_register.data[9] = 0x99999999; //control_register.data[10] = 0xAAAAAAAA; //control_register.data[11] = 0xBBBBBBBB; //control_register.data[12] = 0xCCCCCCCC; //control_register.data[13] = 0xDDDDDDDD; //control_register.data[14] = 0xEEEEEEEE; //control_register.data[15] = 0xFFFFFFFF; //control_register.data[16] = 0x00000000; //control_register.data[17] = 0x11111111; //control_register.data[18] = 0x22222222; //control_register.data[19] = 0x33333333; //const char * string = "33333333 22222222 11111111 00000000 FFFFFFFF " // "EEEEEEEE DDDDDDDD CCCCCCCC BBBBBBBB AAAAAAAA " // "99999999 88888888 77777777 66666666 55555555 " // "44444444 33333333 22222222 11111111 00000000"; const char * string = "FFEEDDCC BBAA9988 77665544 33221100 FFFFFFFF " "EEEEEEEE DDDDDDDD CCCCCCCC BBBBBBBB AAAAAAAA " "99999999 88888888 77777777 66666666 55555555 " "44444444 33333333 22222222 11111111 00000000"; string_to_uint640(string, &control_register); // Sanity check { char string2[200]; uint640_to_string(&control_register, string2); } send_616bit_serial_data(&control_register); printf("Write finish\n"); } printf("-- Example program ends --\n"); // Unmap the UIO devices munmap(uio0_ptr, UIO0_SIZE); return 0; }
#define SR_IN 0x3 #define SK2_SC_NBITS 616 #define SK2_SC_NWORDS 20 struct uint640_t { unsigned int data[SK2_SC_NWORDS]; }; // Other definitions //#define LED_DELAY 80000000 #define LED_DELAY 10000000 // Static variables static unsigned * destination; static unsigned char dest_register[BITNSLOTS(8)]; // Functions inline void gpio_write(void *gpio_base, unsigned int offset, unsigned int value) { *((volatile unsigned *)(gpio_base + offset)) = value; } inline unsigned int gpio_read(void *gpio_base, unsigned int offset) { return *((volatile unsigned *)(gpio_base + offset)); } inline void output_low(unsigned char bit) { BITCLEAR(dest_register, bit); }
BloomFilter::BloomFilter(int init_size){ size = BITNSLOTS(init_size) * CHAR_BIT; bitarray = new char[BITNSLOTS(init_size)]; for(int i =0; i < init_size; i++) BITCLEAR(bitarray,i); };
int Trie_restart(void* T) { assert(T); trie_t* t = (trie_t*)T; assert(t -> root); struct node* start; int i, j; #ifdef LZW_DEBUG printf(" - restarting it\n"); #endif for (i = 0; i < Node_childno(t -> root); i++) { start = t -> root -> child[i]; assert(start); for (j = 0; j < Node_childno(start); j++) Trie_free_traverse(start -> child[ j]); if (Node_childno(start)) free(start -> child); start -> child = NULL; memset(start -> bitarray, 0, BITNSLOTS(256)); } t -> highest_index = Node_childno(t -> root); t -> total_node_count = Node_childno(t -> root); while (1) { for (j = 9; j < 16; j++) if (t -> highest_index == LZW_INCREASE(j)) { if (t -> highest_index < LZW_INCREASE(16) - 1) { t -> highest_index++; continue; } } assert(t -> highest_index < LZW_INCREASE(16)); if (t -> highest_index == LZW_FREEZE) { if (t -> highest_index < LZW_INCREASE(16) - 1) { t -> highest_index++; continue; } } assert(t -> highest_index < LZW_INCREASE(16)); if (t -> highest_index ==LZW_RESTART) { if (t -> highest_index < LZW_INCREASE(16) - 1) { t -> highest_index++; continue; } } assert(t -> highest_index < LZW_INCREASE(16)); break; } if (Node_childno(t -> root) > 0) t -> max_depth = 1; else t -> max_depth = 0; t -> frozen = TRIE_NOTFROZEN; return 0; }
int threaded_main(long max, long procs, FILE* output) { int ret; long i = 0; NUM_THREADS = procs; MAX = max; if (output == NULL) { output = stdout; } pthread_t *threads = (pthread_t *) malloc(NUM_THREADS * sizeof(pthread_t)); if (threads == 0) errExit("malloc threads"); working = (long *) malloc(NUM_THREADS * sizeof(long)); if (working == 0) errExit("malloc working"); for (int i = 0; i < NUM_THREADS; ++i) { working[i] = 1; } mutexes = (pthread_mutex_t *) malloc((BITNSLOTS(MAX)+1) * sizeof(pthread_mutex_t)); if (mutexes == 0) errExit("malloc mutexes"); //fprintf(stderr, "BITNSLOTS = %ld\n", (long) BITNSLOTS(MAX)); bitarray = (uint8_t *) malloc( BITNSLOTS(MAX) * sizeof(uint8_t)); if (bitarray == 0) errExit("malloc bitarray"); memset(bitarray, 0, BITNSLOTS(MAX)); for (i = 0; i < BITNSLOTS(MAX); ++i) { ret = pthread_mutex_init(&mutexes[i], NULL); if (ret != 0) errExit("pthread_mutex_init"); } for (i = 0; i < NUM_THREADS; ++i) { ret = pthread_create(&threads[i], NULL, sieve, (void *)i); if (ret != 0) errExit("pthread_create"); } for (int j = 0; j < NUM_THREADS; ++j) { ret = pthread_join(threads[j], NULL); if (ret != 0) errExit("pthread_join"); } for (i = 0; i < BITNSLOTS(MAX); ++i) { ret = pthread_mutex_destroy(&mutexes[i]); if (ret != 0) errExit("pthread_mutex_destroy"); } for (i = 2; i < MAX; ++i) { if(!BITTEST(bitarray, i)) { fprintf(output, "%ld\n", i); } } free(working); free(threads); free(mutexes); free(bitarray); return 0; }
#include <limits.h> #include <pthread.h> #include <stdlib.h> #define BITMASK(b) (1 << ((b) % CHAR_BIT)) #define BITSLOT(b) ((b) / CHAR_BIT) #define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b)) #define BITCLEAR(a, b) ((a)[BITSLOT(b)] &= ~BITMASK(b)) #define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b)) #define BITNSLOTS(nb) ((nb + CHAR_BIT - 1) / CHAR_BIT) //#define MAX 4294967295 #define MAX 1000000 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; uint32_t bitarray[BITNSLOTS(MAX)]; void *find_prime(void *arg) { int prime_count = 0; uint32_t i, j; int s; //memset(bitarray, 0, BITNSLOTS(MAX)); memset(bitarray, 0, sizeof(bitarray)); for (i = 2; i < MAX; i++){ if (!BITTEST(bitarray, i)){ s = pthread_mutex_lock(&mtx); if (s != 0) perror("pthread_mute_lock"); prime_count++;
int main() { unsigned int input; unsigned int bit_length; unsigned int rng_ceil; unsigned int key_size; unsigned int bits; char generate_keys = 'y'; while(is_power_of_two(rng_ceil = get_rng_ceil()) == 0) { puts("Sorry, I can only use powers of 2!"); } bit_length = get_bit_length(rng_ceil); while (generate_keys == 'y' || generate_keys == 'Y') { bits = 0; key_size = get_key_size(); char key[BITNSLOTS(key_size)]; while (bits < key_size) { input = (get_rng_input(rng_ceil) - 1); for (int i = 0; i < bit_length && bits < key_size; i++) { if (input % 2 == 1) { BITSET(key, bits); } else { BITCLEAR(key, bits); } input = input >> 1; bits += 1; } printf("%d of %d bits generated...\n", bits, key_size); } printf("Your key is:\n0x"); unsigned int chunk = 0; for (int i = 0; i < bits; i++) { if (BITTEST(key, i)) { chunk += 1; } if (i > 0 && ((i+1) % HEXBITS == 0 || i+1 == bits)) { printf("%X", chunk); chunk = 0; } else { chunk = chunk << 1; } } puts("\nBe sure to run this through newBitcoinKey in bitcoin.sh first if you are planning on using this to seed a Bitcoin address!"); puts("Generate another? [y/n]"); generate_keys = getchar(); while (generate_keys != 'y' && generate_keys != 'Y' && generate_keys != 'n' && generate_keys != 'N') { generate_keys = getchar(); } } return 0; }