libcdio  0.90
bytesex.h
Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2000, 2004 Herbert Valerio Riedel <hvr@gnu.org>
00003     Copyright (C) 2005, 2008, 2012 Rocky Bernstein <rocky@gnu.org>
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00026 #ifndef CDIO_BYTESEX_H_
00027 #define CDIO_BYTESEX_H_
00028 
00029 #include <cdio/types.h>
00030 #include <cdio/bytesex_asm.h> /* also defines CDIO_INLINE */
00031 #include <cdio/logging.h>
00032 
00034 #define UINT16_SWAP_LE_BE_C(val) ((uint16_t) ( \
00035     (((uint16_t) (val) & (uint16_t) 0x00ffU) << 8) | \
00036     (((uint16_t) (val) & (uint16_t) 0xff00U) >> 8)))
00037 
00039 #define UINT32_SWAP_LE_BE_C(val) ((uint32_t) ( \
00040     (((uint32_t) (val) & (uint32_t) 0x000000ffU) << 24) | \
00041     (((uint32_t) (val) & (uint32_t) 0x0000ff00U) <<  8) | \
00042     (((uint32_t) (val) & (uint32_t) 0x00ff0000U) >>  8) | \
00043     (((uint32_t) (val) & (uint32_t) 0xff000000U) >> 24)))
00044 
00046 #define UINT64_SWAP_LE_BE_C(val) ((uint64_t) ( \
00047     (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000000000ff)) << 56) | \
00048     (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000000000ff00)) << 40) | \
00049     (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000000000ff0000)) << 24) | \
00050     (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000ff000000)) <<  8) | \
00051     (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000ff00000000)) >>  8) | \
00052     (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000ff0000000000)) >> 24) | \
00053     (((uint64_t) (val) & (uint64_t) UINT64_C(0x00ff000000000000)) >> 40) | \
00054     (((uint64_t) (val) & (uint64_t) UINT64_C(0xff00000000000000)) >> 56)))
00055 
00056 #ifndef UINT16_SWAP_LE_BE
00057 # define UINT16_SWAP_LE_BE UINT16_SWAP_LE_BE_C
00058 #endif
00059 
00060 #ifndef UINT32_SWAP_LE_BE
00061 # define UINT32_SWAP_LE_BE UINT32_SWAP_LE_BE_C
00062 #endif
00063 
00064 #ifndef UINT64_SWAP_LE_BE
00065 # define UINT64_SWAP_LE_BE UINT64_SWAP_LE_BE_C
00066 #endif
00067 
00068 static CDIO_INLINE
00069 uint16_t uint16_swap_le_be (const uint16_t val)
00070 {
00071   return UINT16_SWAP_LE_BE (val);
00072 }
00073 
00074 static CDIO_INLINE
00075 uint32_t uint32_swap_le_be (const uint32_t val)
00076 {
00077   return UINT32_SWAP_LE_BE (val);
00078 }
00079 
00080 static CDIO_INLINE
00081 uint64_t uint64_swap_le_be (const uint64_t val)
00082 {
00083   return UINT64_SWAP_LE_BE (val);
00084 }
00085 
00086 # define UINT8_TO_BE(val)      ((uint8_t) (val))
00087 # define UINT8_TO_LE(val)      ((uint8_t) (val))
00088 #ifdef WORDS_BIGENDIAN
00089 # define UINT16_TO_BE(val)     ((uint16_t) (val))
00090 # define UINT16_TO_LE(val)     ((uint16_t) UINT16_SWAP_LE_BE(val))
00091 
00092 # define UINT32_TO_BE(val)     ((uint32_t) (val))
00093 # define UINT32_TO_LE(val)     ((uint32_t) UINT32_SWAP_LE_BE(val))
00094 
00095 # define UINT64_TO_BE(val)     ((uint64_t) (val))
00096 # define UINT64_TO_LE(val)     ((uint64_t) UINT64_SWAP_LE_BE(val))
00097 #else
00098 # define UINT16_TO_BE(val)     ((uint16_t) UINT16_SWAP_LE_BE(val))
00099 # define UINT16_TO_LE(val)     ((uint16_t) (val))
00100 
00101 # define UINT32_TO_BE(val)     ((uint32_t) UINT32_SWAP_LE_BE(val))
00102 # define UINT32_TO_LE(val)     ((uint32_t) (val))
00103 
00104 # define UINT64_TO_BE(val)     ((uint64_t) UINT64_SWAP_LE_BE(val))
00105 # define UINT64_TO_LE(val)     ((uint64_t) (val))
00106 #endif
00107 
00109 #define UINT8_FROM_BE(val)     (UINT8_TO_BE (val))
00110 #define UINT8_FROM_LE(val)     (UINT8_TO_LE (val))
00111 #define UINT16_FROM_BE(val)    (UINT16_TO_BE (val))
00112 #define UINT16_FROM_LE(val)    (UINT16_TO_LE (val))
00113 #define UINT32_FROM_BE(val)    (UINT32_TO_BE (val))
00114 #define UINT32_FROM_LE(val)    (UINT32_TO_LE (val))
00115 #define UINT64_FROM_BE(val)    (UINT64_TO_BE (val))
00116 #define UINT64_FROM_LE(val)    (UINT64_TO_LE (val))
00117 
00119 #define CVT_TO_FUNC(bits) \
00120  static CDIO_INLINE uint ## bits ## _t \
00121  uint ## bits ## _to_be (uint ## bits ## _t val) \
00122  { return UINT ## bits ## _TO_BE (val); } \
00123  static CDIO_INLINE uint ## bits ## _t \
00124  uint ## bits ## _to_le (uint ## bits ## _t val) \
00125  { return UINT ## bits ## _TO_LE (val); } \
00126 
00127 CVT_TO_FUNC(8)
00128 CVT_TO_FUNC(16)
00129 CVT_TO_FUNC(32)
00130 CVT_TO_FUNC(64)
00131 
00132 #undef CVT_TO_FUNC
00133 
00134 #define uint8_from_be(val)     (uint8_to_be (val))
00135 #define uint8_from_le(val)     (uint8_to_le (val))
00136 #define uint16_from_be(val)    (uint16_to_be (val))
00137 #define uint16_from_le(val)    (uint16_to_le (val))
00138 #define uint32_from_be(val)    (uint32_to_be (val))
00139 #define uint32_from_le(val)    (uint32_to_le (val))
00140 #define uint64_from_be(val)    (uint64_to_be (val))
00141 #define uint64_from_le(val)    (uint64_to_le (val))
00142 
00146 #define to_711(i)   uint8_to_le(i)
00147 
00149 #define from_711(i) uint8_from_le(i)
00150 
00152 #define to_721(i)   uint16_to_le(i)
00153 
00155 #define from_721(i) uint16_from_le(i)
00156 
00158 #define to_722(i)   uint16_to_be(i)
00159 
00161 #define from_722(i) uint16_from_be(i)
00162 
00164 static CDIO_INLINE uint32_t
00165 to_723(uint16_t i)
00166 {
00167   return uint32_swap_le_be(i) | i;
00168 }
00169 
00171 static CDIO_INLINE uint16_t 
00172 from_723 (uint32_t p)
00173 {
00174   if (uint32_swap_le_be (p) != p)
00175     cdio_warn ("from_723: broken byte order");
00176 
00177   return (0xFFFF & p);
00178 }
00179 
00181 #define to_731(i)   uint32_to_le(i)
00182 
00184 #define from_731(i) uint32_from_le(i)
00185 
00187 #define to_732(i)   uint32_to_be(i)
00188 
00190 #define from_732(i) uint32_from_be(i)
00191 
00193 static CDIO_INLINE uint64_t
00194 to_733(uint32_t i)
00195 {
00196   return uint64_swap_le_be(i) | i;
00197 }
00198 
00200 static CDIO_INLINE uint32_t 
00201 from_733 (uint64_t p)
00202 {
00203   if (uint64_swap_le_be (p) != p)
00204     cdio_warn ("from_733: broken byte order");
00205     
00206   return (UINT32_C(0xFFFFFFFF) & p);
00207 }
00208 
00209 #endif /* CDIO_BYTESEX_H_ */
00210 
00211 
00212 /* 
00213  * Local variables:
00214  *  c-file-style: "gnu"
00215  *  tab-width: 8
00216  *  indent-tabs-mode: nil
00217  * End:
00218  */