#include #include #include #include using namespace std; #include "MOA.h" #include int main(int argc, char **argv) { int print; int andor; int bytes, bufsize; int i, j; uint8_t *buf1, *buf2, *buf3; __m128i *b1, *b2, *b3; /* usage: and_or bytes AND|OR|and|or|none seed print(Y|N) */ if (argc != 5 || (strcmp(argv[2], "AND") != 0 && strcmp(argv[2], "OR") != 0 && strcmp(argv[2], "and") != 0 && strcmp(argv[2], "or") != 0 && strcmp(argv[2], "none") != 0)) { fprintf(stderr, "usage: and_or bytes AND|OR|and|or|none seed print(Y|N)\n"); exit(1); } /* Set the variables from the command line */ bytes = atoi(argv[1]); andor = argv[2][0]; print = (argv[4][0] == 'Y'); MOA_Seed(atoi(argv[3])); /* Pad the number of bytes so that it's a multiple of 16, and allocate three buffers: */ bufsize = bytes; if (bufsize % 16 != 0) bufsize += (16 - bufsize%16); buf1 = (uint8_t *) malloc(bufsize); buf2 = (uint8_t *) malloc(bufsize); buf3 = (uint8_t *) malloc(bufsize); /* Use MOA.c to fill buf1 and buf2 with random bytes */ MOA_Fill_Random_Region (buf1, bytes); MOA_Fill_Random_Region (buf2, bytes); /* This lets you access the three buffers as 128-bit vectors. */ b1 = (__m128i *) buf1; b2 = (__m128i *) buf2; b3 = (__m128i *) buf3; /* Use SIMD to do AND. */ if (andor == 'A') { for (i = 0; i < bytes; i += 16) { *b3 = _mm_and_si128(*b1, *b2); b1++; b2++; b3++; } /* Use SIMD to do OR. */ } else if (andor == 'O') { for (i = 0; i < bytes; i += 16) { *b3 = _mm_or_si128(*b1, *b2); b1++; b2++; b3++; } /* Do the bit operations inefficiently on the bytes. */ } else if (andor == 'a') { for (i = 0; i < bytes; i++) buf3[i] = (buf1[i] & buf2[i]); } else if (andor == 'o') { for (i = 0; i < bytes; i++) buf3[i] = (buf1[i] | buf2[i]); } /* Print everything out. */ if (print) { printf("B1: "); for (i = 0; i < bytes; i++) { for (j = 0; j < 8; j++) printf("%d", (buf1[i] >> j) & 1); printf(" "); } printf("\n"); printf("B2: "); for (i = 0; i < bytes; i++) { for (j = 0; j < 8; j++) printf("%d", (buf2[i] >> j) & 1); printf(" "); } printf("\n"); printf("B3: "); for (i = 0; i < bytes; i++) { for (j = 0; j < 8; j++) printf("%d", (buf3[i] >> j) & 1); printf(" "); } printf("\n"); } return 0; }