LCOV - code coverage report
Current view: top level - common/utils - copybinary_support.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 21 33 63.6 %
Date: 2021-10-13 02:24:04 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #ifndef COPYBINARY_SUPPORT_H
      10             : #define COPYBINARY_SUPPORT_H
      11             : 
      12             : #include "copybinary.h"
      13             : 
      14             : // According to Godbolt, these code sequences are recognized by
      15             : // GCC at least back to 6.2 and Clang at least back to 6.0.0.
      16             : // I didn't check earlier ones.
      17             : // They properly use byte swapping instructions.
      18             : // MSVC doesn't recognize it but that's no problem because we will
      19             : // not ever use it for big endian platforms.
      20             : 
      21             : // First some macros that can be used a expressions:
      22             : //    uint16_t swapped = copy_binary_byteswap16(value);
      23             : 
      24             : 
      25             : #ifdef _MSC_VER
      26             : 
      27             : static inline uint16_t
      28             : copy_binary_byteswap16(uint16_t value) {
      29             :         return _byteswap_ushort(value);
      30             : }
      31             : 
      32             : static inline uint32_t
      33             : copy_binary_byteswap32(uint32_t value) {
      34             :         return _byteswap_ulong(value);
      35             : }
      36             : 
      37             : static inline uint64_t
      38             : copy_binary_byteswap64(uint64_t value) {
      39             :         return _byteswap_uint64(value);
      40             : }
      41             : 
      42             : #else
      43             : 
      44             : static inline uint16_t
      45             : copy_binary_byteswap16(uint16_t value) {
      46             :         return
      47     3000012 :                 ((value & 0xFF00u) >>  8u) |
      48             :                 ((value & 0x00FFu) <<  8u)
      49             :                 ;
      50             : }
      51             : 
      52             : static inline uint32_t
      53             : copy_binary_byteswap32(uint32_t value) {
      54             :         return
      55     4000012 :                 ((value & 0xFF000000u) >> 24u) |
      56     4000012 :                 ((value & 0x00FF0000u) >>  8u) |
      57     4000012 :                 ((value & 0x0000FF00u) <<  8u) |
      58     4000012 :                 ((value & 0x000000FFu) << 24u)
      59             :                 ;
      60             : }
      61             : 
      62             : static inline uint64_t
      63     6000012 : copy_binary_byteswap64(uint64_t value) {
      64             :         return
      65     6000012 :                 ((value & 0xFF00000000000000u) >> 56u) |
      66     6000012 :                 ((value & 0x00FF000000000000u) >> 40u) |
      67     6000012 :                 ((value & 0x0000FF0000000000u) >> 24u) |
      68     6000012 :                 ((value & 0x000000FF00000000u) >>  8u) |
      69     6000012 :                 ((value & 0x00000000FF000000u) <<  8u) |
      70     6000012 :                 ((value & 0x0000000000FF0000u) << 24u) |
      71     6000012 :                 ((value & 0x000000000000FF00u) << 40u) |
      72     6000012 :                 ((value & 0x00000000000000FFu) << 56u)
      73             :                 ;
      74             : }
      75             : 
      76             : #endif
      77             : 
      78             : #ifdef HAVE_HGE
      79             : static inline
      80           0 : uhge copy_binary_byteswap128(uhge value) {
      81           0 :         uint64_t lo = (uint64_t) value;
      82           0 :         uint64_t hi = (uint64_t) (value >> 64);
      83           0 :         uhge swapped_lo = (uhge)copy_binary_byteswap64(lo);
      84           0 :         uhge swapped_hi = (uhge)copy_binary_byteswap64(hi);
      85           0 :         return swapped_hi | (swapped_lo << 64);
      86             : }
      87             : #endif
      88             : 
      89             : // These macros are used to convert a value in-place.
      90             : // This makes it possible to also convert timestamp structs.
      91             : 
      92             : static inline void
      93             : copy_binary_convert16(void *p)
      94             : {
      95             :         uint16_t *pp = (uint16_t*)p;
      96     3000012 :         *pp = copy_binary_byteswap16(*pp);
      97     1000012 : }
      98             : 
      99             : static inline void
     100     4000012 : copy_binary_convert32(void *p)
     101             : {
     102             :         uint32_t *pp = (uint32_t*)p;
     103     4000012 :         *pp = copy_binary_byteswap32(*pp);
     104     4000012 : }
     105             : 
     106             : static inline void
     107             : copy_binary_convert64(void *p)
     108             : {
     109             :         uint64_t *pp = (uint64_t*)p;
     110     6000012 :         *pp = copy_binary_byteswap64(*pp);
     111     2000012 : }
     112             : 
     113             : #ifdef HAVE_HGE
     114             : static inline void
     115             : copy_binary_convert128(void *p)
     116             : {
     117             :         uhge *pp = (uhge*)p;
     118           0 :         *pp = copy_binary_byteswap128(*pp);
     119           0 : }
     120             : #endif
     121             : 
     122             : static inline void
     123             : copy_binary_convert_date(void *p)
     124             : {
     125             :         copy_binary_date *pp = (copy_binary_date*)p;
     126             :         copy_binary_convert16(&pp->year);
     127           0 : }
     128             : 
     129             : 
     130             : static inline void
     131             : copy_binary_convert_time(void *p)
     132             : {
     133             :         copy_binary_time *pp = (copy_binary_time*)p;
     134           0 :         copy_binary_convert32(&pp->ms);
     135           0 : }
     136             : 
     137             : static inline void
     138             : copy_binary_convert_timestamp(void *p)
     139             : {
     140             :         copy_binary_timestamp *pp = (copy_binary_timestamp*)p;
     141             :         copy_binary_convert_date(&pp->date);
     142             :         copy_binary_convert_time(&pp->time);
     143           0 : }
     144             : 
     145             : #endif

Generated by: LCOV version 1.14