#include #include #include typedef unsigned int uint32_t; #define UINT32_GLIBC_SWAP(x) ((uint32_t) bswap_32((uint32_t) x)) #define UINT32_GCC_SWAP(x) ((uint32_t) __builtin_bswap32((uint32_t) x)) #define UINT32_SWAP(x) ( (uint32_t) ( ((uint32_t) x >> 24) | ((uint32_t) x << 24) | (((uint32_t) x & 0xFF00) << 8) | ((((uint32_t) x) >> 8) & 0xFF00) ) ) #define UINT32_CAIRO_SWAP(x) (((x & 0x000000ff) << 24) | \ ((x & 0x0000ff00) << 8) | \ ((x & 0x00ff0000) >> 8) | \ ((x) >> 24)) static void swap_4bytes1 (uint32_t *ximage, int len) { int i, j; uint32_t *p = ximage; for (i = 0; i < len; i++) { *p = UINT32_CAIRO_SWAP (*p); p++; } } static void swap_4bytes2 (uint32_t *ximage, int len) { int i, j; uint32_t *p = ximage; for (i = 0; i < len; i++) { *p = UINT32_SWAP (*p); p++; } } static void swap_4bytes3 (uint32_t *ximage, int len) { int i, j; uint32_t *p = ximage; for (i = 0; i < len; i++) { *p = UINT32_GLIBC_SWAP (*p); p++; } } static void swap_4bytes4 (uint32_t *ximage, int len) { int i, j; uint32_t *p = ximage; for (i = 0; i < len; i++) { *p = UINT32_GCC_SWAP (*p); p++; } } void display (struct timeval *start, struct timeval *stop) { printf ("time spent: %d\n", (stop->tv_sec-start->tv_sec)*1000 + (stop->tv_usec-start->tv_usec)/1000); } main () { #define LEN 65535 uint32_t image [LEN]; int i; struct timeval start, stop; printf ("UINT32_GCC_SWAP\n"); gettimeofday (&start, NULL); for (i = 0; i < 16000; i++) swap_4bytes4 (image, LEN); gettimeofday (&stop, NULL); display (&start, &stop); printf ("cairo code\n"); gettimeofday (&start, NULL); for (i = 0; i < 16000; i++) swap_4bytes1 (image, LEN); gettimeofday (&stop, NULL); display (&start, &stop); printf ("UINT32_SWAP\n"); gettimeofday (&start, NULL); for (i = 0; i < 16000; i++) swap_4bytes2 (image, LEN); gettimeofday (&stop, NULL); display (&start, &stop); printf ("UINT32_GLIBC_SWAP\n"); gettimeofday (&start, NULL); for (i = 0; i < 16000; i++) swap_4bytes3 (image, LEN); gettimeofday (&stop, NULL); display (&start, &stop); }