Skip to content

Instantly share code, notes, and snippets.

@airekans
Created March 8, 2015 04:19
Show Gist options
  • Save airekans/86a2f037c623b5e36ee7 to your computer and use it in GitHub Desktop.
Save airekans/86a2f037c623b5e36ee7 to your computer and use it in GitHub Desktop.
A test program for vector extension in gcc. The sse version is not correct, just for testing
#include <iostream>
#include <algorithm>
using namespace std;
void int_to_str(unsigned int num, char* buffer, unsigned int size)
{
static const char* digits = "0123456789";
char* buf_ptr = buffer;
do
{
*(buf_ptr++) = digits[num % 10];
num /= 10;
} while (num > 0);
*buf_ptr = '\0';
reverse(buffer, buf_ptr);
}
typedef unsigned int v4ui __attribute__ ((vector_size (16)));
void int_to_str_sse(unsigned int num, char* buffer, unsigned int size)
{
static const char* digits = "0123456789";
char* buf_ptr = buffer;
v4ui a, b = {1, 10, 100, 1000}, c;
do
{
a[0] = num, a[1] = num, a[2] = num, a[3] = num;
c = (a / b) % 10;
*(buf_ptr++) = digits[c[0]];
*(buf_ptr++) = digits[c[1]];
*(buf_ptr++) = digits[c[2]];
*(buf_ptr++) = digits[c[3]];
num /= 10000;
} while (num > 0);
*buf_ptr = '\0';
reverse(buffer, buf_ptr);
}
int main(int argc, char** argv)
{
cout << "sizeof(int) " << sizeof(unsigned int) << endl;
int times = 100000;
int which = 1;
unsigned int num = 12345678;
if (argc > 1)
{
times = atoi(argv[1]);
}
if (argc > 2)
{
which = atoi(argv[2]);
}
if (argc > 3)
{
num = atoi(argv[3]);
}
char buffer[1024] = "";
char buffer1[1024] = "";
switch (which)
{
case 1:
for (int i = 0; i < times; ++i)
{
int_to_str(num, buffer, 1024);
}
cout << "result: " << buffer << endl;
break;
case 2:
for (int i = 0; i < times; ++i)
{
int_to_str_sse(num, buffer1, 1024);
}
cout << "result: " << buffer1 << endl;
break;
default:
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment