LCOV - code coverage report
Current view: top level - user/libklib/byteorder - generic.h (source / functions) Hit Total Coverage
Test: tux3.info Lines: 3 3 100.0 %
Date: 2019-09-17 21:54:55 Functions: 0 0 -
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : #ifndef LIBKLIB_BYTEORDER_GENERIC_H
       2                 :            : #define LIBKLIB_BYTEORDER_GENERIC_H
       3                 :            : 
       4                 :            : /* include/linux/byteorder/generic.h */
       5                 :            : 
       6                 :            : /*
       7                 :            :  * linux/byteorder/generic.h
       8                 :            :  * Generic Byte-reordering support
       9                 :            :  *
      10                 :            :  * The "... p" macros, like le64_to_cpup, can be used with pointers
      11                 :            :  * to unaligned data, but there will be a performance penalty on 
      12                 :            :  * some architectures.  Use get_unaligned for unaligned data.
      13                 :            :  *
      14                 :            :  * Francois-Rene Rideau <fare@tunes.org> 19970707
      15                 :            :  *    gathered all the good ideas from all asm-foo/byteorder.h into one file,
      16                 :            :  *    cleaned them up.
      17                 :            :  *    I hope it is compliant with non-GCC compilers.
      18                 :            :  *    I decided to put __BYTEORDER_HAS_U64__ in byteorder.h,
      19                 :            :  *    because I wasn't sure it would be ok to put it in types.h
      20                 :            :  *    Upgraded it to 2.1.43
      21                 :            :  * Francois-Rene Rideau <fare@tunes.org> 19971012
      22                 :            :  *    Upgraded it to 2.1.57
      23                 :            :  *    to please Linus T., replaced huge #ifdef's between little/big endian
      24                 :            :  *    by nestedly #include'd files.
      25                 :            :  * Francois-Rene Rideau <fare@tunes.org> 19971205
      26                 :            :  *    Made it to 2.1.71; now a facelift:
      27                 :            :  *    Put files under include/linux/byteorder/
      28                 :            :  *    Split swab from generic support.
      29                 :            :  *
      30                 :            :  * TODO:
      31                 :            :  *   = Regular kernel maintainers could also replace all these manual
      32                 :            :  *    byteswap macros that remain, disseminated among drivers,
      33                 :            :  *    after some grep or the sources...
      34                 :            :  *   = Linus might want to rename all these macros and files to fit his taste,
      35                 :            :  *    to fit his personal naming scheme.
      36                 :            :  *   = it seems that a few drivers would also appreciate
      37                 :            :  *    nybble swapping support...
      38                 :            :  *   = every architecture could add their byteswap macro in asm/byteorder.h
      39                 :            :  *    see how some architectures already do (i386, alpha, ppc, etc)
      40                 :            :  *   = cpu_to_beXX and beXX_to_cpu might some day need to be well
      41                 :            :  *    distinguished throughout the kernel. This is not the case currently,
      42                 :            :  *    since little endian, big endian, and pdp endian machines needn't it.
      43                 :            :  *    But this might be the case for, say, a port of Linux to 20/21 bit
      44                 :            :  *    architectures (and F21 Linux addict around?).
      45                 :            :  */
      46                 :            : 
      47                 :            : /*
      48                 :            :  * The following macros are to be defined by <asm/byteorder.h>:
      49                 :            :  *
      50                 :            :  * Conversion of long and short int between network and host format
      51                 :            :  *      ntohl(__u32 x)
      52                 :            :  *      ntohs(__u16 x)
      53                 :            :  *      htonl(__u32 x)
      54                 :            :  *      htons(__u16 x)
      55                 :            :  * It seems that some programs (which? where? or perhaps a standard? POSIX?)
      56                 :            :  * might like the above to be functions, not macros (why?).
      57                 :            :  * if that's true, then detect them, and take measures.
      58                 :            :  * Anyway, the measure is: define only ___ntohl as a macro instead,
      59                 :            :  * and in a separate file, have
      60                 :            :  * unsigned long inline ntohl(x){return ___ntohl(x);}
      61                 :            :  *
      62                 :            :  * The same for constant arguments
      63                 :            :  *      __constant_ntohl(__u32 x)
      64                 :            :  *      __constant_ntohs(__u16 x)
      65                 :            :  *      __constant_htonl(__u32 x)
      66                 :            :  *      __constant_htons(__u16 x)
      67                 :            :  *
      68                 :            :  * Conversion of XX-bit integers (16- 32- or 64-)
      69                 :            :  * between native CPU format and little/big endian format
      70                 :            :  * 64-bit stuff only defined for proper architectures
      71                 :            :  *      cpu_to_[bl]eXX(__uXX x)
      72                 :            :  *      [bl]eXX_to_cpu(__uXX x)
      73                 :            :  *
      74                 :            :  * The same, but takes a pointer to the value to convert
      75                 :            :  *      cpu_to_[bl]eXXp(__uXX x)
      76                 :            :  *      [bl]eXX_to_cpup(__uXX x)
      77                 :            :  *
      78                 :            :  * The same, but change in situ
      79                 :            :  *      cpu_to_[bl]eXXs(__uXX x)
      80                 :            :  *      [bl]eXX_to_cpus(__uXX x)
      81                 :            :  *
      82                 :            :  * See asm-foo/byteorder.h for examples of how to provide
      83                 :            :  * architecture-optimized versions
      84                 :            :  *
      85                 :            :  */
      86                 :            : 
      87                 :            : #define cpu_to_le64 __cpu_to_le64
      88                 :            : #define le64_to_cpu __le64_to_cpu
      89                 :            : #define cpu_to_le32 __cpu_to_le32
      90                 :            : #define le32_to_cpu __le32_to_cpu
      91                 :            : #define cpu_to_le16 __cpu_to_le16
      92                 :            : #define le16_to_cpu __le16_to_cpu
      93                 :            : #define cpu_to_be64 __cpu_to_be64
      94                 :            : #define be64_to_cpu __be64_to_cpu
      95                 :            : #define cpu_to_be32 __cpu_to_be32
      96                 :            : #define be32_to_cpu __be32_to_cpu
      97                 :            : #define cpu_to_be16 __cpu_to_be16
      98                 :            : #define be16_to_cpu __be16_to_cpu
      99                 :            : #define cpu_to_le64p __cpu_to_le64p
     100                 :            : #define le64_to_cpup __le64_to_cpup
     101                 :            : #define cpu_to_le32p __cpu_to_le32p
     102                 :            : #define le32_to_cpup __le32_to_cpup
     103                 :            : #define cpu_to_le16p __cpu_to_le16p
     104                 :            : #define le16_to_cpup __le16_to_cpup
     105                 :            : #define cpu_to_be64p __cpu_to_be64p
     106                 :            : #define be64_to_cpup __be64_to_cpup
     107                 :            : #define cpu_to_be32p __cpu_to_be32p
     108                 :            : #define be32_to_cpup __be32_to_cpup
     109                 :            : #define cpu_to_be16p __cpu_to_be16p
     110                 :            : #define be16_to_cpup __be16_to_cpup
     111                 :            : #define cpu_to_le64s __cpu_to_le64s
     112                 :            : #define le64_to_cpus __le64_to_cpus
     113                 :            : #define cpu_to_le32s __cpu_to_le32s
     114                 :            : #define le32_to_cpus __le32_to_cpus
     115                 :            : #define cpu_to_le16s __cpu_to_le16s
     116                 :            : #define le16_to_cpus __le16_to_cpus
     117                 :            : #define cpu_to_be64s __cpu_to_be64s
     118                 :            : #define be64_to_cpus __be64_to_cpus
     119                 :            : #define cpu_to_be32s __cpu_to_be32s
     120                 :            : #define be32_to_cpus __be32_to_cpus
     121                 :            : #define cpu_to_be16s __cpu_to_be16s
     122                 :            : #define be16_to_cpus __be16_to_cpus
     123                 :            : 
     124                 :            : /*
     125                 :            :  * They have to be macros in order to do the constant folding
     126                 :            :  * correctly - if the argument passed into a inline function
     127                 :            :  * it is no longer constant according to gcc..
     128                 :            :  */
     129                 :            : /*
     130                 :            : #undef ntohl
     131                 :            : #undef ntohs
     132                 :            : #undef htonl
     133                 :            : #undef htons
     134                 :            : 
     135                 :            : #define ___htonl(x) __cpu_to_be32(x)
     136                 :            : #define ___htons(x) __cpu_to_be16(x)
     137                 :            : #define ___ntohl(x) __be32_to_cpu(x)
     138                 :            : #define ___ntohs(x) __be16_to_cpu(x)
     139                 :            : 
     140                 :            : #define htonl(x) ___htonl(x)
     141                 :            : #define ntohl(x) ___ntohl(x)
     142                 :            : #define htons(x) ___htons(x)
     143                 :            : #define ntohs(x) ___ntohs(x)
     144                 :            : */
     145                 :            : 
     146                 :            : static inline void le16_add_cpu(__le16 *var, u16 val)
     147                 :            : {
     148                 :            :         *var = cpu_to_le16(le16_to_cpu(*var) + val);
     149                 :            : }
     150                 :            : 
     151                 :            : static inline void le32_add_cpu(__le32 *var, u32 val)
     152                 :            : {
     153                 :            :         *var = cpu_to_le32(le32_to_cpu(*var) + val);
     154                 :            : }
     155                 :            : 
     156                 :            : static inline void le64_add_cpu(__le64 *var, u64 val)
     157                 :            : {
     158                 :            :         *var = cpu_to_le64(le64_to_cpu(*var) + val);
     159                 :            : }
     160                 :            : 
     161                 :            : /* XXX: this stuff can be optimized */
     162                 :            : static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
     163                 :            : {
     164                 :            :         while (words--) {
     165                 :            :                 __le32_to_cpus(buf);
     166                 :            :                 buf++;
     167                 :            :         }
     168                 :            : }
     169                 :            : 
     170                 :            : static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
     171                 :            : {
     172                 :            :         while (words--) {
     173                 :            :                 __cpu_to_le32s(buf);
     174                 :            :                 buf++;
     175                 :            :         }
     176                 :            : }
     177                 :            : 
     178                 :            : static inline void be16_add_cpu(__be16 *var, u16 val)
     179                 :            : {
     180                 :      60093 :         *var = cpu_to_be16(be16_to_cpu(*var) + val);
     181                 :            : }
     182                 :            : 
     183                 :            : static inline void be32_add_cpu(__be32 *var, u32 val)
     184                 :            : {
     185                 :       1433 :         *var = cpu_to_be32(be32_to_cpu(*var) + val);
     186                 :            : }
     187                 :            : 
     188                 :            : static inline void be64_add_cpu(__be64 *var, u64 val)
     189                 :            : {
     190                 :        202 :         *var = cpu_to_be64(be64_to_cpu(*var) + val);
     191                 :            : }
     192                 :            : 
     193                 :            : static inline void cpu_to_be32_array(__be32 *dst, const u32 *src, size_t len)
     194                 :            : {
     195                 :            :         int i;
     196                 :            : 
     197                 :            :         for (i = 0; i < len; i++)
     198                 :            :                 dst[i] = cpu_to_be32(src[i]);
     199                 :            : }
     200                 :            : 
     201                 :            : static inline void be32_to_cpu_array(u32 *dst, const __be32 *src, size_t len)
     202                 :            : {
     203                 :            :         int i;
     204                 :            : 
     205                 :            :         for (i = 0; i < len; i++)
     206                 :            :                 dst[i] = be32_to_cpu(src[i]);
     207                 :            : }
     208                 :            : 
     209                 :            : #endif /* !LIBKLIB_BYTEORDER_GENERIC_H */

Generated by: LCOV version 1.14