Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endian conversion using builtin functions [optimization] #217

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 26 additions & 44 deletions src/endianconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,85 +41,67 @@
* POSSIBILITY OF SUCH DAMAGE.
*/


#include <stdint.h>

/* Toggle the 16 bit unsigned integer pointed by *p from little endian to
* big endian */
void memrev16(void *p) {
unsigned char *x = p, t;

t = x[0];
x[0] = x[1];
x[1] = t;
void memrev16(void *p)
{
uint16_t *p16 = (uint16_t *)p;
*p16 = __builtin_bswap16(*p16);
}

/* Toggle the 32 bit unsigned integer pointed by *p from little endian to
* big endian */
void memrev32(void *p) {
unsigned char *x = p, t;

t = x[0];
x[0] = x[3];
x[3] = t;
t = x[1];
x[1] = x[2];
x[2] = t;
void memrev32(void *p)
{
uint32_t *p32 = (uint32_t *)p;
*p32 = __builtin_bswap32(*p32);
}

/* Toggle the 64 bit unsigned integer pointed by *p from little endian to
* big endian */
void memrev64(void *p) {
unsigned char *x = p, t;

t = x[0];
x[0] = x[7];
x[7] = t;
t = x[1];
x[1] = x[6];
x[6] = t;
t = x[2];
x[2] = x[5];
x[5] = t;
t = x[3];
x[3] = x[4];
x[4] = t;
void memrev64(void *p)
{
uint64_t *p64 = (uint64_t *)p;
*p64 = __builtin_bswap64(*p64);
}

uint16_t intrev16(uint16_t v) {
memrev16(&v);
return v;
uint16_t intrev16(uint16_t v)
{
return __builtin_bswap16(v);
}

uint32_t intrev32(uint32_t v) {
memrev32(&v);
return v;
uint32_t intrev32(uint32_t v)
{
return __builtin_bswap32(v);
}

uint64_t intrev64(uint64_t v) {
memrev64(&v);
return v;
uint64_t intrev64(uint64_t v)
{
return __builtin_bswap64(v);
}

#ifdef REDIS_TEST
#include <stdio.h>

#define UNUSED(x) (void)(x)
int endianconvTest(int argc, char *argv[]) {
int endianconvTest(int argc, char *argv[])
{
char buf[32];

UNUSED(argc);
UNUSED(argv);

sprintf(buf,"ciaoroma");
sprintf(buf, "ciaoroma");
memrev16(buf);
printf("%s\n", buf);

sprintf(buf,"ciaoroma");
sprintf(buf, "ciaoroma");
memrev32(buf);
printf("%s\n", buf);

sprintf(buf,"ciaoroma");
sprintf(buf, "ciaoroma");
memrev64(buf);
printf("%s\n", buf);

Expand Down