pcpbnumisc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include "owncp.h"
  32. #include "pcpbnumisc.h"
  33. /*
  34. // number of leading zeros
  35. */
  36. cpSize cpNLZ_BNU(BNU_CHUNK_T x)
  37. {
  38. cpSize nlz = BNU_CHUNK_BITS;
  39. if(x) {
  40. nlz = 0;
  41. #if (BNU_CHUNK_BITS == BNU_CHUNK_64BIT)
  42. if( 0==(x & 0xFFFFFFFF00000000) ) { nlz +=32; x<<=32; }
  43. if( 0==(x & 0xFFFF000000000000) ) { nlz +=16; x<<=16; }
  44. if( 0==(x & 0xFF00000000000000) ) { nlz += 8; x<<= 8; }
  45. if( 0==(x & 0xF000000000000000) ) { nlz += 4; x<<= 4; }
  46. if( 0==(x & 0xC000000000000000) ) { nlz += 2; x<<= 2; }
  47. if( 0==(x & 0x8000000000000000) ) { nlz++; }
  48. #else
  49. if( 0==(x & 0xFFFF0000) ) { nlz +=16; x<<=16; }
  50. if( 0==(x & 0xFF000000) ) { nlz += 8; x<<= 8; }
  51. if( 0==(x & 0xF0000000) ) { nlz += 4; x<<= 4; }
  52. if( 0==(x & 0xC0000000) ) { nlz += 2; x<<= 2; }
  53. if( 0==(x & 0x80000000) ) { nlz++; }
  54. #endif
  55. }
  56. return nlz;
  57. }
  58. /*
  59. // number of trailing zeros
  60. */
  61. cpSize cpNTZ_BNU(BNU_CHUNK_T x)
  62. {
  63. cpSize ntz = BNU_CHUNK_BITS;
  64. if(x) {
  65. ntz = 0;
  66. #if (BNU_CHUNK_BITS==BNU_CHUNK_64BIT)
  67. if( 0==(x & 0x00000000FFFFFFFF) ) { ntz+=32; x>>=32; }
  68. if( 0==(x & 0x000000000000FFFF) ) { ntz+=16; x>>=16; }
  69. if( 0==(x & 0x00000000000000FF) ) { ntz+= 8; x>>= 8; }
  70. if( 0==(x & 0x000000000000000F) ) { ntz+= 4; x>>= 4; }
  71. if( 0==(x & 0x0000000000000003) ) { ntz+= 2; x>>= 2; }
  72. if( 0==(x & 0x0000000000000001) ) { ntz++; }
  73. #else
  74. if( 0==(x & 0x0000FFFF) ) { ntz+=16; x>>=16; }
  75. if( 0==(x & 0x000000FF) ) { ntz+= 8; x>>= 8; }
  76. if( 0==(x & 0x0000000F) ) { ntz+= 4; x>>= 4; }
  77. if( 0==(x & 0x00000003) ) { ntz+= 2; x>>= 2; }
  78. if( 0==(x & 0x00000001) ) { ntz++; }
  79. #endif
  80. }
  81. return ntz;
  82. }
  83. /*
  84. // Logical shift right (including inplace)
  85. //
  86. // Returns new length
  87. //
  88. */
  89. cpSize cpLSR_BNU(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, cpSize nsA, cpSize nBits)
  90. {
  91. cpSize nw = nBits/BNU_CHUNK_BITS;
  92. cpSize n;
  93. pA += nw;
  94. nsA -= nw;
  95. nBits %= BNU_CHUNK_BITS;
  96. if(nBits) {
  97. BNU_CHUNK_T hi;
  98. BNU_CHUNK_T lo = pA[0];
  99. for(n=0; n<(nsA-1); n++) {
  100. hi = pA[n+1];
  101. pR[n] = (lo>>nBits) | (hi<<(BNU_CHUNK_BITS-nBits));
  102. lo = hi;
  103. }
  104. pR[nsA-1] = (lo>>nBits);
  105. }
  106. else {
  107. for(n=0; n<nsA; n++)
  108. pR[n] = pA[n];
  109. }
  110. for(n=0; n<nw; n++)
  111. pR[nsA+n] = 0;
  112. return nsA+nw;
  113. }
  114. /*
  115. // Convert Oct String into BNU representation
  116. //
  117. // Returns size of BNU in BNU_CHUNK_T chunks
  118. */
  119. cpSize cpFromOctStr_BNU(BNU_CHUNK_T* pA, const Ipp8u* pStr, cpSize strLen)
  120. {
  121. int nsA =0;
  122. /* start from the end of string */
  123. for(; strLen>=(int)sizeof(BNU_CHUNK_T); nsA++,strLen-=(int)(sizeof(BNU_CHUNK_T))) {
  124. /* pack sizeof(BNU_CHUNK_T) bytes into single BNU_CHUNK_T value*/
  125. *pA++ =
  126. #if (BNU_CHUNK_BITS==BNU_CHUNK_64BIT)
  127. +( (BNU_CHUNK_T)pStr[strLen-8]<<(8*7) )
  128. +( (BNU_CHUNK_T)pStr[strLen-7]<<(8*6) )
  129. +( (BNU_CHUNK_T)pStr[strLen-6]<<(8*5) )
  130. +( (BNU_CHUNK_T)pStr[strLen-5]<<(8*4) )
  131. #endif
  132. +( (BNU_CHUNK_T)pStr[strLen-4]<<(8*3) )
  133. +( (BNU_CHUNK_T)pStr[strLen-3]<<(8*2) )
  134. +( (BNU_CHUNK_T)pStr[strLen-2]<<(8*1) )
  135. + (BNU_CHUNK_T)pStr[strLen-1];
  136. }
  137. /* convert the beginning of the string */
  138. if(strLen) {
  139. BNU_CHUNK_T x = 0;
  140. for(x=0; strLen>0; strLen--) {
  141. BNU_CHUNK_T d = *pStr++;
  142. x = (x<<8) + d;
  143. }
  144. *pA++ = x;
  145. nsA++;
  146. }
  147. return nsA;
  148. }
  149. /*
  150. // Convert BNU into HexString representation
  151. //
  152. // Returns length of the string or 0 if no success
  153. */
  154. cpSize cpToOctStr_BNU(Ipp8u* pStr, cpSize strLen, const BNU_CHUNK_T* pA, cpSize nsA)
  155. {
  156. FIX_BNU(pA, nsA);
  157. {
  158. cpSize bnuBitSize = BITSIZE_BNU(pA, nsA);
  159. if(bnuBitSize <= strLen*BYTESIZE) {
  160. int cnvLen = 0;
  161. BNU_CHUNK_T x = pA[nsA-1];
  162. ZEXPAND_BNU(pStr, 0, strLen);
  163. pStr += strLen - BITS2WORD8_SIZE(bnuBitSize);
  164. if(x) {
  165. //int nb;
  166. cpSize nb;
  167. for(nb=cpNLZ_BNU(x)/BYTESIZE; nb<(cpSize)(sizeof(BNU_CHUNK_T)); cnvLen++, nb++)
  168. *pStr++ = EBYTE(x, sizeof(BNU_CHUNK_T)-1-nb);
  169. for(--nsA; nsA>0; cnvLen+=sizeof(BNU_CHUNK_T), nsA--) {
  170. x = pA[nsA-1];
  171. #if (BNU_CHUNK_BITS==BNU_CHUNK_64BIT)
  172. *pStr++ = EBYTE(x,7);
  173. *pStr++ = EBYTE(x,6);
  174. *pStr++ = EBYTE(x,5);
  175. *pStr++ = EBYTE(x,4);
  176. #endif
  177. *pStr++ = EBYTE(x,3);
  178. *pStr++ = EBYTE(x,2);
  179. *pStr++ = EBYTE(x,1);
  180. *pStr++ = EBYTE(x,0);
  181. }
  182. }
  183. return strLen;
  184. }
  185. else
  186. return 0;
  187. }
  188. }