Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Compile and run as follows:
- place in the same folder as randomdata.bin
- gcc -O2 -mbmi2 -march=native main.c
- time ./a.out > /dev/null
- [code]
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <immintrin.h>
- #define INPUT_FILE_PATH "randomdata.bin"
- #define BYTES_AT_A_TIME 20000 /*note: the program will use 11x this amount of RAM, 20x if you enable FIX_OUTPUT_NEWLINES*/
- //#define FIX_OUTPUT_NEWLINES TRUE /*enable this to output newlines between each byte (slower and uses more ram)*/#
- FILE *fp;
- unsigned int output_file_offset = 0;
- unsigned int input_file_offset = 0;
- char *buffer;
- uint64_t *out_buffer;
- unsigned int bytes_read = 0;
- #ifdef FIX_OUTPUT_NEWLINES
- void copyAndFixBuffer() {
- char* rawbuff = (char*)out_buffer;
- int s,e;
- for(s=0,e=0; s<output_file_offset*9;) {
- if(s % 9) {
- buffer[s]=rawbuff[e];
- s++,
- e++;
- }
- else {
- buffer[s] = '\n';
- s++;
- }
- }
- buffer[s]='\n';
- }
- #endif
- void flushdata() {
- #ifdef FIX_OUTPUT_NEWLINES
- copyAndFixBuffer();
- fwrite(buffer, output_file_offset*9+1,1,stdout );
- #else
- fwrite(out_buffer, output_file_offset*9+1,1,stdout );
- #endif
- }
- int main ()
- {
- fp = fopen(INPUT_FILE_PATH, "r");
- #ifdef FIX_OUTPUT_NEWLINES
- buffer = (char*) malloc(BYTES_AT_A_TIME+100);
- out_buffer = (uint64_t*)malloc(BYTES_AT_A_TIME*10);
- #else
- buffer = (char*) malloc(BYTES_AT_A_TIME*10);
- out_buffer = (uint64_t*)malloc(BYTES_AT_A_TIME*10);
- #endif
- while(bytes_read = fread(buffer, 1, BYTES_AT_A_TIME, fp)) {
- output_file_offset = 0;
- input_file_offset = 0;
- while(1) {
- if(input_file_offset == bytes_read) break;
- char c = buffer[input_file_offset];
- input_file_offset++;
- uint64_t out = __builtin_bswap64(_pdep_u64(c,0x0101010101010101)|0x3030303030303030);
- out_buffer[output_file_offset] = out;
- output_file_offset++;
- }
- flushdata();
- }
- return 0;
- }
- [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement