Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #41260
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Subject | Re: Union of bitfields and larger type on ARM |
| Newsgroups | comp.lang.c++, comp.dsp, sci.electronics.design |
| References | <If2zy.23937$Nf2.15642@fx14.iad> <nanf3n$t2j$1@dont-email.me> <napt6d$1us$1@dont-email.me> |
| Message-ID | <SX%zy.610$LP.435@fx16.iad> (permalink) |
| Organization | UsenetServer - www.usenetserver.com |
| Date | 2016-02-26 17:29 +0000 |
Cross-posted to 3 groups.
David Brown <david.brown@hesbynett.no> writes:
>On 25/02/16 19:05, Les Cargill wrote:
>> bitrex wrote:
>>> Does anyone know if the following "naughty" (implementation dependent)
>>> construct would work "correctly" on the ARM Cortex M4, compiled with
>>> arm-gcc-embedded?
>>>
>>> typedef union {
>>> uint32_t phase_accumulator_full;
>>> struct {
>>> uint8_t integer_high : 8,
>>> integer_low : 8,
>>> frac_high : 8,
>>> frac_low : 8;
>>>
>>> };
>>> } phase_accumulator;
>>
>>
>> If it doesn't, fix it. Endianness is more or less undefined behavior in
>> 'C' so you have to test it.
>
>No, endianness is not remotely "undefined behaviour" - it is
>/implementation/ defined behaviour. There is a huge difference.
We have software that is built for both big-endian and little-endian
hosts (and more interestingly, it must internally simulate both big-endian
and little-endian processors).
We have this in one of the project header files.
#include <endian.h>
#ifndef __BYTE_ORDER
#if defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
#define __BYTE_ORDER __BIG_ENDIAN
#elif !defined(__BIG_ENDIAN) && defined(__LITTLE_ENDIAN)
#define __BYTE_ORDER __LITTLE_ENDIAN
#define __BIG_ENDIAN 4321
#elif !defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
#define __BIG_ENDIAN 4321
#define __BYTE_ORDER __BIG_ENDIAN
#else
#error Unable to determine Endian mode
#endif
#endif
#if !defined(htobe16)
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define htobe16(x) __bswap_16 (x)
# define htole16(x) (x)
# define be16toh(x) __bswap_16 (x)
# define le16toh(x) (x)
# define htobe32(x) __bswap_32 (x)
# define htole32(x) (x)
# define be32toh(x) __bswap_32 (x)
# define le32toh(x) (x)
# define htobe64(x) __bswap_64 (x)
# define htole64(x) (x)
# define be64toh(x) __bswap_64 (x)
# define le64toh(x) (x)
# else
# define htobe16(x) (x)
# define htole16(x) __bswap_16 (x)
# define be16toh(x) (x)
# define le16toh(x) __bswap_16 (x)
# define htobe32(x) (x)
# define htole32(x) __bswap_32 (x)
# define be32toh(x) (x)
# define le32toh(x) __bswap_32 (x)
# define htobe64(x) (x)
# define htole64(x) __bswap_64 (x)
# define be64toh(x) (x)
# define le64toh(x) __bswap_64 (x)
# endif
#endif
Then use hto{bl}eXX and {bl}e64tohXX in
the code without the need for local ifdefs.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Union of bitfields and larger type on ARM bitrex <bitrex@de.lete.earthlink.net> - 2016-02-23 14:17 -0500
Re: Union of bitfields and larger type on ARM John Devereux <john@devereux.me.uk> - 2016-02-23 19:36 +0000
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-23 22:34 +0100
Re: Union of bitfields and larger type on ARM bitrex <bitrex@de.lete.earthlink.net> - 2016-02-23 16:45 -0500
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-23 23:21 +0100
Re: Union of bitfields and larger type on ARM spope33@speedymail.org (Steve Pope) - 2016-02-25 00:06 +0000
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-25 01:29 +0100
Re: Union of bitfields and larger type on ARM Tim Wescott <seemywebsite@myfooter.really> - 2016-02-23 15:53 -0600
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-23 23:48 +0100
Re: Union of bitfields and larger type on ARM Christian Gollwitzer <auriocus@gmx.de> - 2016-02-24 09:44 +0100
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-24 10:39 +0100
Re: Union of bitfields and larger type on ARM Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-02-24 09:54 +0200
Re: Union of bitfields and larger type on ARM M Philbrook <jamie_ka1lpa@charter.net> - 2016-02-23 20:39 -0500
Re: Union of bitfields and larger type on ARM Ian Collins <ian-news@hotmail.com> - 2016-02-24 14:48 +1300
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-24 10:41 +0100
Re: Union of bitfields and larger type on ARM Les Cargill <lcargill99@comcast.com> - 2016-02-25 12:05 -0600
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-26 17:09 +0100
Re: Union of bitfields and larger type on ARM scott@slp53.sl.home (Scott Lurndal) - 2016-02-26 17:29 +0000
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-26 18:42 +0100
Re: Union of bitfields and larger type on ARM Les Cargill <lcargill99@comcast.com> - 2016-02-26 19:07 -0600
Re: Union of bitfields and larger type on ARM Allan Herriman <allanherriman@hotmail.com> - 2016-02-27 08:58 +0000
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-27 14:37 +0100
Re: Union of bitfields and larger type on ARM Öö Tiib <ootiib@hot.ee> - 2016-02-27 07:21 -0800
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-27 16:34 +0100
Re: Union of bitfields and larger type on ARM Öö Tiib <ootiib@hot.ee> - 2016-02-27 10:14 -0800
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-28 15:26 +0100
Re: Union of bitfields and larger type on ARM Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-27 16:40 -0500
Re: Union of bitfields and larger type on ARM Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-28 00:47 +0200
Re: Union of bitfields and larger type on ARM Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-27 18:21 -0500
Re: Union of bitfields and larger type on ARM Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-28 01:33 +0200
Re: Union of bitfields and larger type on ARM Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-27 20:15 -0500
csiph-web