Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <process.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include <intrin.h>
- #include <time.h>
- // macros
- #define DEBUGP(x) if(1) { puts(x); system("pause"); }
- #define DEBUGF(x, ...) if(1) { printf(x, __VA_ARGS__); system("pause"); }
- // typedefs
- typedef unsigned long long ulong;
- typedef unsigned int uint;
- typedef unsigned short int ushort;
- typedef unsigned char uchar;
- // defs
- void set_icon();
- void thread_benchmark(void *);
- int _m128i_equals_uword(__m128i *, __m128i *);
- int _m128i_equals_sword(__m128i *, __m128i *);
- int _m128i_equals_sbyte(__m128i *, __m128i *);
- int _m128i_equals_ubyte(__m128i *, __m128i *);
- __declspec(noinline) float benchmark(const uint, const uint, char *, char *, int(*)(__m128i *, __m128i *));
- // constants
- const float TICK_MODIFIER = 1000;
- const uint BENCHMARK_ROUNDS = 0x3FFFFFF;
- const uint BENCHMARK_OUTER_ROUNDS = 32;
- const char *const descriptions[] =
- {
- "_SIDD_SWORD_OPS",
- "_SIDD_UWORD_OPS",
- "_SIDD_SBYTE_OPS",
- "_SIDD_UBYTE_OPS"
- };
- int(*const methods[])(__m128i *, __m128i *) =
- {
- _m128i_equals_sword,
- _m128i_equals_uword,
- _m128i_equals_sbyte,
- _m128i_equals_ubyte
- };
- // structs
- struct Stats
- {
- uint threads;
- uint totalRounds;
- uint currentRound[4];
- float averages[4];
- uchar complete[4];
- } _stats;
- struct ThreadInstructions
- {
- const uint ordinal;
- const uint innerRounds;
- const uint outerRounds;
- const char *textOne;
- const char *textTwo;
- int(*const method)(__m128i *, __m128i *);
- };
- bool draw_stats(HANDLE consoleHandle, struct Stats *stats)
- {
- const static CONSOLE_CURSOR_INFO hideCursorInfo = { .dwSize = 100, .bVisible = FALSE };
- const static CONSOLE_CURSOR_INFO defaultCursorInfo = { .dwSize = 25, .bVisible = TRUE };
- const static COORD zeroCoords = { .X = 0, .Y = 0 };
- SetConsoleCursorInfo(consoleHandle, &hideCursorInfo);
- SetConsoleCursorPosition(consoleHandle, zeroCoords);
- puts(" STATISTICS\n"
- "------------");
- for (uint i = 0; i < 4; ++i)
- {
- printf("Thread %-2u\n", i+1);
- printf(" Method: %s \n", descriptions[i]);
- printf(" Average: %-6g \n", _stats.averages[i]);
- printf(" Round: %u/%u \n", _stats.currentRound[i], BENCHMARK_OUTER_ROUNDS);
- printf(" Done: %u \n\n", _stats.complete[i]);
- }
- SetConsoleCursorInfo(consoleHandle, &defaultCursorInfo);
- return _stats.complete[0] &&
- _stats.complete[1] &&
- _stats.complete[2] &&
- _stats.complete[3];
- }
- int main()
- {
- char *const first = _aligned_malloc(32, 16);
- memcpy(first, "11113333333333334444333333333333", 32);
- char *const second = _aligned_malloc(32, 16);
- memcpy(second, "11113333333333334444333333333333", 32);
- HANDLE conHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- draw_stats(conHandle, &_stats);
- struct ThreadInstructions *instructionPool = malloc(sizeof(struct ThreadInstructions) * 4);
- for (uint i = 0; i < 4; ++i)
- {
- struct ThreadInstructions instructions =
- {
- .ordinal = i,
- .innerRounds = BENCHMARK_ROUNDS,
- .outerRounds = BENCHMARK_OUTER_ROUNDS,
- .textOne = first,
- .textTwo = second,
- .method = methods[i],
- };
- memcpy(instructionPool + i, &instructions, sizeof(struct ThreadInstructions));
- _beginthread(thread_benchmark, 0, (void *)(instructionPool + i));
- }
- while (1)
- {
- if (draw_stats(conHandle, &_stats))
- {
- puts("\nFinished.");
- break;
- }
- Sleep(100);
- }
- free(instructionPool);
- _aligned_free(first);
- _aligned_free(second);
- system("pause");
- return EXIT_SUCCESS;
- }
- void __cdecl thread_benchmark(void *instructions)
- {
- struct ThreadInstructions *instr = (struct ThreadInstructions *)instructions;
- ulong total = 0;
- uint count = 0;
- _stats.currentRound[instr->ordinal] = 0;
- _stats.complete[instr->ordinal] = false;
- _stats.averages[instr->ordinal] = 0;
- for (uint i = 0; i < instr->outerRounds; ++i)
- {
- clock_t begin = clock();
- for (uint j = 0; j < instr->innerRounds; ++j)
- {
- instr->method((__m128i *)instr->textOne, (__m128i *)instr->textTwo);
- }
- clock_t end = clock();
- total += (end - begin);
- ++count;
- _stats.currentRound[instr->ordinal] = i+1;
- _stats.averages[instr->ordinal] = total / (float)count;
- }
- _stats.complete[instr->ordinal] = true;
- }
- int _m128i_equals_sword(__m128i *x, __m128i *z)
- {
- while (*(char *)x && *(char *)z)
- {
- if (_mm_cmpistri(*x, *z, _SIDD_CMP_EQUAL_EACH | _SIDD_SWORD_OPS))
- return 0;
- ++z;
- x++;
- }
- return 1;
- }
- int _m128i_equals_uword(__m128i *x, __m128i *z)
- {
- while (*(char *)x && *(char *)z)
- {
- if (_mm_cmpistri(*x, *z, _SIDD_CMP_EQUAL_EACH | _SIDD_UWORD_OPS))
- return 0;
- ++z;
- ++x;
- }
- return 1;
- }
- int _m128i_equals_sbyte(__m128i *x, __m128i *z)
- {
- while (*(char *)x && *(char *)z)
- {
- if (_mm_cmpistri(*x, *z, _SIDD_CMP_EQUAL_EACH | _SIDD_SBYTE_OPS))
- return 0;
- ++z;
- ++x;
- }
- return 1;
- }
- int _m128i_equals_ubyte(__m128i *x, __m128i *z)
- {
- while (*(char *)x && *(char *)z)
- {
- if (_mm_cmpistri(*x, *z, _SIDD_CMP_EQUAL_EACH | _SIDD_UBYTE_OPS))
- return 0;
- z++;
- x++;
- }
- return 1;
- }
- /*
- void set_icon()
- {
- HICON icon = LoadImageA(NULL, "F:\\ok.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
- HMODULE kernelModule = LoadLibraryA("kernel32.dll");
- DWORD(__stdcall *setConsoleIcon)(HICON) = (DWORD(__stdcall *)(HICON))GetProcAddress(kernelModule, "SetConsoleIcon");
- if (setConsoleIcon == NULL)
- {
- DWORD error = GetLastError();
- printf("Couldn't set icon because of error %u.\n", error);
- return;
- }
- setConsoleIcon(icon);
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement