pcpbnuimpl.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #if !defined(_CP_BNU_IMPL_H)
  32. #define _CP_BNU_IMPL_H
  33. #define BNU_CHUNK_64BIT (64)
  34. #define BNU_CHUNK_32BIT (32)
  35. /*
  36. // define BNU chunk data type
  37. */
  38. #if ((_IPP_ARCH == _IPP_ARCH_EM64T) || (_IPP_ARCH == _IPP_ARCH_LP64) || (_IPP_ARCH == _IPP_ARCH_LRB) || (_IPP_ARCH == _IPP_ARCH_LRB2))
  39. typedef Ipp64u BNU_CHUNK_T;
  40. typedef Ipp64s BNS_CHUNK_T;
  41. #define BNU_CHUNK_LOG2 (6)
  42. #define BNU_CHUNK_BITS BNU_CHUNK_64BIT
  43. #else
  44. typedef Ipp32u BNU_CHUNK_T;
  45. typedef Ipp32s BNS_CHUNK_T;
  46. #define BNU_CHUNK_LOG2 (5)
  47. #define BNU_CHUNK_BITS BNU_CHUNK_32BIT
  48. #endif
  49. #define BNU_CHUNK_MASK (~(BNU_CHUNK_T)(0))
  50. #if (BNU_CHUNK_BITS == BNU_CHUNK_64BIT)
  51. #pragma message ("BNU_CHUNK_BITS = 64 bit")
  52. #elif (BNU_CHUNK_BITS == BNU_CHUNK_32BIT)
  53. #pragma message ("BNU_CHUNK_BITS = 32 bit")
  54. #else
  55. #error BNU_CHUNK_BITS should be either 64 or 32 bit!
  56. #endif
  57. #ifdef _MSC_VER
  58. # pragma warning( disable : 4127)
  59. #endif
  60. /* user's API BNU chunk data type */
  61. typedef Ipp32u API_BNU_CHUNK_T;
  62. /* convert API_BNU_CHUNK_T (usual Ipp32u) length into the BNU_CHUNK_T length */
  63. #define INTERNAL_BNU_LENGTH(apiLen) \
  64. ((apiLen) + sizeof(BNU_CHUNK_T)/sizeof(API_BNU_CHUNK_T) -1)/(sizeof(BNU_CHUNK_T)/sizeof(API_BNU_CHUNK_T))
  65. /* Low and High parts of BNU_CHUNK_T value */
  66. #define BNU_CHUNK_2H ((BNU_CHUNK_T)1 << (BNU_CHUNK_BITS/2))
  67. #define LO_CHUNK(c) ((BNU_CHUNK_T)(c) & (BNU_CHUNK_2H - 1))
  68. #define HI_CHUNK(c) ((BNU_CHUNK_T)(c) >> (BNU_CHUNK_BITS/2))
  69. /* (carry,R) = A+B */
  70. #define ADD_AB(CARRY,R, A,B) \
  71. do { \
  72. BNU_CHUNK_T __s = (A) + (B); \
  73. (CARRY) = __s < (A); \
  74. (R) = __s; \
  75. } while(0)
  76. /* (carry,R) = A+B+C */
  77. #define ADD_ABC(CARRY,R, A,B,C) \
  78. do { \
  79. BNU_CHUNK_T __s = (A) + (B); \
  80. BNU_CHUNK_T __t1= __s < (A); \
  81. BNU_CHUNK_T __r = __s + (C); \
  82. BNU_CHUNK_T __t2 = __r < __s; \
  83. (CARRY) = __t1 + __t2; \
  84. (R) = __r; \
  85. } while(0)
  86. /* (borrow,R) = A-B */
  87. #define SUB_AB(BORROW,R, A,B) \
  88. do { \
  89. (BORROW) = (A)<(B); \
  90. (R) = (A)-(B); \
  91. } while(0)
  92. /* (borrow,R) = A-B-C */
  93. #define SUB_ABC(BORROW,R, A,B,C) \
  94. do { \
  95. BNU_CHUNK_T __s = (A) -( B); \
  96. BNU_CHUNK_T __t1= __s > (A); \
  97. BNU_CHUNK_T __r = __s - (C); \
  98. BNU_CHUNK_T __t2 = __r > __s; \
  99. (BORROW) = __t1 + __t2; \
  100. (R) = __r; \
  101. } while(0)
  102. /* (RH,RL) = A*B */
  103. #define MUL_AB(RH, RL, A, B) \
  104. do { \
  105. BNU_CHUNK_T __aL = LO_CHUNK((A)); \
  106. BNU_CHUNK_T __aH = HI_CHUNK((A)); \
  107. BNU_CHUNK_T __bL = LO_CHUNK((B)); \
  108. BNU_CHUNK_T __bH = HI_CHUNK((B)); \
  109. \
  110. BNU_CHUNK_T __x0 = (BNU_CHUNK_T) __aL * __bL; \
  111. BNU_CHUNK_T __x1 = (BNU_CHUNK_T) __aL * __bH; \
  112. BNU_CHUNK_T __x2 = (BNU_CHUNK_T) __aH * __bL; \
  113. BNU_CHUNK_T __x3 = (BNU_CHUNK_T) __aH * __bH; \
  114. \
  115. __x1 += HI_CHUNK(__x0); \
  116. __x1 += __x2; \
  117. if(__x1 < __x2) \
  118. __x3 += BNU_CHUNK_2H; \
  119. \
  120. (RH) = __x3 + HI_CHUNK(__x1); \
  121. (RL) = (__x1 << BNU_CHUNK_BITS/2) + LO_CHUNK(__x0); \
  122. } while (0)
  123. #endif /* _CP_BNU_IMPL_H */