Advertisement
Guest User

Untitled

a guest
Oct 8th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. Compile and run as follows:
  2. place in the same folder as randomdata.bin
  3. gcc -O2 -mbmi2 -march=native main.c
  4. time ./a.out > /dev/null
  5.  
  6. [code]
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <immintrin.h>
  11.  
  12. #define INPUT_FILE_PATH "randomdata.bin"
  13.  
  14. #define BYTES_AT_A_TIME 20000 /*note: the program will use 11x this amount of RAM, 20x if you enable FIX_OUTPUT_NEWLINES*/
  15.  
  16. //#define FIX_OUTPUT_NEWLINES TRUE  /*enable this to output newlines between each byte (slower and uses more ram)*/#
  17.  
  18. FILE *fp;
  19. unsigned int output_file_offset = 0;
  20. unsigned int input_file_offset = 0;
  21. char *buffer;
  22. uint64_t *out_buffer;
  23. unsigned int bytes_read = 0;
  24.  
  25. #ifdef FIX_OUTPUT_NEWLINES
  26. void copyAndFixBuffer() {
  27.     char* rawbuff = (char*)out_buffer;
  28.     int s,e;
  29.     for(s=0,e=0; s<output_file_offset*9;) {
  30.         if(s % 9) {
  31.             buffer[s]=rawbuff[e];
  32.             s++,
  33.             e++;
  34.         }
  35.         else {
  36.             buffer[s] = '\n';
  37.             s++;
  38.         }
  39.     }
  40.     buffer[s]='\n';
  41. }
  42. #endif
  43.  
  44. void flushdata() {
  45. #ifdef FIX_OUTPUT_NEWLINES
  46.     copyAndFixBuffer();
  47.     fwrite(buffer, output_file_offset*9+1,1,stdout );
  48. #else
  49.     fwrite(out_buffer, output_file_offset*9+1,1,stdout );
  50. #endif
  51.  
  52. }
  53.  
  54. int main ()
  55. {
  56.     fp = fopen(INPUT_FILE_PATH, "r");
  57. #ifdef FIX_OUTPUT_NEWLINES
  58.     buffer = (char*) malloc(BYTES_AT_A_TIME+100);
  59.     out_buffer = (uint64_t*)malloc(BYTES_AT_A_TIME*10);
  60. #else
  61.     buffer = (char*) malloc(BYTES_AT_A_TIME*10);
  62.     out_buffer = (uint64_t*)malloc(BYTES_AT_A_TIME*10);
  63. #endif 
  64.     while(bytes_read = fread(buffer, 1, BYTES_AT_A_TIME, fp)) {
  65.         output_file_offset = 0;
  66.         input_file_offset = 0;
  67.         while(1) {
  68.             if(input_file_offset == bytes_read) break;
  69.             char c = buffer[input_file_offset];
  70.             input_file_offset++;
  71.             uint64_t out = __builtin_bswap64(_pdep_u64(c,0x0101010101010101)|0x3030303030303030);
  72.             out_buffer[output_file_offset] = out;
  73.             output_file_offset++;
  74.         }
  75.         flushdata();
  76.     }
  77.     return 0;
  78. }
  79. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement