ReactOS 0.4.15-dev-5666-gc548b97
swap_bytes_impl.h
Go to the documentation of this file.
1/*
2 swap_bytes: Swap byte order of samples in a buffer.
3
4 copyright 2018 by the mpg123 project
5 licensed under the terms of the LGPL 2.1
6 see COPYING and AUTHORS files in distribution or http://mpg123.org
7
8 initially written by Thomas Orgis
9
10 This is C source to include in your code to get the function named
11 swap_bytes. It is serves intentional duplication in libmpg123 and
12 libsyn123 to avoid introducing a dependency between them. This function
13 is too small for that.
14*/
15
16/* Other headers are already included! */
17
18/* Optionally use platform-specific byteswap macros. */
19#ifdef HAVE_BYTESWAP_H
20#include <byteswap.h>
21#endif
22
23/* Plain stupid swapping of elements in a byte array. */
24/* This is the fallback when there is no native bswap macro. */
25#define SWAP(a,b) tmp = p[a]; p[a] = p[b]; p[b] = tmp;
26
27/* Convert samplecount elements of samplesize bytes each in buffer buf. */
28static void swap_bytes(void *buf, size_t samplesize, size_t samplecount)
29{
30 unsigned char *p = buf;
31 unsigned char *pend = (unsigned char*)buf+samplesize*samplecount;
32 unsigned char tmp;
33
34 if(samplesize < 2)
35 return;
36 switch(samplesize)
37 {
38 case 2: /* AB -> BA */
39#ifdef HAVE_BYTESWAP_H
40 {
41 uint16_t* pp = (uint16_t*)p;
42 for(; pp<(uint16_t*)pend; ++pp)
43 *pp = bswap_16(*pp);
44 }
45#else
46 for(; p<pend; p+=2)
47 {
48 SWAP(0,1)
49 }
50#endif
51 break;
52 case 3: /* ABC -> CBA */
53 for(; p<pend; p+=3)
54 {
55 SWAP(0,2)
56 }
57 break;
58 case 4: /* ABCD -> DCBA */
59#ifdef HAVE_BYTESWAP_H
60 {
61 uint32_t* pp = (uint32_t*)p;
62 for(; pp<(uint32_t*)pend; ++pp)
63 *pp = bswap_32(*pp);
64 }
65#else
66 for(; p<pend; p+=4)
67 {
68 SWAP(0,3)
69 SWAP(1,2)
70 }
71#endif
72 break;
73 case 8: /* ABCDEFGH -> HGFEDCBA */
74#ifdef HAVE_BYTESWAP_H
75 {
76 uint64_t* pp = (uint64_t*)p;
77 for(; pp<(uint64_t*)pend; ++pp)
78 *pp = bswap_64(*pp);
79 }
80#else
81 for(; p<pend; p+=8)
82 {
83 SWAP(0,7)
84 SWAP(1,6)
85 SWAP(2,5)
86 SWAP(3,4)
87 }
88#endif
89 break;
90 /* All the weird choices with the full nested loop. */
91 default:
92 for(; p<pend; p+=samplesize)
93 {
94 size_t j;
95 for(j=0; j<samplesize/2; ++j)
96 {
97 SWAP(j, samplesize-j-1)
98 }
99 }
100 }
101}
102
103#undef SWAP
unsigned short int uint16_t
Definition: acefiex.h:54
static UInt64_t bswap_64(x) UInt64_t x
#define bswap_32(x)
Definition: byteswap.h:34
#define bswap_16(x)
Definition: byteswap.h:31
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLfloat GLfloat p
Definition: glext.h:8902
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
static void swap_bytes(void *buf, size_t samplesize, size_t samplecount)
#define SWAP(a, b)