NCEPLIBS-bacio  2.6.0
byteswap.c File Reference
#include <byteswap.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "clib.h"
#include "fast-byteswap.h"

Go to the source code of this file.

Functions

void byteswap_ (char *data, int *nbyte, int *nnum)
 Byteswap. More...
 
int fast_byteswap (void *data, int bytes, size_t count)
 Fast byteswap. More...
 
void fast_byteswap_errors (int flag)
 Set a flag to turn warnings off for non-aligned pointers. More...
 
static int macro_swap_64 (void *data, size_t len)
 Use the GNU macros, which are specialized byteswap ASM instructions. More...
 
static int simple_swap_16 (void *data, size_t len)
 Simple single-value loops. More...
 
static int simple_swap_32 (void *data, size_t len)
 Simple single-value loops. More...
 

Variables

static int send_errors = 1
 If non-zero, warn about non-aligned pointers. More...
 

Detailed Description

This file contains various implementations of fast byteswapping routines. The main entry point, fast_byteswap(), is the only one you should need, and it should be modified to use whatever method is fastest on your architecture.

In all cases, the routines return 1 on success and 0 on failure. They only fail if your data is non-aligned. All routines require that arrays of N-bit data be N-bit aligned. If they are not, an error will be sent to stderr and the routine will return non-zero. To silence the error message, call fast_byteswap_errors(0).

Author
Dexin Zhang, Jun Wang, Ed Hartnett

Definition in file byteswap.c.

Function Documentation

◆ byteswap_()

void byteswap_ ( char *  data,
int *  nbyte,
int *  nnum 
)

Byteswap.

Parameters
dataData
nbyteNumber of bytes.
nnumNNUM
Author
Dexin Zhang, Jun Wang

Definition at line 173 of file byteswap.c.

References fast_byteswap().

◆ fast_byteswap()

int fast_byteswap ( void *  data,
int  bytes,
size_t  count 
)

Fast byteswap.

Parameters
datadata
bytesNumber of bytes
countCount.
Returns
0 for error, 1 otherwise.
Author
Dexin Zhang, Jun Wang

Definition at line 146 of file byteswap.c.

References macro_swap_64(), simple_swap_16(), and simple_swap_32().

Referenced by byteswap_().

◆ fast_byteswap_errors()

void fast_byteswap_errors ( int  flag)

Set a flag to turn warnings off for non-aligned pointers.

Parameters
flagSet to 0 to turn off warnings, non-zero to turn them on (the default).
Author
Dexin Zhang, Jun Wang

Definition at line 41 of file byteswap.c.

References send_errors.

◆ macro_swap_64()

static int macro_swap_64 ( void *  data,
size_t  len 
)
static

Use the GNU macros, which are specialized byteswap ASM instructions.

Parameters
datadata
lenLength
Returns
0 for error, 1 otherwise.
Author
Dexin Zhang, Jun Wang

Definition at line 118 of file byteswap.c.

References send_errors.

Referenced by fast_byteswap().

◆ simple_swap_16()

static int simple_swap_16 ( void *  data,
size_t  len 
)
static

Simple single-value loops.

Parameters
datadata
lenLength
Returns
0 for error, 1 otherwise.
Author
Dexin Zhang, Jun Wang

Definition at line 88 of file byteswap.c.

References send_errors.

Referenced by fast_byteswap().

◆ simple_swap_32()

static int simple_swap_32 ( void *  data,
size_t  len 
)
static

Simple single-value loops.

Parameters
datadata
lenLength
Returns
0 for error, 1 otherwise.
Author
Dexin Zhang, Jun Wang

Definition at line 57 of file byteswap.c.

References send_errors.

Referenced by fast_byteswap().

Variable Documentation

◆ send_errors

int send_errors = 1
static

If non-zero, warn about non-aligned pointers.

Definition at line 30 of file byteswap.c.

Referenced by fast_byteswap_errors(), macro_swap_64(), simple_swap_16(), and simple_swap_32().