pcpbnumisc.h 4.8 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. #if !defined(_PCP_BNUMISC_H)
  32. #define _PCP_BNUMISC_H
  33. #include "pcpbnuimpl.h"
  34. /* bit operations */
  35. #define BITSIZE_BNU(p,ns) ((ns)*BNU_CHUNK_BITS-cpNLZ_BNU((p)[(ns)-1]))
  36. #define BIT_BNU(bnu, ns,nbit) ((((nbit)>>BNU_CHUNK_LOG2) < (ns))? ((((bnu))[(nbit)>>BNU_CHUNK_LOG2] >>((nbit)&(BNU_CHUNK_BITS))) &1) : 0)
  37. #define TST_BIT(bnu, nbit) ((((bnu))[(nbit)>>BNU_CHUNK_LOG2]) & ((BNU_CHUNK_T)1<<((nbit)&(BNU_CHUNK_BITS-1))))
  38. #define SET_BIT(bnu, nbit) ((((bnu))[(nbit)>>BNU_CHUNK_LOG2]) |= ((BNU_CHUNK_T)1<<((nbit)&(BNU_CHUNK_BITS-1))))
  39. #define CLR_BIT(bnu, nbit) ((((bnu))[(nbit)>>BNU_CHUNK_LOG2]) &=~((BNU_CHUNK_T)1<<((nbit)&(BNU_CHUNK_BITS-1))))
  40. /* convert bitsize nbits into the number of BNU_CHUNK_T */
  41. #define BITS_BNU_CHUNK(nbits) (((nbits)+BNU_CHUNK_BITS-1)/BNU_CHUNK_BITS)
  42. /* mask for top BNU_CHUNK_T */
  43. #define MASK_BNU_CHUNK(nbits) ((BNU_CHUNK_T)(-1) >>((BNU_CHUNK_BITS- ((nbits)&(BNU_CHUNK_BITS-1))) &(BNU_CHUNK_BITS-1)))
  44. /* copy BNU content */
  45. #define COPY_BNU(dst, src, len) \
  46. { \
  47. cpSize __idx; \
  48. for(__idx=0; __idx<(len); __idx++) (dst)[__idx] = (src)[__idx]; \
  49. }
  50. /* expand by zeros */
  51. #define ZEXPAND_BNU(srcdst,srcLen, dstLen) \
  52. { \
  53. cpSize __idx; \
  54. for(__idx=(srcLen); __idx<(dstLen); __idx++) (srcdst)[__idx] = 0; \
  55. }
  56. /* copy and expand by zeros */
  57. #define ZEXPAND_COPY_BNU(dst,dstLen, src,srcLen) \
  58. { \
  59. cpSize __idx; \
  60. for(__idx=0; __idx<(srcLen); __idx++) (dst)[__idx] = (src)[__idx]; \
  61. for(; __idx<(dstLen); __idx++) (dst)[__idx] = 0; \
  62. }
  63. /* fix actual length */
  64. #define FIX_BNU(src,srcLen) \
  65. for(; ((srcLen)>1) && (0==(src)[(srcLen)-1]); (srcLen)--)
  66. /* copy and set */
  67. __INLINE void cpCpy_BNU(BNU_CHUNK_T* pDst, const BNU_CHUNK_T* pSrc, cpSize ns)
  68. { COPY_BNU(pDst, pSrc, ns); }
  69. __INLINE void cpSet_BNU(BNU_CHUNK_T* pDst, cpSize ns, BNU_CHUNK_T val)
  70. {
  71. ZEXPAND_BNU(pDst, 0, ns);
  72. pDst[0] = val;
  73. }
  74. /* fix up */
  75. __INLINE int cpFix_BNU(const BNU_CHUNK_T* pA, int nsA)
  76. {
  77. FIX_BNU(pA, nsA);
  78. return nsA;
  79. }
  80. /* comparison
  81. //
  82. // returns
  83. // negative, if A < B
  84. // 0, if A = B
  85. // positive, if A > B
  86. */
  87. __INLINE int cpCmp_BNU(const BNU_CHUNK_T* pA, cpSize nsA, const BNU_CHUNK_T* pB, cpSize nsB)
  88. {
  89. if(nsA!=nsB)
  90. return nsA>nsB? 1 : -1;
  91. else {
  92. for(; nsA>0; nsA--) {
  93. if(pA[nsA-1] > pB[nsA-1])
  94. return 1;
  95. else if(pA[nsA-1] < pB[nsA-1])
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. }
  101. __INLINE int cpEqu_BNU_CHUNK(const BNU_CHUNK_T* pA, cpSize nsA, BNU_CHUNK_T b)
  102. {
  103. return (pA[0]==b && 1==cpFix_BNU(pA, nsA));
  104. }
  105. /*
  106. // test
  107. //
  108. // returns
  109. // 0, if A = 0
  110. // >0, if A > 0
  111. // <0, looks like impossible (or error) case
  112. */
  113. __INLINE int cpTst_BNU(const BNU_CHUNK_T* pA, int nsA)
  114. {
  115. for(; (nsA>0) && (0==pA[nsA-1]); nsA--) ;
  116. return nsA;
  117. }
  118. /* number of leading/trailing zeros */
  119. cpSize cpNLZ_BNU(BNU_CHUNK_T x);
  120. cpSize cpNTZ_BNU(BNU_CHUNK_T x);
  121. /* logical shift left/right */
  122. int cpLSR_BNU(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, cpSize nsA, cpSize nBits);
  123. /* least and most significant BNU bit */
  124. int cpMSBit_BNU(const BNU_CHUNK_T* pA, cpSize nsA);
  125. /* BNU <-> hex-string conversion */
  126. int cpToOctStr_BNU(Ipp8u* pStr, cpSize strLen, const BNU_CHUNK_T* pA, cpSize nsA);
  127. int cpFromOctStr_BNU(BNU_CHUNK_T* pA, const Ipp8u* pStr, cpSize strLen);
  128. #endif /* _PCP_BNUMISC_H */