Home | History | Annotate | Download | only in machine
      1 /*	$OpenBSD: endian.h,v 1.3 2005/12/13 00:35:23 millert Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2010 The Android Open Source Project
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  *  * Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  *  * Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in
     14  *    the documentation and/or other materials provided with the
     15  *    distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     21  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     27  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #ifndef _ARM_ENDIAN_H_
     32 #define _ARM_ENDIAN_H_
     33 
     34 #ifdef __GNUC__
     35 
     36 /* According to RealView Assembler User's Guide, REV and REV16 are available
     37  * in Thumb code and 16-bit instructions when used in Thumb-2 code.
     38  *
     39  * REV Rd, Rm
     40  *   Rd and Rm must both be Lo registers.
     41  *
     42  * REV16 Rd, Rm
     43  *   Rd and Rm must both be Lo registers.
     44  *
     45  * The +l constraint takes care of this without constraining us in ARM mode.
     46  */
     47 #define __swap16md(x) ({                                        \
     48     register u_int16_t _x = (x);                                \
     49     __asm__ __volatile__("rev16 %0, %0" : "+l" (_x));           \
     50     _x;                                                         \
     51 })
     52 
     53 #define __swap32md(x) ({                                        \
     54     register u_int32_t _x = (x);                                \
     55     __asm__ __volatile__("rev %0, %0" : "+l" (_x));             \
     56     _x;                                                         \
     57 })
     58 
     59 #define __swap64md(x) ({                                        \
     60     u_int64_t _swap64md_x = (x);                                \
     61     (u_int64_t) __swap32md(_swap64md_x >> 32) |                 \
     62         (u_int64_t) __swap32md(_swap64md_x & 0xffffffff) << 32; \
     63 })
     64 
     65 /* Tell sys/endian.h we have MD variants of the swap macros.  */
     66 #define MD_SWAP
     67 
     68 #endif  /* __GNUC__ */
     69 
     70 #if defined(__ARMEB__)
     71 #define _BYTE_ORDER _BIG_ENDIAN
     72 #else
     73 #define _BYTE_ORDER _LITTLE_ENDIAN
     74 #endif
     75 #define __STRICT_ALIGNMENT
     76 #include <sys/types.h>
     77 #include <sys/endian.h>
     78 
     79 #endif  /* !_ARM_ENDIAN_H_ */
     80