owncp.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef __OWNCP_H__
  32. #define __OWNCP_H__
  33. #ifndef __OWNDEFS_H__
  34. #include "owndefs.h"
  35. #endif
  36. #ifndef __IPPCP_H__
  37. #include "ippcp.h"
  38. #endif
  39. #pragma warning( disable : 4324)
  40. /* ippCP length */
  41. typedef int cpSize;
  42. /*
  43. // common macros & definitions
  44. */
  45. /* size of cache line (bytes) */
  46. #define CACHE_LINE_SIZE (64)
  47. #define LOG_CACHE_LINE_SIZE (6)
  48. /* swap data & pointers */
  49. #define SWAP_PTR(ATYPE, pX,pY) { ATYPE* aPtr=(pX); (pX)=(pY); (pY)=aPtr; }
  50. #define SWAP(x,y) {(x)^=(y); (y)^=(x); (x)^=(y);}
  51. /* alignment value */
  52. #define ALIGN_VAL ((int)sizeof(void*))
  53. /* bitsize */
  54. #define BYTESIZE (8)
  55. #define BITSIZE(x) ((int)(sizeof(x)*BYTESIZE))
  56. /* bit length -> byte/word length conversion */
  57. #define BITS2WORD8_SIZE(x) (((x)+ 7)>>3)
  58. #define BITS2WORD16_SIZE(x) (((x)+15)>>4)
  59. #define BITS2WORD32_SIZE(x) (((x)+31)>>5)
  60. #define BITS2WORD64_SIZE(x) (((x)+63)>>6)
  61. /* WORD and DWORD manipulators */
  62. #define LODWORD(x) ((Ipp32u)(x))
  63. #define HIDWORD(x) ((Ipp32u)(((Ipp64u)(x) >>32) & 0xFFFFFFFF))
  64. #define MAKEHWORD(bLo,bHi) ((Ipp16u)(((Ipp8u)(bLo)) | ((Ipp16u)((Ipp8u)(bHi))) << 8))
  65. #define MAKEWORD(hLo,hHi) ((Ipp32u)(((Ipp16u)(hLo)) | ((Ipp32u)((Ipp16u)(hHi))) << 16))
  66. #define MAKEDWORD(wLo,wHi) ((Ipp64u)(((Ipp32u)(wLo)) | ((Ipp64u)((Ipp32u)(wHi))) << 32))
  67. /* extract byte */
  68. #define EBYTE(w,n) ((Ipp8u)((w) >> (8 * (n))))
  69. /* hexString <-> Ipp32u conversion */
  70. #define HSTRING_TO_U32(ptrByte) \
  71. (((ptrByte)[0]) <<24) \
  72. +(((ptrByte)[1]) <<16) \
  73. +(((ptrByte)[2]) <<8) \
  74. +((ptrByte)[3])
  75. #define U32_TO_HSTRING(ptrByte, x) \
  76. (ptrByte)[0] = (Ipp8u)((x)>>24); \
  77. (ptrByte)[1] = (Ipp8u)((x)>>16); \
  78. (ptrByte)[2] = (Ipp8u)((x)>>8); \
  79. (ptrByte)[3] = (Ipp8u)(x)
  80. /* 32- and 64-bit masks for MSB of nbits-sequence */
  81. #define MAKEMASK32(nbits) (0xFFFFFFFF >>((32 - ((nbits)&0x1F)) &0x1F))
  82. #define MAKEMASK64(nbits) (0xFFFFFFFFFFFFFFFF >>((64 - ((nbits)&0x3F)) &0x3F))
  83. /* Logical Shifts (right and left) of WORD */
  84. #define LSR32(x,nBits) ((x)>>(nBits))
  85. #define LSL32(x,nBits) ((x)<<(nBits))
  86. /* Rorate (right and left) of WORD */
  87. #if defined(_MSC_VER)
  88. # include <stdlib.h>
  89. # define ROR32(x, nBits) _lrotr((x),(nBits))
  90. # define ROL32(x, nBits) _lrotl((x),(nBits))
  91. #else
  92. # define ROR32(x, nBits) (LSR32((x),(nBits)) | LSL32((x),32-(nBits)))
  93. # define ROL32(x, nBits) ROR32((x),(32-(nBits)))
  94. #endif
  95. /* Logical Shifts (right and left) of DWORD */
  96. #define LSR64(x,nBits) ((x)>>(nBits))
  97. #define LSL64(x,nBits) ((x)<<(nBits))
  98. /* Rorate (right and left) of DWORD */
  99. #define ROR64(x, nBits) (LSR64((x),(nBits)) | LSL64((x),64-(nBits)))
  100. #define ROL64(x, nBits) ROR64((x),(64-(nBits)))
  101. /* change endian */
  102. #if defined(_MSC_VER)
  103. # define ENDIANNESS(x) _byteswap_ulong((x))
  104. # define ENDIANNESS32(x) ENDIANNESS((x))
  105. # define ENDIANNESS64(x) _byteswap_uint64((x))
  106. #else
  107. # define ENDIANNESS(x) ((ROR32((x), 24) & 0x00ff00ff) | (ROR32((x), 8) & 0xff00ff00))
  108. # define ENDIANNESS32(x) ENDIANNESS((x))
  109. # define ENDIANNESS64(x) MAKEDWORD(ENDIANNESS(HIDWORD((x))), ENDIANNESS(LODWORD((x))))
  110. #endif
  111. #define IPP_MAKE_MULTIPLE_OF_8(x) ((x) = ((x)+7)&(~7))
  112. #define IPP_MAKE_MULTIPLE_OF_16(x) ((x) = ((x)+15)&(~15))
  113. /* 64-bit constant */
  114. #if !defined(__GNUC__)
  115. #define CONST_64(x) (x) /*(x##i64)*/
  116. #else
  117. #define CONST_64(x) (x##LL)
  118. #endif
  119. /* copy under mask */
  120. #define MASKED_COPY_BNU(dst, mask, src1, src2, len) { \
  121. cpSize i; \
  122. for(i=0; i<(len); i++) (dst)[i] = ((mask) & (src1)[i]) | (~(mask) & (src2)[i]); \
  123. }
  124. #endif /* __OWNCP_H__ */