a zip code crypto-currency system good for red ONLY

genCombMulTo.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. // NOTE: This could be potentionally used to generate loop-less multiplications
  3. function genCombMulTo (alen, blen) {
  4. var len = alen + blen - 1;
  5. var src = [
  6. 'var a = self.words;',
  7. 'var b = num.words;',
  8. 'var o = out.words;',
  9. 'var c = 0;',
  10. 'var lo;',
  11. 'var mid;',
  12. 'var hi;'
  13. ];
  14. for (var i = 0; i < alen; i++) {
  15. src.push('var a' + i + ' = a[' + i + '] | 0;');
  16. src.push('var al' + i + ' = a' + i + ' & 0x1fff;');
  17. src.push('var ah' + i + ' = a' + i + ' >>> 13;');
  18. }
  19. for (i = 0; i < blen; i++) {
  20. src.push('var b' + i + ' = b[' + i + '] | 0;');
  21. src.push('var bl' + i + ' = b' + i + ' & 0x1fff;');
  22. src.push('var bh' + i + ' = b' + i + ' >>> 13;');
  23. }
  24. src.push('');
  25. src.push('out.negative = self.negative ^ num.negative;');
  26. src.push('out.length = ' + len + ';');
  27. for (var k = 0; k < len; k++) {
  28. var minJ = Math.max(0, k - alen + 1);
  29. var maxJ = Math.min(k, blen - 1);
  30. src.push('\/* k = ' + k + ' *\/');
  31. src.push('var w' + k + ' = c;');
  32. src.push('c = 0;');
  33. for (var j = minJ; j <= maxJ; j++) {
  34. i = k - j;
  35. src.push('lo = Math.imul(al' + i + ', bl' + j + ');');
  36. src.push('mid = Math.imul(al' + i + ', bh' + j + ');');
  37. src.push('mid = (mid + Math.imul(ah' + i + ', bl' + j + ')) | 0;');
  38. src.push('hi = Math.imul(ah' + i + ', bh' + j + ');');
  39. src.push('w' + k + ' = (w' + k + ' + lo) | 0;');
  40. src.push('w' + k + ' = (w' + k + ' + ((mid & 0x1fff) << 13)) | 0;');
  41. src.push('c = (c + hi) | 0;');
  42. src.push('c = (c + (mid >>> 13)) | 0;');
  43. src.push('c = (c + (w' + k + ' >>> 26)) | 0;');
  44. src.push('w' + k + ' &= 0x3ffffff;');
  45. }
  46. }
  47. // Store in separate step for better memory access
  48. for (k = 0; k < len; k++) {
  49. src.push('o[' + k + '] = w' + k + ';');
  50. }
  51. src.push('if (c !== 0) {',
  52. ' o[' + k + '] = c;',
  53. ' out.length++;',
  54. '}',
  55. 'return out;');
  56. return src.join('\n');
  57. }
  58. console.log(genCombMulTo(10, 10));