Home | History | Annotate | Download | only in port
      1 /*
      2  * Copyright 2011 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SFNTLY_CPP_SRC_SFNTLY_PORT_ENDIAN_H_
     18 #define SFNTLY_CPP_SRC_SFNTLY_PORT_ENDIAN_H_
     19 
     20 #include "sfntly/port/config.h"
     21 #include "sfntly/port/type.h"
     22 
     23 namespace sfntly {
     24 
     25 static inline uint16_t EndianSwap16(uint16_t value) {
     26   return (uint16_t)((value >> 8) | (value << 8));
     27 }
     28 
     29 static inline int32_t EndianSwap32(int32_t value) {
     30   return (((value & 0x000000ff) << 24) |
     31           ((value & 0x0000ff00) <<  8) |
     32           ((value & 0x00ff0000) >>  8) |
     33           ((value & 0xff000000) >> 24));
     34 }
     35 
     36 static inline uint64_t EndianSwap64(uint64_t value) {
     37   return (((value & 0x00000000000000ffLL) << 56) |
     38           ((value & 0x000000000000ff00LL) << 40) |
     39           ((value & 0x0000000000ff0000LL) << 24) |
     40           ((value & 0x00000000ff000000LL) << 8)  |
     41           ((value & 0x000000ff00000000LL) >> 8)  |
     42           ((value & 0x0000ff0000000000LL) >> 24) |
     43           ((value & 0x00ff000000000000LL) >> 40) |
     44           ((value & 0xff00000000000000LL) >> 56));
     45 }
     46 
     47 #ifdef SFNTLY_LITTLE_ENDIAN
     48   #define ToBE16(n) EndianSwap16(n)
     49   #define ToBE32(n) EndianSwap32(n)
     50   #define ToBE64(n) EndianSwap64(n)
     51   #define ToLE16(n) (n)
     52   #define ToLE32(n) (n)
     53   #define ToLE64(n) (n)
     54   #define FromBE16(n) EndianSwap16(n)
     55   #define FromBE32(n) EndianSwap32(n)
     56   #define FromBE64(n) EndianSwap64(n)
     57   #define FromLE16(n) (n)
     58   #define FromLE32(n) (n)
     59   #define FromLE64(n) (n)
     60 #else  // SFNTLY_BIG_ENDIAN
     61   #define ToBE16(n) (n)
     62   #define ToBE32(n) (n)
     63   #define ToBE64(n) (n)
     64   #define ToLE16(n) EndianSwap16(n)
     65   #define ToLE32(n) EndianSwap32(n)
     66   #define ToLE64(n) EndianSwap64(n)
     67   #define FromBE16(n) (n)
     68   #define FromBE32(n) (n)
     69   #define FromBE64(n) (n)
     70   #define FromLE16(n) EndianSwap16(n)
     71   #define FromLE32(n) EndianSwap32(n)
     72   #define FromLE64(n) EndianSwap64(n)
     73 #endif
     74 
     75 }  // namespace sfntly
     76 
     77 #endif  // SFNTLY_CPP_SRC_SFNTLY_PORT_ENDIAN_H_
     78