Page 235 - ARM 64 Bit Assembly Language
P. 235

Integer mathematics 223


                   440  if (bit->blks[bit->size-1] == 0)
                   441    bit->blks[bit->size-2] = ((bigchunk)1<<(BITSPERCHUNK-2));
                   442  /* this could be more efficient. TODO */
                   443  while (bigint_gt(bit,zero)) {
                   444    resplusbit = bigint_add(r, bit);
                   445    if (bigint_ge(num,resplusbit)) {
                   446      tmp = num;
                   447      num = bigint_sub(num,resplusbit);
                   448      bigint_free(tmp);
                   449      tmp = bigint_shift_right(r,1);
                   450      bigint_free(r);
                   451      r = bigint_add(tmp,bit);
                   452      bigint_free(tmp);
                   453    } else {
                   454      tmp=r;
                   455      r = bigint_shift_right(r,1);
                   456      bigint_free(tmp);
                   457    }
                   458    bigint_free(resplusbit);
                   459    tmp = bit;
                   460    bit = bigint_shift_right(bit,2);
                   461    bigint_free(tmp);
                   462  }
                   463  bigint_free(zero);
                   464  bigint_free(num);
                   465  bigint_free(bit);
                   466  return r;
                   467  }
                   468
                   469  /******************************************************************/
                   470
                   471  /* shift left by entire chunks */
                   472  static bigint bigint_shift_left_chunk(bigint l, int chunks) {
                   473  bigint tmp = bigint_alloc(l->size + chunks);
                   474  for (int i = -chunks; i < l->size; i++) {
                   475    if (i < 0)
                   476      tmp->blks[i+chunks] = 0;
                   477    else
                   478      tmp->blks[i+chunks] = l->blks[i];
                   479  }
                   480  return tmp;
                   481  }
                   482
                   483  /* shift right by entire chunks */
                   484  static bigint bigint_shift_right_chunk(bigint l, int chunks) {
                   485  bigint tmp = bigint_alloc(l->size - chunks);
                   486  for (int i = 0; i < tmp->size; i++) {
                   487    if (i<chunks)
                   488      tmp->blks[i] = 0;  // should do sign extend // TODO comment
   230   231   232   233   234   235   236   237   238   239   240