1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
|
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
File: octapi_largmath.h
Copyright (c) 2001-2007 Octasic Inc.
Description:
Library used to perform arithmetic on integer values of an integer multiple
of 32-bits.
This file is part of the Octasic OCT6100 GPL API . The OCT6100 GPL API is
free software; you can redistribute it and/or modify it under the terms of
the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.
The OCT6100 GPL API is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with the OCT6100 GPL API; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
$Octasic_Release: OCT612xAPI-01.00-PR49 $
$Octasic_Revision: 10 $
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#include "apilib/octapi_largmath.h"
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
| API UTILITIES
|
| Function: OctApiLmAdd.
|
| Description: This function adds 2 numbers, a and b. Number a is
| (alen + 1) * 32 bits long; b is (blen + 1) * 32 bits long. The
| result is (zlen + 1) * 32 bits long. It the function succeeds it returns
| GENERIC_OK, else GENERIC_ERROR.
|
| -----------------------------------------------------------------------
| | Variable | Type | Description
| -----------------------------------------------------------------------
| *a UINT32 The array containing the first number.
| alen USHORT The length of array a, minus 1 (0 - 99).
| *b UINT32 The array containing the second number.
| blen USHORT The length of array b, minus 1 (0 - 99).
| *z UINT32 The array containing the resulting number.
| zlen USHORT The length of array z, minus 1 (0 - 99).
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if !SKIP_OctApiLmAdd
UINT32 OctApiLmAdd(UINT32 * a,USHORT alen,UINT32 * b,USHORT blen,UINT32 * z, USHORT zlen)
{
USHORT i;
UINT32 temp;
UINT32 carry=0;
UINT32 aprim;
UINT32 bprim;
/* Check for array lengths.*/
if (alen > zlen || blen > zlen) return(OCTAPI_LM_ARRAY_SIZE_MISMATCH);
for(i=0;i<=zlen;i++)
{
if (i <= alen) aprim = *(a+i); else aprim = 0;
if (i <= blen) bprim = *(b+i); else bprim = 0;
temp = aprim + bprim + carry;
/* Calculate carry for next time.*/
if (carry == 0)
if (temp < aprim) carry = 1; else carry = 0;
else
if (temp <= aprim) carry = 1; else carry = 0;
/* Write new value.*/
*(z+i) = temp;
}
/* Check for overflow.*/
if (carry == 1) return(OCTAPI_LM_OVERFLOW);
/* All is well.*/
return(GENERIC_OK);
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
| API UTILITIES
|
| Function: OctApiLmSubtract.
|
| Description: This function subtracts 2 numbers, a and b. Number a is
| (alen + 1) * 32 bits long; b is (blen + 1) * 32 bits long. The result
| is (zlen + 1) * 32 bits long. It the function succeeds it returns
| GENERIC_OK, else GENERIC_ERROR.
|
| -----------------------------------------------------------------------
| | Variable | Type | Description
| -----------------------------------------------------------------------
| *a UINT32 The array containing the first number.
| alen USHORT The length of array a, minus 1 (0 - 99).
| *bneg UINT32 The array containing the second number.
| blen USHORT The length of array b, minus 1 (0 - 99).
| *z UINT32 The array containing the resulting number.
| zlen USHORT The length of array z, minus 1 (0 - 99).
| *neg USHORT Indicates if the result is negative
| (TRUE/FALSE).
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if !SKIP_OctApiLmSubtract
UINT32 OctApiLmSubtract(UINT32 * a,USHORT alen,UINT32 * bneg,USHORT blen,UINT32 * z,USHORT zlen,USHORT * neg)
{
USHORT i;
UINT32 temp;
UINT32 carry=1;
UINT32 aprim;
UINT32 bprim;
/* Check for array lengths.*/
if (alen > zlen || blen > zlen) return(OCTAPI_LM_ARRAY_SIZE_MISMATCH);
for(i=0;i<=zlen;i++)
{
if (i <= alen) aprim = *(a+i); else aprim = 0;
if (i <= blen) bprim = ~(*(bneg+i)); else bprim = 0xFFFFFFFF;
temp = aprim + bprim + carry;
/* Calculate carry for next time.*/
if (carry == 0)
if (temp < aprim) carry = 1; else carry = 0;
else
if (temp <= aprim) carry = 1; else carry = 0;
/* Write new value.*/
*(z+i) = temp;
}
/* Check for overflow, which means negative number!*/
if (carry == 0)
{
/* Number is not of right neg. Invert and add one to correct neg.*/
for(i=0;i<=zlen;i++)
*(z+i) = ~(*(z+i));
temp = 1;
OctApiLmAdd(&temp,0,z,zlen,z,zlen);
*neg = TRUE;
return(GENERIC_OK);
}
/* Result is positive.*/
*neg = FALSE;
return(GENERIC_OK);
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
| API UTILITIES
|
| Function: OctApiLmCompare.
|
| Description: This function compares two numbers (arrays) of equal lengths.
| Number a is (alen + 1) * 32 bits long; b is (blen + 1) * 32 bits long. The result
|
| -----------------------------------------------------------------------
| | Variable | Type | Description
| -----------------------------------------------------------------------
| *a UINT32 The array containing the first number.
| alen USHORT The length of array a, minus 1 (0 - 99).
| *b UINT32 The array containing the second number.
| blen USHORT The length of array b, minus 1 (0 - 99).
| *neg USHORT Result of compare.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if !SKIP_OctApiLmCompare
UINT32 OctApiLmCompare(UINT32 * a,USHORT alen,UINT32 * bneg,USHORT blen,USHORT * neg)
{
USHORT i;
UINT32 temp;
UINT32 carry=1;
UINT32 aprim;
UINT32 bprim;
UINT32 zlen;
/* Set zlen to alen or blen (which ever is longer)*/
if (alen < blen)
zlen = blen;
else
zlen = alen;
for(i=0;i<=zlen;i++)
{
if (i <= alen) aprim = *(a+i); else aprim = 0;
if (i <= blen) bprim = ~(*(bneg+i)); else bprim = 0xFFFFFFFF;
temp = aprim + bprim + carry;
/* Calculate carry for next time.*/
if (carry == 0)
if (temp < aprim) carry = 1; else carry = 0;
else
if (temp <= aprim) carry = 1; else carry = 0;
}
/* Check for overflow, which means negative number!*/
if (carry == 0)
{
*neg = TRUE;
return(GENERIC_OK);
}
/* Result is positive.*/
*neg = FALSE;
return(GENERIC_OK);
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
| API UTILITIES
|
| Function: OctApiLmSubtract.
|
| Description: This function multiplies 2 numbers, a and b. Number a and
| b are both (ablen + 1) * 32 bits long. The result is twice as
| long. If the functions succeeds if returns GENERIC_OK,
| else GENERIC_ERROR.
|
| -----------------------------------------------------------------------
| | Variable | Type | Description
| -----------------------------------------------------------------------
| *a UINT32 The array containing the first number.
| *b UINT32 The array containing the second number.
| ablen USHORT The length of arrays a and b, minus 1 (0 - 99).
| *z UINT32 The array containing the resulting number.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if !SKIP_OctApiLmMultiply
UINT32 OctApiLmMultiply(UINT32 * a,UINT32 * b,USHORT ablen,UINT32 * z)
{
USHORT i,j,k;
USHORT nos;
UINT32 lownum;
UINT32 highnum;
USHORT longnumi;
USHORT longnumj;
USHORT indentw,indentl;
/* Caculate number of shorts in a and b.*/
nos = (USHORT)((ablen+1) * 2);
/* Clear answer word.*/
for(i=0;i<nos;i++)
*(z+i) = 0;
{
USHORT optimizea, optimizeb;
USHORT l;
optimizea = TRUE;
optimizeb = TRUE;
for(l = 1; l < ablen+1; l++)
{
if(*(a+l) != 0)
optimizea = FALSE;
if(*(b+l) != 0)
optimizeb = FALSE;
}
if(*a > OCTAPI_LM_MAX_OPTIMIZE_MUL)
optimizea = FALSE;
if(*b > OCTAPI_LM_MAX_OPTIMIZE_MUL)
optimizeb = FALSE;
if(optimizea == TRUE)
{
for(l = 0; l < *a; l++)
OctApiLmAdd(z, (USHORT)(nos-1), b, ablen, z, (USHORT)(nos-1));
return(GENERIC_OK);
}
if(optimizeb == TRUE)
{
for(l = 0; l < *b; l++)
OctApiLmAdd(z, (USHORT)(nos-1), a, ablen, z, (USHORT)(nos-1));
return(GENERIC_OK);
}
}
for(i=0;i<nos;i++)
{
longnumi = (USHORT)( i/2 );
/* One iteration per short in a.*/
if ((i%2) == 0)
lownum = *(a+longnumi) & 0xFFFF; /* Even word. Lower part of long.*/
else
lownum = *(a+longnumi)>>16; /* Odd word. Upper part of long.*/
for(j=0;j<nos;j++)
{
UINT32 product;
longnumj = (USHORT)( j/2 );
/* One iteration per short in a.*/
if ((j%2) == 0)
highnum = *(b+longnumj) & 0xFFFF; /* Even word. Lower part of long.*/
else
highnum = *(b+longnumj)>>16; /* Odd word. Upper part of long.*/
/* Find the word indent of the answer. 0 = no indent. 1 = one word indent.*/
indentw = (USHORT)( j+i );
indentl = (USHORT)( indentw / 2 );
/* Multiply both numbers.*/
product = highnum * lownum;
/* After multiplying both numbers, add result to end result.*/
if ((indentw % 2) == 0) /* Even word boundary, addition in one shot!*/
{
UINT32 carry=0;
UINT32 temp;
UINT32 addme;
for(k=indentl;k<nos;k++)
{
if (k==indentl) addme = product; else addme = 0;
temp = *(z+k) + addme + carry;
/* Calculate carry for next time.*/
if (carry == 0)
if (temp < addme) carry = 1; else carry = 0;
else
if (temp <= addme) carry = 1; else carry = 0;
/* Set value.*/
*(z+k) = temp;
}
/* Carry should always be 0.*/
if (carry == 1) return(GENERIC_ERROR);
}
else /* Odd word boundary, addition in two shots.*/
{
UINT32 carry=0;
UINT32 temp;
UINT32 addme;
for(k=indentl;k<nos;k++)
{
if (k==indentl) addme = product<<16;
else if (k==(indentl+1)) addme = product>>16;
else addme = 0;
temp = *(z+k) + addme + carry;
/* Calculate carry for next time.*/
if (carry == 0)
if (temp < addme) carry = 1; else carry = 0;
else
if (temp <= addme) carry = 1; else carry = 0;
/* Set value.*/
*(z+k) = temp;
}
/* Carry should always be 0.*/
if (carry == 1) return(GENERIC_ERROR);
}
}
}
return(GENERIC_OK);
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
| API UTILITIES
|
| Function: OctApiLmDivide.
|
| Description: This function divides the number n by the number d. The
| quotient is placed in q and the remainder in r. The arrays
| n, d, q and r are all of the same length, namely (ndqrlen + 1).
| If the functions succeeds if returns GENERIC_OK, else
| GENERIC_ERROR.
|
| -----------------------------------------------------------------------
| | Variable | Type | Description
| -----------------------------------------------------------------------
| *a UINT32 The array containing the first number.
| *b UINT32 The array containing the second number.
| ablen USHORT The length of arrays a and b, minus 1 (0 - 99).
| *z UINT32 The array containing the resulting number.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if !SKIP_OctApiLmDivide
UINT32 OctApiLmDivide(UINT32 * n,UINT32 * d,UINT32 * q,UINT32 * r,USHORT ndqrlen)
{
/* Proceedure for division:*/
/* r = n*/
/* q = 0*/
/* shift = initial_denominator_shift (for upper '1's to be in same bit position).*/
/* d <<= shift;*/
/* Start loop:*/
/* compare r and d*/
/* if r > d then*/
/* r -= d;*/
/* write a '1' to bit "shift" of array q.*/
/* end if;*/
/* if shift == 0 then*/
/* return;*/
/* else*/
/* shift--;*/
/* d>>=1;*/
/* goto "Start loop:"*/
/* end if;*/
UINT32 i;
UINT32 result;
USHORT shift,n_msb,d_msb;
USHORT neg;
USHORT ConditionFlag = TRUE;
/* r = n*/
for(i=0;i<=ndqrlen;i++)
*(r+i) = *(n+i);
/* q = 0*/
for(i=0;i<=ndqrlen;i++)
*(q+i) = 0;
/* shift = initial_denominator_shift (for upper '1's to be in same bit position).*/
result = OctApiLmGetMsb(d,ndqrlen,&d_msb);
if (result != GENERIC_OK) return(result);
result = OctApiLmGetMsb(n,ndqrlen,&n_msb);
if (result != GENERIC_OK) return(result);
if (d_msb == 0xFFFF) /* Division by 0.*/
return(OCTAPI_LM_DIVISION_BY_ZERO);
if (n_msb == 0xFFFF) /* 0/n, returns 0 R 0.*/
return(GENERIC_OK);
if (n_msb < d_msb) /* x/y, where x is smaller than y, returns 0 R x.*/
return(GENERIC_OK);
shift = (USHORT)( n_msb - d_msb );
/* Shift d to match n highest bit position.*/
result = OctApiLmShiftn(d,ndqrlen,TRUE,shift);
if (result != GENERIC_OK) return(result);
/* Start loop:*/
while( ConditionFlag == TRUE )
{
/* compare r and d*/
result = OctApiLmCompare(r,ndqrlen,d,ndqrlen,&neg);
if (result != GENERIC_OK) return(result);
if (neg == FALSE) /* Subtraction can be done(do it).*/
{
/* r -= d;*/
result = OctApiLmSubtract(r,ndqrlen,d,ndqrlen,r,ndqrlen,&neg);
if (result != GENERIC_OK) return(result);
/* write a '1' to bit "shift" of array q.*/
*(q+(shift/32)) |= (UINT32)0x1 << (shift%32);
}
/* if shift == 0 then*/
/* return;*/
if (shift == 0) return(GENERIC_OK);
/* shift--;*/
/* d>>=1;*/
/* goto "Start loop:"*/
shift--;
OctApiLmShiftRight1(d,ndqrlen);
}
return(GENERIC_OK);
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
| API UTILITIES
|
| Function: octapi_lm_shifright1.
|
| Description: The function is for internal use only.
|
| -----------------------------------------------------------------------
| | Variable | Type | Description
| -----------------------------------------------------------------------
| N/A.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if !SKIP_OctApiLmShiftRight1
UINT32 OctApiLmShiftRight1(UINT32 * a,USHORT alen)
{
UINT32 i;
/* Start with lower long and move up by one long each time,*/
/* shifting each long to the right by one bit. The upper bit*/
/* of the next long will have to be concatenated each time a*/
/* loop is executed. For the last long, leave the highest bit*/
/* intact.*/
for(i=0;i<alen;i++)
{
*(a+i)>>=1; /* Shift long by one to the right.*/
*(a+i)|=*(a+i+1)<<31;
}
*(a+alen)>>=1; /* Shift last long, leaving it's highest bit at 0.*/
return(GENERIC_OK);
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
| API UTILITIES
|
| Function: OctApiLmShiftn.
|
| Description: The function is for internal use only.
|
| -----------------------------------------------------------------------
| | Variable | Type | Description
| -----------------------------------------------------------------------
| N/A.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if !SKIP_OctApiLmShiftn
UINT32 OctApiLmShiftn(UINT32 * a,USHORT alen,USHORT shiftleft,USHORT shiftn)
{
UINT32 i;
USHORT long_offset;
USHORT bit_offset;
long_offset = (USHORT)( shiftn / 32 );
bit_offset = (USHORT)( shiftn % 32 );
if (shiftleft == TRUE) /* Shift left.*/
{
for(i=alen;i<=alen;i--)
{
/* Fill upper bits of long.*/
if (i >= long_offset)
*(a+i) = *(a+i-long_offset) << bit_offset;
else
*(a+i) = 0;
/* Fill lower bits of long.*/
if (i > long_offset && bit_offset != 0)
*(a+i) |= *(a+i-long_offset-1) >> (32-bit_offset);
}
}
else /* Shift right.*/
{
for(i=0;i<=alen;i++)
{
/* Fill lower bits of long.*/
if ((alen-i) >= long_offset)
*(a+i) = *(a+i+long_offset) >> bit_offset;
else
*(a+i) = 0;
/* Fill upper bits of long.*/
if ((alen-i) > long_offset && bit_offset != 0)
*(a+i) |= *(a+i+long_offset+1) << (32-bit_offset);
}
}
return(GENERIC_OK);
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
| API UTILITIES
|
| Function: OctApiLmGetMsb.
|
| Description: The function is for internal use only.
|
| -----------------------------------------------------------------------
| | Variable | Type | Description
| -----------------------------------------------------------------------
| N/A.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if !SKIP_OctApiLmGetMsb
UINT32 OctApiLmGetMsb(UINT32 * a,USHORT alen,USHORT * msb_pos)
{
UINT32 i,j;
UINT32 x;
for(i=alen;i<=alen;i--)
{
if (*(a+i) == 0) continue;
x = *(a+i);
for(j=31;j<=31;j--)
{
/* Test for bit being '1'.*/
if ((x & 0x80000000) != 0)
{
*msb_pos=(USHORT)(j+(32*i));
return(GENERIC_OK);
}
/* Shift bit one bit position, and try again.*/
x<<=1;
}
}
/* MSB not found.*/
*msb_pos = 0xFFFF;
return(GENERIC_OK);
}
#endif
|