diff --git a/miosix/arch/arm7_lpc2000/common/interfaces-impl/endianness_impl.h b/miosix/arch/arm7_lpc2000/common/interfaces-impl/endianness_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..04a65f604e46199f6c41603bcbeba4481fe06e4f --- /dev/null +++ b/miosix/arch/arm7_lpc2000/common/interfaces-impl/endianness_impl.h @@ -0,0 +1,76 @@ +/*************************************************************************** + * Copyright (C) 2011 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#ifndef ENDIANNESS_IMPL_H +#define ENDIANNESS_IMPL_H + +//This target is little endian +#define MIOSIX_LITTLE_ENDIAN + +#ifdef __cplusplus +#define __MIOSIX_INLINE inline +#else //__cplusplus +#define __MIOSIX_INLINE static inline +#endif //__cplusplus + + +__MIOSIX_INLINE unsigned short swapBytes16(unsigned short x) +{ + return (x>>8) | (x<<8); +} + +__MIOSIX_INLINE unsigned int swapBytes32(unsigned int x) +{ + #ifdef __GNUC__ + return __builtin_bswap32(x); + #else //__GNUC__ + return ( x>>24) | + ((x<< 8) & 0x00ff0000) | + ((x>> 8) & 0x0000ff00) | + ( x<<24); + #endif //__GNUC__ +} + +__MIOSIX_INLINE unsigned long long swapBytes64(unsigned long long x) +{ + #ifdef __GNUC__ + return __builtin_bswap64(x); + #else //__GNUC__ + return ( x>>56) | + ((x<<40) & 0x00ff000000000000ull) | + ((x<<24) & 0x0000ff0000000000ull) | + ((x<< 8) & 0x000000ff00000000ull) | + ((x>> 8) & 0x00000000ff000000ull) | + ((x>>24) & 0x0000000000ff0000ull) | + ((x>>40) & 0x000000000000ff00ull) | + ( x<<56); + #endif //__GNUC__ +} + +#undef __MIOSIX_INLINE + +#endif //ENDIANNESS_IMPL_H diff --git a/miosix/arch/cortexM3_stm32/common/interfaces-impl/endianness_impl.h b/miosix/arch/cortexM3_stm32/common/interfaces-impl/endianness_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..f34043ea305e1a6a28fca8513310bad687e7055f --- /dev/null +++ b/miosix/arch/cortexM3_stm32/common/interfaces-impl/endianness_impl.h @@ -0,0 +1,92 @@ +/*************************************************************************** + * Copyright (C) 2011 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#ifndef ENDIANNESS_IMPL_H +#define ENDIANNESS_IMPL_H + +//This target is little endian +#define MIOSIX_LITTLE_ENDIAN + +#ifdef __cplusplus +#define __MIOSIX_INLINE inline +#else //__cplusplus +#define __MIOSIX_INLINE static inline +#endif //__cplusplus + +__MIOSIX_INLINE unsigned short swapBytes16(unsigned short x) +{ + //It's kind of a shame that GCC can't automatically make use of + //instructions like rev and rev16 to do byte swapping. + //Moreover, while for 32 and 64 bit integers it has builtins, for 16 bit + //we're forced to use inline asm. + #ifdef __GNUC__ + if(!__builtin_constant_p(x)) + { + unsigned short y; + asm("rev16 %0, %1":"=r"(y):"r"(x)); + return y; + } else { + //It gets worse: if value is constant inlining assembler disables + //contant folding, wtf... + return (x>>8) | (x<<8); + } + #else + return (x>>8) | (x<<8); + #endif +} + +__MIOSIX_INLINE unsigned int swapBytes32(unsigned int x) +{ + #ifdef __GNUC__ + return __builtin_bswap32(x); + #else + return ( x>>24) | + ((x<< 8) & 0x00ff0000) | + ((x>> 8) & 0x0000ff00) | + ( x<<24); + #endif +} + +__MIOSIX_INLINE unsigned long long swapBytes64(unsigned long long x) +{ + #ifdef __GNUC__ + return __builtin_bswap64(x); + #else + return ( x>>56) | + ((x<<40) & 0x00ff000000000000ull) | + ((x<<24) & 0x0000ff0000000000ull) | + ((x<< 8) & 0x000000ff00000000ull) | + ((x>> 8) & 0x00000000ff000000ull) | + ((x>>24) & 0x0000000000ff0000ull) | + ((x>>40) & 0x000000000000ff00ull) | + ( x<<56); + #endif +} + +#undef __MIOSIX_INLINE + +#endif //ENDIANNESS_IMPL_H diff --git a/miosix/arch/cortexM3_stm32/stm32f103ze_stm3210e-eval/core/stage_1_boot.cpp b/miosix/arch/cortexM3_stm32/stm32f103ze_stm3210e-eval/core/stage_1_boot.cpp index 55f1073ce2eb14798e486912224493437656023a..56116fdcf7d06c49fddf842cde3b1bdef87d4f16 100644 --- a/miosix/arch/cortexM3_stm32/stm32f103ze_stm3210e-eval/core/stage_1_boot.cpp +++ b/miosix/arch/cortexM3_stm32/stm32f103ze_stm3210e-eval/core/stage_1_boot.cpp @@ -76,6 +76,10 @@ void program_startup() unsigned char *bss_end=&_bss_end; #ifndef __CODE_IN_XRAM memcpy(data, etext, edata-data); + #else //__CODE_IN_XRAM + (void)etext; //Avoid unused variable warning + (void)data; + (void)edata; #endif //__CODE_IN_XRAM memset(bss_start, 0, bss_end-bss_start); diff --git a/miosix/config/miosix_settings.h b/miosix/config/miosix_settings.h index 002c0aeee7a694e2584e93fd6fbe6bd320bf0bfd..d451d2b913f9e4f66d6c759f73e4f21f9adfccaf 100644 --- a/miosix/config/miosix_settings.h +++ b/miosix/config/miosix_settings.h @@ -50,9 +50,13 @@ namespace miosix { // Scheduler options // -/// \def SCHED_TYPE_* -/// Choose scheduler type -/// Uncomment one #define only +/// \def SCHED_TYPE_PRIORITY +/// If uncommented selects the priority scheduler +/// \def SCHED_TYPE_CONTROL_BASED +/// If uncommented selects the control based scheduler +/// \def SCHED_TYPE_EDF +///If uncommented selects the EDF scheduler +//Uncomment only *one* of those #define SCHED_TYPE_PRIORITY //#define SCHED_TYPE_CONTROL_BASED diff --git a/miosix/doc/textdoc/Changelog.txt b/miosix/doc/textdoc/Changelog.txt index 5d5e4bd9eb8596236dbb04971e2d8f7464535898..92f007706aaa81dcc2f22540928afc6917bb2323 100644 --- a/miosix/doc/textdoc/Changelog.txt +++ b/miosix/doc/textdoc/Changelog.txt @@ -1,6 +1,9 @@ Changelog for Miosix np embedded OS v1.59 +- Added tests to testsuite for endianness.h +- Added interfaces/endianness.h with optimized functions to convert to/from + lille/big endian, as well as byte swapping. - Improved boot speed by replacing for loops to initialize .data, .bss and to initialize the stack of threads with memset/memcpy. - Improved getMiosixVersion(), new format contains more information, including diff --git a/miosix/interfaces/endianness.h b/miosix/interfaces/endianness.h new file mode 100644 index 0000000000000000000000000000000000000000..7f6c66112275287a9935e04f2f6201e41b0492f0 --- /dev/null +++ b/miosix/interfaces/endianness.h @@ -0,0 +1,147 @@ +/*************************************************************************** + * Copyright (C) 2011 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ + +#include "interfaces-impl/endianness_impl.h" + +#ifndef ENDIANNESS_H +#define ENDIANNESS_H + +/** + * \addtogroup Interfaces + * \{ + */ + +/** + * \file endianness.h + * This file contains optimized functions to convert data from the system's + * endianness to little or big endian, as well as to perform byte swapping. + */ + +// Implementation of these functions is in endianness_impl.h + +#ifdef __cplusplus +#define __MIOSIX_INLINE inline +#else //__cplusplus +#define __MIOSIX_INLINE static inline +#endif //__cplusplus + +/** + * \fn inline unsigned short swapBytes16(unsigned short x) + * \param x an short int + * \return the same short with its bytes swapped + */ +__MIOSIX_INLINE unsigned short swapBytes16(unsigned short x); + +/** + * \fn inline unsigned int swapBytes32(unsigned int x) + * \param x an int + * \return the same int with its bytes swapped + */ +__MIOSIX_INLINE unsigned int swapBytes32(unsigned int x); + +/** + * \fn inline unsigned long long swapBytes64(unsigned long long x) + * \param x a long long + * \return the same long long with its bytes swapped + */ +__MIOSIX_INLINE unsigned long long swapBytes64(unsigned long long x); + +#undef __MIOSIX_INLINE + +/** + * \def toLittleEndian16(x) + * Convert a (signed or unsigned) short int from the system representation + * to little endian + * \param x value to convert + * \return value converted to little endian + * + * \def toLittleEndian32(x) + * Convert a (signed or unsigned) int from the system representation + * to little endian + * \param x value to convert + * \return value converted to little endian + * + * \def toLittleEndian64(x) + * Convert a (signed or unsigned) long long from the system representation + * to little endian + * \param x value to convert + * \return value converted to little endian + * + * \def toBigEndian16(x) + * Convert a (signed or unsigned) short int from the system representation + * to big endian + * \param x value to convert + * \return value converted to big endian + * + * \def toBigEndian32(x) + * Convert a (signed or unsigned) int from the system representation + * to big endian + * \param x value to convert + * \return value converted to big endian + * + * \def toBigEndian64(x) + * Convert a (signed or unsigned) long long from the system representation + * to big endian + * \param x value to convert + * \return value converted to big endian + */ + +#ifdef MIOSIX_LITTLE_ENDIAN +#define toLittleEndian16(x) (x) +#define toLittleEndian32(x) (x) +#define toLittleEndian64(x) (x) +#define fromLittleEndian16(x) (x) +#define fromLittleEndian32(x) (x) +#define fromLittleEndian64(x) (x) +#define toBigEndian16(x) swapBytes16(x) +#define toBigEndian32(x) swapBytes32(x) +#define toBigEndian64(x) swapBytes64(x) +#define fromBigEndian16(x) swapBytes16(x) +#define fromBigEndian32(x) swapBytes32(x) +#define fromBigEndian64(x) swapBytes64(x) +#elif defined(MIOSIX_BIG_ENDIAN) +#define toLittleEndian16(x) swapBytes16(x) +#define toLittleEndian32(x) swapBytes32(x) +#define toLittleEndian64(x) swapBytes64(x) +#define fromLittleEndian16(x) swapBytes16(x) +#define fromLittleEndian32(x) swapBytes32(x) +#define fromLittleEndian64(x) swapBytes64(x) +#define toBigEndian16(x) (x) +#define toBigEndian32(x) (x) +#define toBigEndian64(x) (x) +#define fromBigEndian16(x) (x) +#define fromBigEndian32(x) (x) +#define fromBigEndian64(x) (x) +#else +#error "endianness_impl.h does not define endianness" +#endif + +/** + * \} + */ + +#endif //ENDIANNESS_H diff --git a/miosix/kernel/scheduler/control/control_scheduler_types.h b/miosix/kernel/scheduler/control/control_scheduler_types.h index 9a7d5e90b4bef451dbd4febceb1c2088cf535da5..c21a34ddd15e488f9e284ead28464caf0f797ee5 100644 --- a/miosix/kernel/scheduler/control/control_scheduler_types.h +++ b/miosix/kernel/scheduler/control/control_scheduler_types.h @@ -50,7 +50,7 @@ class ControlSchedulerPriority public: /** * Constructor. Not explicit for backward compatibility. - * \param priority, the desired priority value. + * \param priority the desired priority value. */ ControlSchedulerPriority(short int priority): priority(priority) {} diff --git a/miosix/kernel/scheduler/edf/edf_scheduler_types.h b/miosix/kernel/scheduler/edf/edf_scheduler_types.h index a9c952528262b500fce20f5c2667a14eaf99bc72..a2e4fc3aec241f02bc15eba3c7ec0ebb0bd08636 100644 --- a/miosix/kernel/scheduler/edf/edf_scheduler_types.h +++ b/miosix/kernel/scheduler/edf/edf_scheduler_types.h @@ -47,7 +47,7 @@ class EDFSchedulerPriority public: /** * Constructor. Not explicit for backward compatibility. - * \param priority, the desired priority value. + * \param deadline the thread deadline. */ EDFSchedulerPriority(long long deadline): deadline(deadline) {} diff --git a/miosix/kernel/scheduler/priority/priority_scheduler_types.h b/miosix/kernel/scheduler/priority/priority_scheduler_types.h index 15aff690811384cb72a2ac662e24aa9e2cc2c7b5..e58880c9b393ce242ba0f4a7be0eeaa033f6e932 100644 --- a/miosix/kernel/scheduler/priority/priority_scheduler_types.h +++ b/miosix/kernel/scheduler/priority/priority_scheduler_types.h @@ -47,9 +47,9 @@ class PrioritySchedulerPriority public: /** * Constructor. Not explicit for backward compatibility. - * \param priority, the desired priority value. + * \param priority the desired priority value. */ - PrioritySchedulerPriority(short int priority): priority(priority){} + PrioritySchedulerPriority(short int priority): priority(priority) {} /** * Default constructor. diff --git a/miosix/util/software_i2c.h b/miosix/util/software_i2c.h index cf0a06351015fb6b8ce67dce31ed3cdec6b64268..b940d8d9c69345b17d28b90e56fcfb8d535073c3 100644 --- a/miosix/util/software_i2c.h +++ b/miosix/util/software_i2c.h @@ -1,3 +1,29 @@ +/*************************************************************************** + * Copyright (C) 2011 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ #ifndef SOFTWARE_I2C_H #define SOFTWARE_I2C_H diff --git a/miosix/util/software_spi.h b/miosix/util/software_spi.h index 5d8c58289dfbc1d93b88c0f77f0ad244f339936c..cf2fc84d8ea7a7d884f48a32911b28b926ddb672 100644 --- a/miosix/util/software_spi.h +++ b/miosix/util/software_spi.h @@ -1,3 +1,29 @@ +/*************************************************************************** + * Copyright (C) 2011 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ #ifndef SOFTWARE_SPI_H #define SOFTWARE_SPI_H @@ -50,7 +76,7 @@ public: * \param data to send * \return data received */ - //static unsigned int sendRecvLong(unsigned int data); + static unsigned int sendRecvLong(unsigned int data); /** * Pull CE low, indicating transmission start. @@ -103,6 +129,26 @@ unsigned short SoftwareSPI<SI,SO,SCK,CE,numNops>:: return result; } +template<typename SI, typename SO, typename SCK, typename CE, unsigned numNops> +unsigned int SoftwareSPI<SI,SO,SCK,CE,numNops>:: + sendRecvLong(unsigned int data) +{ + unsigned int result; + for(int i=0;i<32;i++) + { + if(data & 0x80000000) SO::high(); + SCK::high(); + data<<=1; + result<<=1; + if(SI::value()) result |= 0x1; + for(int j=0;j<numNops;j++) asm volatile("nop"); + SCK::low(); + for(int j=0;j<numNops;j++) asm volatile("nop"); + SO::low(); + } + return result; +} + } //namespace miosix #endif //SOFTWARE_SPI_H diff --git a/miosix/util/version.cpp b/miosix/util/version.cpp index 10b7bce9db02cd9aeeabed8e2b0a07e72b377c0c..f59c4e058a3f6389a10437b90ffb3ef9adce7b4b 100644 --- a/miosix/util/version.cpp +++ b/miosix/util/version.cpp @@ -1,3 +1,29 @@ +/*************************************************************************** + * Copyright (C) 2010, 2011 by Terraneo Federico * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * As a special exception, if other files instantiate templates or use * + * macros or inline functions from this file, or you compile this file * + * and link it with other works to produce a work based on this file, * + * this file does not by itself cause the resulting work to be covered * + * by the GNU General Public License. However the source code for this * + * file must still be made available in accordance with the GNU General * + * Public License. This exception does not invalidate any other reasons * + * why a work based on this file might be covered by the GNU General * + * Public License. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see <http://www.gnu.org/licenses/> * + ***************************************************************************/ namespace miosix { diff --git a/miosix/util/version.h b/miosix/util/version.h index 96d4b02d74152fb7c89fe620a963b94f137dcd2d..1dbd44415e7028d63a849ef76e2b8156f9d0f952 100644 --- a/miosix/util/version.h +++ b/miosix/util/version.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2010 by Terraneo Federico * + * Copyright (C) 2010, 2011 by Terraneo Federico * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * diff --git a/miosix_np_2/nbproject/configurations.xml b/miosix_np_2/nbproject/configurations.xml index 9154f532c464c2b61534d3298140e605b9ab4d4c..ec572556379602de6d9ebbe16ccd7f5a87d7c7b8 100644 --- a/miosix_np_2/nbproject/configurations.xml +++ b/miosix_np_2/nbproject/configurations.xml @@ -6,6 +6,9 @@ <df name="arch"> <df name="arm7_lpc2000"> <df name="common"> + <df name="interfaces-impl"> + <in>endianness_impl.h</in> + </df> <in>arch_settings.h</in> </df> <df name="lpc2138_miosix_board"> @@ -46,12 +49,15 @@ <in>interrupts.h</in> </df> <df name="drivers"> + <in>dcc.cpp</in> + <in>dcc.h</in> <in>serial.cpp</in> <in>serial.h</in> </df> <df name="interfaces-impl"> <in>arch_registers_impl.h</in> <in>delays.cpp</in> + <in>endianness_impl.h</in> <in>gpio_impl.h</in> <in>portability.cpp</in> <in>portability_impl.h</in> @@ -105,6 +111,7 @@ <in>console.h</in> <in>delays.h</in> <in>disk.h</in> + <in>endianness.h</in> <in>gpio.h</in> <in>portability.h</in> </df> diff --git a/testsuite.cpp b/testsuite.cpp index b269cd82665afeff1fc24fd9f8d80ce52ac3da59..2eff786fa9d2d1f5f246891f2f76333fadaea383 100644 --- a/testsuite.cpp +++ b/testsuite.cpp @@ -44,6 +44,7 @@ #include "config/miosix_settings.h" #include "interfaces/console.h" #include "board_settings.h" +#include "interfaces/endianness.h" using namespace miosix; @@ -77,6 +78,7 @@ static void test_14(); static void test_15(); static void test_16(); static void test_17(); +static void test_18(); //Filesystem test functions #ifdef WITH_FILESYSTEM static void fs_test_1(); @@ -133,6 +135,7 @@ int main() test_15(); test_16(); test_17(); + test_18(); ledOff(); Thread::sleep(500);//Ensure all threads are deleted. @@ -2241,6 +2244,92 @@ static void test_17() pass(); } +// +// Test 18 +// +/* +tests: +endianness API +*/ + +void __attribute__((noinline)) check16(unsigned short a, unsigned short b) +{ + if(swapBytes16(a)!=b) fail("swapBytes16"); +} + +void __attribute__((noinline)) check32(unsigned int a, unsigned int b) +{ + if(swapBytes32(a)!=b) fail("swapBytes32"); +} + +void __attribute__((noinline)) check64(unsigned long long a, + unsigned long long b) +{ + if(swapBytes64(a)!=b) fail("swapBytes64"); +} + +static void test_18() +{ + test_name("endianness"); + if(swapBytes16(0x1234)!=0x3412 || + swapBytes16(0x55aa)!=0xaa55) fail("swapBytes16"); + if(swapBytes32(0x12345678)!=0x78563412 || + swapBytes32(0x55aa00ff)!=0xff00aa55) fail("swapBytes32"); + if(swapBytes64(0x0123456789abcdefull)!=0xefcdab8967452301ull || + swapBytes64(0x55aa00ffcc33ab56ull)!=0x56ab33ccff00aa55ull) + fail("swapBytes64"); + check16(0x1234,0x3412); + check16(0x55aa,0xaa55); + check32(0x12345678,0x78563412); + check32(0x55aa00ff,0xff00aa55); + check64(0x0123456789abcdefull,0xefcdab8967452301ull); + check64(0x55aa00ffcc33ab56ull,0x56ab33ccff00aa55ull); + union { + short a; + unsigned char b[2]; + } test; + test.a=0x1234; + if(test.b[0]==0x12) + { + //Runtime check says our CPU is big endian + if(toBigEndian16(0x0123)!=0x0123 || + toBigEndian32(0x01234567)!=0x01234567 || + toBigEndian64(0x0123456789abcdef)!=0x0123456789abcdef) + fail("toBigEndian"); + if(fromBigEndian16(0x0123)!=0x0123 || + fromBigEndian32(0x01234567)!=0x01234567 || + fromBigEndian64(0x0123456789abcdef)!=0x0123456789abcdef) + fail("fromBigEndian"); + if(toLittleEndian16(0x0123)!=0x2301 || + toLittleEndian32(0x01234567)!=0x67452301 || + toLittleEndian64(0x0123456789abcdef)!=0xefcdab8967452301) + fail("toLittleEndian"); + if(fromLittleEndian16(0x0123)!=0x2301 || + fromLittleEndian32(0x01234567)!=0x67452301 || + fromLittleEndian64(0x0123456789abcdef)!=0xefcdab8967452301) + fail("fromLittleEndian"); + } else { + //Runtime check says our CPU is little endian + if(toLittleEndian16(0x0123)!=0x0123 || + toLittleEndian32(0x01234567)!=0x01234567 || + toLittleEndian64(0x0123456789abcdef)!=0x0123456789abcdef) + fail("toLittleEndian"); + if(fromLittleEndian16(0x0123)!=0x0123 || + fromLittleEndian32(0x01234567)!=0x01234567 || + fromLittleEndian64(0x0123456789abcdef)!=0x0123456789abcdef) + fail("fromLittleEndian"); + if(toBigEndian16(0x0123)!=0x2301 || + toBigEndian32(0x01234567)!=0x67452301 || + toBigEndian64(0x0123456789abcdef)!=0xefcdab8967452301) + fail("toBigEndian"); + if(fromBigEndian16(0x0123)!=0x2301 || + fromBigEndian32(0x01234567)!=0x67452301 || + fromBigEndian64(0x0123456789abcdef)!=0xefcdab8967452301) + fail("fromBigEndian"); + } + pass(); +} + #ifdef WITH_FILESYSTEM // // Filesystem test 1