123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095 |
- /* Copyright (C) 2007-2022 Free Software Foundation, Inc.
- This file is part of GCC.
- GCC 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 3, or (at your option) any later
- version.
- GCC 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.
- Under Section 7 of GPL version 3, you are granted additional
- permissions described in the GCC Runtime Library Exception, version
- 3.1, as published by the Free Software Foundation.
- You should have received a copy of the GNU General Public License and
- a copy of the GCC Runtime Library Exception along with this program;
- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
- <http://www.gnu.org/licenses/>. */
- #define BID_128RES
- #include "bid_internal.h"
- /*****************************************************************************
- * BID128 minimum number
- *****************************************************************************/
- #if DECIMAL_CALL_BY_REFERENCE
- void
- bid128_minnum (UINT128 * pres, UINT128 * px,
- UINT128 * py _EXC_FLAGS_PARAM) {
- UINT128 x = *px;
- UINT128 y = *py;
- #else
- UINT128
- bid128_minnum (UINT128 x, UINT128 y _EXC_FLAGS_PARAM) {
- #endif
- UINT128 res;
- int exp_x, exp_y;
- int diff;
- UINT128 sig_x, sig_y;
- UINT192 sig_n_prime192;
- UINT256 sig_n_prime256;
- char x_is_zero = 0, y_is_zero = 0;
- BID_SWAP128 (x);
- BID_SWAP128 (y);
- // check for non-canonical x
- if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
- x.w[1] = x.w[1] & 0xfe003fffffffffffull; // clear out G[6]-G[16]
- // check for non-canonical NaN payload
- if (((x.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
- (((x.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
- (x.w[0] > 0x38c15b09ffffffffull))) {
- x.w[1] = x.w[1] & 0xffffc00000000000ull;
- x.w[0] = 0x0ull;
- }
- } else if ((x.w[1] & MASK_ANY_INF) == MASK_INF) { // x = inf
- x.w[1] = x.w[1] & (MASK_SIGN | MASK_INF);
- x.w[0] = 0x0ull;
- } else { // x is not special
- // check for non-canonical values - treated as zero
- if ((x.w[1] & MASK_STEERING_BITS) == MASK_STEERING_BITS) { // G0_G1=11
- // non-canonical
- x.w[1] = (x.w[1] & MASK_SIGN) | ((x.w[1] << 2) & MASK_EXP);
- x.w[0] = 0x0ull;
- } else { // G0_G1 != 11
- if ((x.w[1] & MASK_COEFF) > 0x0001ed09bead87c0ull ||
- ((x.w[1] & MASK_COEFF) == 0x0001ed09bead87c0ull
- && x.w[0] > 0x378d8e63ffffffffull)) {
- // x is non-canonical if coefficient is larger than 10^34 -1
- x.w[1] = (x.w[1] & MASK_SIGN) | (x.w[1] & MASK_EXP);
- x.w[0] = 0x0ull;
- } else { // canonical
- ;
- }
- }
- }
- // check for non-canonical y
- if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN
- y.w[1] = y.w[1] & 0xfe003fffffffffffull; // clear out G[6]-G[16]
- // check for non-canonical NaN payload
- if (((y.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
- (((y.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
- (y.w[0] > 0x38c15b09ffffffffull))) {
- y.w[1] = y.w[1] & 0xffffc00000000000ull;
- y.w[0] = 0x0ull;
- }
- } else if ((y.w[1] & MASK_ANY_INF) == MASK_INF) { // y = inf
- y.w[1] = y.w[1] & (MASK_SIGN | MASK_INF);
- y.w[0] = 0x0ull;
- } else { // y is not special
- // check for non-canonical values - treated as zero
- if ((y.w[1] & MASK_STEERING_BITS) == MASK_STEERING_BITS) { // G0_G1=11
- // non-canonical
- y.w[1] = (y.w[1] & MASK_SIGN) | ((y.w[1] << 2) & MASK_EXP);
- y.w[0] = 0x0ull;
- } else { // G0_G1 != 11
- if ((y.w[1] & MASK_COEFF) > 0x0001ed09bead87c0ull ||
- ((y.w[1] & MASK_COEFF) == 0x0001ed09bead87c0ull
- && y.w[0] > 0x378d8e63ffffffffull)) {
- // y is non-canonical if coefficient is larger than 10^34 -1
- y.w[1] = (y.w[1] & MASK_SIGN) | (y.w[1] & MASK_EXP);
- y.w[0] = 0x0ull;
- } else { // canonical
- ;
- }
- }
- }
- // NaN (CASE1)
- if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
- if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNaN
- // if x is SNAN, then return quiet (x)
- *pfpsf |= INVALID_EXCEPTION; // set exception if SNaN
- x.w[1] = x.w[1] & 0xfdffffffffffffffull; // quietize x
- res = x;
- } else { // x is QNaN
- if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN
- if ((y.w[1] & MASK_SNAN) == MASK_SNAN) { // y is SNAN
- *pfpsf |= INVALID_EXCEPTION; // set invalid flag
- }
- res = x;
- } else {
- res = y;
- }
- }
- BID_RETURN (res);
- } else if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NaN, but x is not
- if ((y.w[1] & MASK_SNAN) == MASK_SNAN) {
- *pfpsf |= INVALID_EXCEPTION; // set exception if SNaN
- y.w[1] = y.w[1] & 0xfdffffffffffffffull; // quietize y
- res = y;
- } else {
- // will return x (which is not NaN)
- res = x;
- }
- BID_RETURN (res);
- }
- // SIMPLE (CASE2)
- // if all the bits are the same, these numbers are equal (not Greater).
- if (x.w[0] == y.w[0] && x.w[1] == y.w[1]) {
- res = x;
- BID_RETURN (res);
- }
- // INFINITY (CASE3)
- if ((x.w[1] & MASK_INF) == MASK_INF) {
- // if x is neg infinity, there is no way it is greater than y, return 0
- res = (((x.w[1] & MASK_SIGN) == MASK_SIGN)) ? x : y;
- BID_RETURN (res);
- } else if ((y.w[1] & MASK_INF) == MASK_INF) {
- // x is finite, so if y is positive infinity, then x is less, return 0
- // if y is negative infinity, then x is greater, return 1
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? y : x;
- BID_RETURN (res);
- }
- // CONVERT X
- sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
- sig_x.w[0] = x.w[0];
- exp_x = (x.w[1] >> 49) & 0x000000000003fffull;
- // CONVERT Y
- exp_y = (y.w[1] >> 49) & 0x0000000000003fffull;
- sig_y.w[1] = y.w[1] & 0x0001ffffffffffffull;
- sig_y.w[0] = y.w[0];
- // ZERO (CASE4)
- // some properties:
- // (+ZERO == -ZERO) => therefore ignore the sign
- // (ZERO x 10^A == ZERO x 10^B) for any valid A, B => ignore the exponent
- // field
- // (Any non-canonical # is considered 0)
- if ((sig_x.w[1] == 0) && (sig_x.w[0] == 0)) {
- x_is_zero = 1;
- }
- if ((sig_y.w[1] == 0) && (sig_y.w[0] == 0)) {
- y_is_zero = 1;
- }
- if (x_is_zero && y_is_zero) {
- // if both numbers are zero, neither is greater => return either number
- res = x;
- BID_RETURN (res);
- } else if (x_is_zero) {
- // is x is zero, it is greater if Y is negative
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? y : x;
- BID_RETURN (res);
- } else if (y_is_zero) {
- // is y is zero, X is greater if it is positive
- res = ((x.w[1] & MASK_SIGN) != MASK_SIGN) ? y : x;
- BID_RETURN (res);
- }
- // OPPOSITE SIGN (CASE5)
- // now, if the sign bits differ, x is greater if y is negative
- if (((x.w[1] ^ y.w[1]) & MASK_SIGN) == MASK_SIGN) {
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? y : x;
- BID_RETURN (res);
- }
- // REDUNDANT REPRESENTATIONS (CASE6)
- // if exponents are the same, then we have a simple comparison of
- // the significands
- if (exp_y == exp_x) {
- res = (((sig_x.w[1] > sig_y.w[1])
- || (sig_x.w[1] == sig_y.w[1]
- && sig_x.w[0] >= sig_y.w[0])) ^ ((x.w[1] & MASK_SIGN) ==
- MASK_SIGN)) ? y : x;
- BID_RETURN (res);
- }
- // if both components are either bigger or smaller, it is clear what
- // needs to be done
- if (sig_x.w[1] >= sig_y.w[1] && sig_x.w[0] >= sig_y.w[0]
- && exp_x > exp_y) {
- res = ((x.w[1] & MASK_SIGN) != MASK_SIGN) ? y : x;
- BID_RETURN (res);
- }
- if (sig_x.w[1] <= sig_y.w[1] && sig_x.w[0] <= sig_y.w[0]
- && exp_x < exp_y) {
- res = ((x.w[1] & MASK_SIGN) == MASK_SIGN) ? y : x;
- BID_RETURN (res);
- }
- diff = exp_x - exp_y;
- // if |exp_x - exp_y| < 33, it comes down to the compensated significand
- if (diff > 0) { // to simplify the loop below,
- // if exp_x is 33 greater than exp_y, no need for compensation
- if (diff > 33) {
- // difference cannot be greater than 10^33
- res = ((x.w[1] & MASK_SIGN) != MASK_SIGN) ? y : x;
- BID_RETURN (res);
- }
- if (diff > 19) { //128 by 128 bit multiply -> 256 bits
- __mul_128x128_to_256 (sig_n_prime256, sig_x, ten2k128[diff - 20]);
- // if postitive, return whichever significand is larger
- // (converse if negative)
- res = ((((sig_n_prime256.w[3] > 0) || sig_n_prime256.w[2] > 0)
- || (sig_n_prime256.w[1] > sig_y.w[1])
- || (sig_n_prime256.w[1] == sig_y.w[1]
- && sig_n_prime256.w[0] >
- sig_y.w[0])) ^ ((y.w[1] & MASK_SIGN) ==
- MASK_SIGN)) ? y : x;
- BID_RETURN (res);
- }
- __mul_64x128_to_192 (sig_n_prime192, ten2k64[diff], sig_x);
- // if postitive, return whichever significand is larger
- // (converse if negative)
- res =
- (((sig_n_prime192.w[2] > 0) || (sig_n_prime192.w[1] > sig_y.w[1])
- || (sig_n_prime192.w[1] == sig_y.w[1]
- && sig_n_prime192.w[0] >
- sig_y.w[0])) ^ ((y.w[1] & MASK_SIGN) == MASK_SIGN)) ? y : x;
- BID_RETURN (res);
- }
- diff = exp_y - exp_x;
- // if exp_x is 33 less than exp_y, no need for compensation
- if (diff > 33) {
- res = ((x.w[1] & MASK_SIGN) == MASK_SIGN) ? y : x;
- BID_RETURN (res);
- }
- if (diff > 19) { //128 by 128 bit multiply -> 256 bits
- // adjust the y significand upwards
- __mul_128x128_to_256 (sig_n_prime256, sig_y, ten2k128[diff - 20]);
- // if postitive, return whichever significand is larger
- // (converse if negative)
- res =
- ((sig_n_prime256.w[3] != 0 || sig_n_prime256.w[2] != 0
- || (sig_n_prime256.w[1] > sig_x.w[1]
- || (sig_n_prime256.w[1] == sig_x.w[1]
- && sig_n_prime256.w[0] >
- sig_x.w[0]))) ^ ((x.w[1] & MASK_SIGN) ==
- MASK_SIGN)) ? x : y;
- BID_RETURN (res);
- }
- // adjust the y significand upwards
- __mul_64x128_to_192 (sig_n_prime192, ten2k64[diff], sig_y);
- // if postitive, return whichever significand is larger (converse if negative)
- res =
- ((sig_n_prime192.w[2] != 0
- || (sig_n_prime192.w[1] > sig_x.w[1]
- || (sig_n_prime192.w[1] == sig_x.w[1]
- && sig_n_prime192.w[0] > sig_x.w[0])))
- ^ ((y.w[1] & MASK_SIGN) == MASK_SIGN)) ? x : y;
- BID_RETURN (res);
- }
- /*****************************************************************************
- * BID128 minimum magnitude function - returns greater of two numbers
- *****************************************************************************/
- #if DECIMAL_CALL_BY_REFERENCE
- void
- bid128_minnum_mag (UINT128 * pres, UINT128 * px,
- UINT128 * py _EXC_FLAGS_PARAM) {
- UINT128 x = *px;
- UINT128 y = *py;
- #else
- UINT128
- bid128_minnum_mag (UINT128 x, UINT128 y _EXC_FLAGS_PARAM) {
- #endif
- UINT128 res;
- int exp_x, exp_y;
- int diff;
- UINT128 sig_x, sig_y;
- UINT192 sig_n_prime192;
- UINT256 sig_n_prime256;
- BID_SWAP128 (x);
- BID_SWAP128 (y);
- // check for non-canonical x
- if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
- x.w[1] = x.w[1] & 0xfe003fffffffffffull; // clear out G[6]-G[16]
- // check for non-canonical NaN payload
- if (((x.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
- (((x.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
- (x.w[0] > 0x38c15b09ffffffffull))) {
- x.w[1] = x.w[1] & 0xffffc00000000000ull;
- x.w[0] = 0x0ull;
- }
- } else if ((x.w[1] & MASK_ANY_INF) == MASK_INF) { // x = inf
- x.w[1] = x.w[1] & (MASK_SIGN | MASK_INF);
- x.w[0] = 0x0ull;
- } else { // x is not special
- // check for non-canonical values - treated as zero
- if ((x.w[1] & MASK_STEERING_BITS) == MASK_STEERING_BITS) { // G0_G1=11
- // non-canonical
- x.w[1] = (x.w[1] & MASK_SIGN) | ((x.w[1] << 2) & MASK_EXP);
- x.w[0] = 0x0ull;
- } else { // G0_G1 != 11
- if ((x.w[1] & MASK_COEFF) > 0x0001ed09bead87c0ull ||
- ((x.w[1] & MASK_COEFF) == 0x0001ed09bead87c0ull
- && x.w[0] > 0x378d8e63ffffffffull)) {
- // x is non-canonical if coefficient is larger than 10^34 -1
- x.w[1] = (x.w[1] & MASK_SIGN) | (x.w[1] & MASK_EXP);
- x.w[0] = 0x0ull;
- } else { // canonical
- ;
- }
- }
- }
- // check for non-canonical y
- if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN
- y.w[1] = y.w[1] & 0xfe003fffffffffffull; // clear out G[6]-G[16]
- // check for non-canonical NaN payload
- if (((y.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
- (((y.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
- (y.w[0] > 0x38c15b09ffffffffull))) {
- y.w[1] = y.w[1] & 0xffffc00000000000ull;
- y.w[0] = 0x0ull;
- }
- } else if ((y.w[1] & MASK_ANY_INF) == MASK_INF) { // y = inf
- y.w[1] = y.w[1] & (MASK_SIGN | MASK_INF);
- y.w[0] = 0x0ull;
- } else { // y is not special
- // check for non-canonical values - treated as zero
- if ((y.w[1] & MASK_STEERING_BITS) == MASK_STEERING_BITS) { // G0_G1=11
- // non-canonical
- y.w[1] = (y.w[1] & MASK_SIGN) | ((y.w[1] << 2) & MASK_EXP);
- y.w[0] = 0x0ull;
- } else { // G0_G1 != 11
- if ((y.w[1] & MASK_COEFF) > 0x0001ed09bead87c0ull ||
- ((y.w[1] & MASK_COEFF) == 0x0001ed09bead87c0ull
- && y.w[0] > 0x378d8e63ffffffffull)) {
- // y is non-canonical if coefficient is larger than 10^34 -1
- y.w[1] = (y.w[1] & MASK_SIGN) | (y.w[1] & MASK_EXP);
- y.w[0] = 0x0ull;
- } else { // canonical
- ;
- }
- }
- }
- // NaN (CASE1)
- if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
- if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNaN
- // if x is SNAN, then return quiet (x)
- *pfpsf |= INVALID_EXCEPTION; // set exception if SNaN
- x.w[1] = x.w[1] & 0xfdffffffffffffffull; // quietize x
- res = x;
- } else { // x is QNaN
- if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN
- if ((y.w[1] & MASK_SNAN) == MASK_SNAN) { // y is SNAN
- *pfpsf |= INVALID_EXCEPTION; // set invalid flag
- }
- res = x;
- } else {
- res = y;
- }
- }
- BID_RETURN (res);
- } else if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NaN, but x is not
- if ((y.w[1] & MASK_SNAN) == MASK_SNAN) {
- *pfpsf |= INVALID_EXCEPTION; // set exception if SNaN
- y.w[1] = y.w[1] & 0xfdffffffffffffffull; // quietize y
- res = y;
- } else {
- // will return x (which is not NaN)
- res = x;
- }
- BID_RETURN (res);
- }
- // SIMPLE (CASE2)
- // if all the bits are the same, these numbers are equal (not Greater).
- if (x.w[0] == y.w[0] && x.w[1] == y.w[1]) {
- res = y;
- BID_RETURN (res);
- }
- // INFINITY (CASE3)
- if ((x.w[1] & MASK_INF) == MASK_INF) {
- // if x infinity, it has maximum magnitude.
- // Check if magnitudes are equal. If x is negative, return it.
- res = ((x.w[1] & MASK_SIGN) == MASK_SIGN
- && (y.w[1] & MASK_INF) == MASK_INF) ? x : y;
- BID_RETURN (res);
- } else if ((y.w[1] & MASK_INF) == MASK_INF) {
- // x is finite, so if y is infinity, then x is less in magnitude
- res = x;
- BID_RETURN (res);
- }
- // CONVERT X
- sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
- sig_x.w[0] = x.w[0];
- exp_x = (x.w[1] >> 49) & 0x000000000003fffull;
- // CONVERT Y
- exp_y = (y.w[1] >> 49) & 0x0000000000003fffull;
- sig_y.w[1] = y.w[1] & 0x0001ffffffffffffull;
- sig_y.w[0] = y.w[0];
- // ZERO (CASE4)
- // some properties:
- // (+ZERO == -ZERO) => therefore ignore the sign
- // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
- // therefore ignore the exponent field
- // (Any non-canonical # is considered 0)
- if ((sig_x.w[1] == 0) && (sig_x.w[0] == 0)) {
- res = x;
- BID_RETURN (res);
- }
- if ((sig_y.w[1] == 0) && (sig_y.w[0] == 0)) {
- res = y;
- BID_RETURN (res);
- }
- // REDUNDANT REPRESENTATIONS (CASE6)
- // check if exponents are the same and significands are the same
- if (exp_y == exp_x && sig_x.w[1] == sig_y.w[1]
- && sig_x.w[0] == sig_y.w[0]) {
- if (x.w[1] & 0x8000000000000000ull) { // x is negative
- res = x;
- BID_RETURN (res);
- } else {
- res = y;
- BID_RETURN (res);
- }
- } else if (((sig_x.w[1] > sig_y.w[1] || (sig_x.w[1] == sig_y.w[1]
- && sig_x.w[0] > sig_y.w[0]))
- && exp_x == exp_y)
- || ((sig_x.w[1] > sig_y.w[1]
- || (sig_x.w[1] == sig_y.w[1]
- && sig_x.w[0] >= sig_y.w[0]))
- && exp_x > exp_y)) {
- // if both components are either bigger or smaller, it is clear what
- // needs to be done; also if the magnitudes are equal
- res = y;
- BID_RETURN (res);
- } else if (((sig_y.w[1] > sig_x.w[1] || (sig_y.w[1] == sig_x.w[1]
- && sig_y.w[0] > sig_x.w[0]))
- && exp_y == exp_x)
- || ((sig_y.w[1] > sig_x.w[1]
- || (sig_y.w[1] == sig_x.w[1]
- && sig_y.w[0] >= sig_x.w[0]))
- && exp_y > exp_x)) {
- res = x;
- BID_RETURN (res);
- } else {
- ; // continue
- }
- diff = exp_x - exp_y;
- // if |exp_x - exp_y| < 33, it comes down to the compensated significand
- if (diff > 0) { // to simplify the loop below,
- // if exp_x is 33 greater than exp_y, no need for compensation
- if (diff > 33) {
- res = y; // difference cannot be greater than 10^33
- BID_RETURN (res);
- }
- if (diff > 19) { //128 by 128 bit multiply -> 256 bits
- __mul_128x128_to_256 (sig_n_prime256, sig_x, ten2k128[diff - 20]);
- // if positive, return whichever significand is larger
- // (converse if negative)
- if (sig_n_prime256.w[3] == 0 && (sig_n_prime256.w[2] == 0)
- && sig_n_prime256.w[1] == sig_y.w[1]
- && (sig_n_prime256.w[0] == sig_y.w[0])) {
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? y : x; // if equal
- BID_RETURN (res);
- }
- res = (((sig_n_prime256.w[3] > 0) || sig_n_prime256.w[2] > 0)
- || (sig_n_prime256.w[1] > sig_y.w[1])
- || (sig_n_prime256.w[1] == sig_y.w[1]
- && sig_n_prime256.w[0] > sig_y.w[0])) ? y : x;
- BID_RETURN (res);
- }
- __mul_64x128_to_192 (sig_n_prime192, ten2k64[diff], sig_x);
- // if positive, return whichever significand is larger
- // (converse if negative)
- if ((sig_n_prime192.w[2] == 0) && sig_n_prime192.w[1] == sig_y.w[1]
- && (sig_n_prime192.w[0] == sig_y.w[0])) {
- // if = in magnitude, return +, (if possible)
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? y : x;
- BID_RETURN (res);
- }
- res = ((sig_n_prime192.w[2] > 0)
- || (sig_n_prime192.w[1] > sig_y.w[1])
- || (sig_n_prime192.w[1] == sig_y.w[1]
- && sig_n_prime192.w[0] > sig_y.w[0])) ? y : x;
- BID_RETURN (res);
- }
- diff = exp_y - exp_x;
- // if exp_x is 33 less than exp_y, no need for compensation
- if (diff > 33) {
- res = x;
- BID_RETURN (res);
- }
- if (diff > 19) { //128 by 128 bit multiply -> 256 bits
- // adjust the y significand upwards
- __mul_128x128_to_256 (sig_n_prime256, sig_y, ten2k128[diff - 20]);
- // if positive, return whichever significand is larger
- // (converse if negative)
- if (sig_n_prime256.w[3] == 0 && (sig_n_prime256.w[2] == 0)
- && sig_n_prime256.w[1] == sig_x.w[1]
- && (sig_n_prime256.w[0] == sig_x.w[0])) {
- // if = in magnitude, return +, (if possible)
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? y : x;
- BID_RETURN (res);
- }
- res = (sig_n_prime256.w[3] == 0 && sig_n_prime256.w[2] == 0
- && (sig_n_prime256.w[1] < sig_x.w[1]
- || (sig_n_prime256.w[1] == sig_x.w[1]
- && sig_n_prime256.w[0] < sig_x.w[0]))) ? y : x;
- BID_RETURN (res);
- }
- // adjust the y significand upwards
- __mul_64x128_to_192 (sig_n_prime192, ten2k64[diff], sig_y);
- // if positive, return whichever significand is larger (converse if negative)
- if ((sig_n_prime192.w[2] == 0) && sig_n_prime192.w[1] == sig_x.w[1]
- && (sig_n_prime192.w[0] == sig_x.w[0])) {
- // if = in magnitude, return +, if possible)
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? y : x;
- BID_RETURN (res);
- }
- res = (sig_n_prime192.w[2] == 0
- && (sig_n_prime192.w[1] < sig_x.w[1]
- || (sig_n_prime192.w[1] == sig_x.w[1]
- && sig_n_prime192.w[0] < sig_x.w[0]))) ? y : x;
- BID_RETURN (res);
- }
- /*****************************************************************************
- * BID128 maximum function - returns greater of two numbers
- *****************************************************************************/
- #if DECIMAL_CALL_BY_REFERENCE
- void
- bid128_maxnum (UINT128 * pres, UINT128 * px,
- UINT128 * py _EXC_FLAGS_PARAM) {
- UINT128 x = *px;
- UINT128 y = *py;
- #else
- UINT128
- bid128_maxnum (UINT128 x, UINT128 y _EXC_FLAGS_PARAM) {
- #endif
- UINT128 res;
- int exp_x, exp_y;
- int diff;
- UINT128 sig_x, sig_y;
- UINT192 sig_n_prime192;
- UINT256 sig_n_prime256;
- char x_is_zero = 0, y_is_zero = 0;
- BID_SWAP128 (x);
- BID_SWAP128 (y);
- // check for non-canonical x
- if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
- x.w[1] = x.w[1] & 0xfe003fffffffffffull; // clear out G[6]-G[16]
- // check for non-canonical NaN payload
- if (((x.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
- (((x.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
- (x.w[0] > 0x38c15b09ffffffffull))) {
- x.w[1] = x.w[1] & 0xffffc00000000000ull;
- x.w[0] = 0x0ull;
- }
- } else if ((x.w[1] & MASK_ANY_INF) == MASK_INF) { // x = inf
- x.w[1] = x.w[1] & (MASK_SIGN | MASK_INF);
- x.w[0] = 0x0ull;
- } else { // x is not special
- // check for non-canonical values - treated as zero
- if ((x.w[1] & MASK_STEERING_BITS) == MASK_STEERING_BITS) { // G0_G1=11
- // non-canonical
- x.w[1] = (x.w[1] & MASK_SIGN) | ((x.w[1] << 2) & MASK_EXP);
- x.w[0] = 0x0ull;
- } else { // G0_G1 != 11
- if ((x.w[1] & MASK_COEFF) > 0x0001ed09bead87c0ull ||
- ((x.w[1] & MASK_COEFF) == 0x0001ed09bead87c0ull
- && x.w[0] > 0x378d8e63ffffffffull)) {
- // x is non-canonical if coefficient is larger than 10^34 -1
- x.w[1] = (x.w[1] & MASK_SIGN) | (x.w[1] & MASK_EXP);
- x.w[0] = 0x0ull;
- } else { // canonical
- ;
- }
- }
- }
- // check for non-canonical y
- if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN
- y.w[1] = y.w[1] & 0xfe003fffffffffffull; // clear out G[6]-G[16]
- // check for non-canonical NaN payload
- if (((y.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
- (((y.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
- (y.w[0] > 0x38c15b09ffffffffull))) {
- y.w[1] = y.w[1] & 0xffffc00000000000ull;
- y.w[0] = 0x0ull;
- }
- } else if ((y.w[1] & MASK_ANY_INF) == MASK_INF) { // y = inf
- y.w[1] = y.w[1] & (MASK_SIGN | MASK_INF);
- y.w[0] = 0x0ull;
- } else { // y is not special
- // check for non-canonical values - treated as zero
- if ((y.w[1] & MASK_STEERING_BITS) == MASK_STEERING_BITS) { // G0_G1=11
- // non-canonical
- y.w[1] = (y.w[1] & MASK_SIGN) | ((y.w[1] << 2) & MASK_EXP);
- y.w[0] = 0x0ull;
- } else { // G0_G1 != 11
- if ((y.w[1] & MASK_COEFF) > 0x0001ed09bead87c0ull ||
- ((y.w[1] & MASK_COEFF) == 0x0001ed09bead87c0ull
- && y.w[0] > 0x378d8e63ffffffffull)) {
- // y is non-canonical if coefficient is larger than 10^34 -1
- y.w[1] = (y.w[1] & MASK_SIGN) | (y.w[1] & MASK_EXP);
- y.w[0] = 0x0ull;
- } else { // canonical
- ;
- }
- }
- }
- // NaN (CASE1)
- if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
- if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNaN
- // if x is SNAN, then return quiet (x)
- *pfpsf |= INVALID_EXCEPTION; // set exception if SNaN
- x.w[1] = x.w[1] & 0xfdffffffffffffffull; // quietize x
- res = x;
- } else { // x is QNaN
- if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN
- if ((y.w[1] & MASK_SNAN) == MASK_SNAN) { // y is SNAN
- *pfpsf |= INVALID_EXCEPTION; // set invalid flag
- }
- res = x;
- } else {
- res = y;
- }
- }
- BID_RETURN (res);
- } else if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NaN, but x is not
- if ((y.w[1] & MASK_SNAN) == MASK_SNAN) {
- *pfpsf |= INVALID_EXCEPTION; // set exception if SNaN
- y.w[1] = y.w[1] & 0xfdffffffffffffffull; // quietize y
- res = y;
- } else {
- // will return x (which is not NaN)
- res = x;
- }
- BID_RETURN (res);
- }
- // SIMPLE (CASE2)
- // if all the bits are the same, these numbers are equal (not Greater).
- if (x.w[0] == y.w[0] && x.w[1] == y.w[1]) {
- res = x;
- BID_RETURN (res);
- }
- // INFINITY (CASE3)
- if ((x.w[1] & MASK_INF) == MASK_INF) {
- res = ((x.w[1] & MASK_SIGN) == MASK_SIGN) ? y : x;
- BID_RETURN (res);
- } else if ((y.w[1] & MASK_INF) == MASK_INF) {
- // x is finite, so if y is positive infinity, then x is less, return 0
- // if y is negative infinity, then x is greater, return 1
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? x : y;
- BID_RETURN (res);
- }
- // CONVERT X
- sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
- sig_x.w[0] = x.w[0];
- exp_x = (x.w[1] >> 49) & 0x000000000003fffull;
- // CONVERT Y
- exp_y = (y.w[1] >> 49) & 0x0000000000003fffull;
- sig_y.w[1] = y.w[1] & 0x0001ffffffffffffull;
- sig_y.w[0] = y.w[0];
- // ZERO (CASE4)
- // some properties:
- // (+ZERO == -ZERO) => therefore ignore the sign
- // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
- // therefore ignore the exponent field
- // (Any non-canonical # is considered 0)
- if ((sig_x.w[1] == 0) && (sig_x.w[0] == 0)) {
- x_is_zero = 1;
- }
- if ((sig_y.w[1] == 0) && (sig_y.w[0] == 0)) {
- y_is_zero = 1;
- }
- if (x_is_zero && y_is_zero) {
- // if both numbers are zero, neither is greater => return either number
- res = x;
- BID_RETURN (res);
- } else if (x_is_zero) {
- // is x is zero, it is greater if Y is negative
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? x : y;
- BID_RETURN (res);
- } else if (y_is_zero) {
- // is y is zero, X is greater if it is positive
- res = ((x.w[1] & MASK_SIGN) != MASK_SIGN) ? x : y;
- BID_RETURN (res);
- }
- // OPPOSITE SIGN (CASE5)
- // now, if the sign bits differ, x is greater if y is negative
- if (((x.w[1] ^ y.w[1]) & MASK_SIGN) == MASK_SIGN) {
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? x : y;
- BID_RETURN (res);
- }
- // REDUNDANT REPRESENTATIONS (CASE6)
- // if exponents are the same, then we have a simple comparison of
- // the significands
- if (exp_y == exp_x) {
- res = (((sig_x.w[1] > sig_y.w[1]) || (sig_x.w[1] == sig_y.w[1] &&
- sig_x.w[0] >= sig_y.w[0])) ^
- ((x.w[1] & MASK_SIGN) == MASK_SIGN)) ? x : y;
- BID_RETURN (res);
- }
- // if both components are either bigger or smaller, it is clear what
- // needs to be done
- if ((sig_x.w[1] > sig_y.w[1]
- || (sig_x.w[1] == sig_y.w[1] && sig_x.w[0] > sig_y.w[0]))
- && exp_x >= exp_y) {
- res = ((x.w[1] & MASK_SIGN) != MASK_SIGN) ? x : y;
- BID_RETURN (res);
- }
- if ((sig_x.w[1] < sig_y.w[1]
- || (sig_x.w[1] == sig_y.w[1] && sig_x.w[0] < sig_y.w[0]))
- && exp_x <= exp_y) {
- res = ((x.w[1] & MASK_SIGN) == MASK_SIGN) ? x : y;
- BID_RETURN (res);
- }
- diff = exp_x - exp_y;
- // if |exp_x - exp_y| < 33, it comes down to the compensated significand
- if (diff > 0) { // to simplify the loop below,
- // if exp_x is 33 greater than exp_y, no need for compensation
- if (diff > 33) {
- // difference cannot be greater than 10^33
- res = ((x.w[1] & MASK_SIGN) != MASK_SIGN) ? x : y;
- BID_RETURN (res);
- }
- if (diff > 19) { //128 by 128 bit multiply -> 256 bits
- __mul_128x128_to_256 (sig_n_prime256, sig_x, ten2k128[diff - 20]);
- // if postitive, return whichever significand is larger
- // (converse if negative)
- res = ((((sig_n_prime256.w[3] > 0) || sig_n_prime256.w[2] > 0)
- || (sig_n_prime256.w[1] > sig_y.w[1])
- || (sig_n_prime256.w[1] == sig_y.w[1]
- && sig_n_prime256.w[0] >
- sig_y.w[0])) ^ ((y.w[1] & MASK_SIGN) ==
- MASK_SIGN)) ? x : y;
- BID_RETURN (res);
- }
- __mul_64x128_to_192 (sig_n_prime192, ten2k64[diff], sig_x);
- // if postitive, return whichever significand is larger
- // (converse if negative)
- res =
- (((sig_n_prime192.w[2] > 0) || (sig_n_prime192.w[1] > sig_y.w[1])
- || (sig_n_prime192.w[1] == sig_y.w[1]
- && sig_n_prime192.w[0] >
- sig_y.w[0])) ^ ((y.w[1] & MASK_SIGN) == MASK_SIGN)) ? x : y;
- BID_RETURN (res);
- }
- diff = exp_y - exp_x;
- // if exp_x is 33 less than exp_y, no need for compensation
- if (diff > 33) {
- res = ((x.w[1] & MASK_SIGN) == MASK_SIGN) ? x : y;
- BID_RETURN (res);
- }
- if (diff > 19) { //128 by 128 bit multiply -> 256 bits
- // adjust the y significand upwards
- __mul_128x128_to_256 (sig_n_prime256, sig_y, ten2k128[diff - 20]);
- // if postitive, return whichever significand is larger
- // (converse if negative)
- res =
- ((sig_n_prime256.w[3] != 0 || sig_n_prime256.w[2] != 0
- || (sig_n_prime256.w[1] > sig_x.w[1]
- || (sig_n_prime256.w[1] == sig_x.w[1]
- && sig_n_prime256.w[0] >
- sig_x.w[0]))) ^ ((x.w[1] & MASK_SIGN) !=
- MASK_SIGN)) ? x : y;
- BID_RETURN (res);
- }
- // adjust the y significand upwards
- __mul_64x128_to_192 (sig_n_prime192, ten2k64[diff], sig_y);
- // if postitive, return whichever significand is larger (converse if negative)
- res =
- ((sig_n_prime192.w[2] != 0
- || (sig_n_prime192.w[1] > sig_x.w[1]
- || (sig_n_prime192.w[1] == sig_x.w[1]
- && sig_n_prime192.w[0] >
- sig_x.w[0]))) ^ ((y.w[1] & MASK_SIGN) !=
- MASK_SIGN)) ? x : y;
- BID_RETURN (res);
- }
- /*****************************************************************************
- * BID128 maximum magnitude function - returns greater of two numbers
- *****************************************************************************/
- #if DECIMAL_CALL_BY_REFERENCE
- void
- bid128_maxnum_mag (UINT128 * pres, UINT128 * px,
- UINT128 * py _EXC_FLAGS_PARAM) {
- UINT128 x = *px;
- UINT128 y = *py;
- #else
- UINT128
- bid128_maxnum_mag (UINT128 x, UINT128 y _EXC_FLAGS_PARAM) {
- #endif
- UINT128 res;
- int exp_x, exp_y;
- int diff;
- UINT128 sig_x, sig_y;
- UINT192 sig_n_prime192;
- UINT256 sig_n_prime256;
- BID_SWAP128 (x);
- BID_SWAP128 (y);
- // check for non-canonical x
- if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
- x.w[1] = x.w[1] & 0xfe003fffffffffffull; // clear out G[6]-G[16]
- // check for non-canonical NaN payload
- if (((x.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
- (((x.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
- (x.w[0] > 0x38c15b09ffffffffull))) {
- x.w[1] = x.w[1] & 0xffffc00000000000ull;
- x.w[0] = 0x0ull;
- }
- } else if ((x.w[1] & MASK_ANY_INF) == MASK_INF) { // x = inf
- x.w[1] = x.w[1] & (MASK_SIGN | MASK_INF);
- x.w[0] = 0x0ull;
- } else { // x is not special
- // check for non-canonical values - treated as zero
- if ((x.w[1] & MASK_STEERING_BITS) == MASK_STEERING_BITS) { // G0_G1=11
- // non-canonical
- x.w[1] = (x.w[1] & MASK_SIGN) | ((x.w[1] << 2) & MASK_EXP);
- x.w[0] = 0x0ull;
- } else { // G0_G1 != 11
- if ((x.w[1] & MASK_COEFF) > 0x0001ed09bead87c0ull ||
- ((x.w[1] & MASK_COEFF) == 0x0001ed09bead87c0ull
- && x.w[0] > 0x378d8e63ffffffffull)) {
- // x is non-canonical if coefficient is larger than 10^34 -1
- x.w[1] = (x.w[1] & MASK_SIGN) | (x.w[1] & MASK_EXP);
- x.w[0] = 0x0ull;
- } else { // canonical
- ;
- }
- }
- }
- // check for non-canonical y
- if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN
- y.w[1] = y.w[1] & 0xfe003fffffffffffull; // clear out G[6]-G[16]
- // check for non-canonical NaN payload
- if (((y.w[1] & 0x00003fffffffffffull) > 0x0000314dc6448d93ull) ||
- (((y.w[1] & 0x00003fffffffffffull) == 0x0000314dc6448d93ull) &&
- (y.w[0] > 0x38c15b09ffffffffull))) {
- y.w[1] = y.w[1] & 0xffffc00000000000ull;
- y.w[0] = 0x0ull;
- }
- } else if ((y.w[1] & MASK_ANY_INF) == MASK_INF) { // y = inf
- y.w[1] = y.w[1] & (MASK_SIGN | MASK_INF);
- y.w[0] = 0x0ull;
- } else { // y is not special
- // check for non-canonical values - treated as zero
- if ((y.w[1] & MASK_STEERING_BITS) == MASK_STEERING_BITS) { // G0_G1=11
- // non-canonical
- y.w[1] = (y.w[1] & MASK_SIGN) | ((y.w[1] << 2) & MASK_EXP);
- y.w[0] = 0x0ull;
- } else { // G0_G1 != 11
- if ((y.w[1] & MASK_COEFF) > 0x0001ed09bead87c0ull ||
- ((y.w[1] & MASK_COEFF) == 0x0001ed09bead87c0ull &&
- y.w[0] > 0x378d8e63ffffffffull)) {
- // y is non-canonical if coefficient is larger than 10^34 -1
- y.w[1] = (y.w[1] & MASK_SIGN) | (y.w[1] & MASK_EXP);
- y.w[0] = 0x0ull;
- } else { // canonical
- ;
- }
- }
- }
- // NaN (CASE1)
- if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
- if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNaN
- // if x is SNAN, then return quiet (x)
- *pfpsf |= INVALID_EXCEPTION; // set exception if SNaN
- x.w[1] = x.w[1] & 0xfdffffffffffffffull; // quietize x
- res = x;
- } else { // x is QNaN
- if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NAN
- if ((y.w[1] & MASK_SNAN) == MASK_SNAN) { // y is SNAN
- *pfpsf |= INVALID_EXCEPTION; // set invalid flag
- }
- res = x;
- } else {
- res = y;
- }
- }
- BID_RETURN (res);
- } else if ((y.w[1] & MASK_NAN) == MASK_NAN) { // y is NaN, but x is not
- if ((y.w[1] & MASK_SNAN) == MASK_SNAN) {
- *pfpsf |= INVALID_EXCEPTION; // set exception if SNaN
- y.w[1] = y.w[1] & 0xfdffffffffffffffull; // quietize y
- res = y;
- } else {
- // will return x (which is not NaN)
- res = x;
- }
- BID_RETURN (res);
- }
- // SIMPLE (CASE2)
- // if all the bits are the same, these numbers are equal (not Greater).
- if (x.w[0] == y.w[0] && x.w[1] == y.w[1]) {
- res = y;
- BID_RETURN (res);
- }
- // INFINITY (CASE3)
- if ((x.w[1] & MASK_INF) == MASK_INF) {
- // if x infinity, it has maximum magnitude
- res = ((x.w[1] & MASK_SIGN) == MASK_SIGN
- && (y.w[1] & MASK_INF) == MASK_INF) ? y : x;
- BID_RETURN (res);
- } else if ((y.w[1] & MASK_INF) == MASK_INF) {
- // x is finite, so if y is positive infinity, then x is less, return 0
- // if y is negative infinity, then x is greater, return 1
- res = y;
- BID_RETURN (res);
- }
- // CONVERT X
- sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
- sig_x.w[0] = x.w[0];
- exp_x = (x.w[1] >> 49) & 0x000000000003fffull;
- // CONVERT Y
- exp_y = (y.w[1] >> 49) & 0x0000000000003fffull;
- sig_y.w[1] = y.w[1] & 0x0001ffffffffffffull;
- sig_y.w[0] = y.w[0];
- // ZERO (CASE4)
- // some properties:
- // (+ZERO == -ZERO) => therefore ignore the sign
- // (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
- // therefore ignore the exponent field
- // (Any non-canonical # is considered 0)
- if ((sig_x.w[1] == 0) && (sig_x.w[0] == 0)) {
- res = y;
- BID_RETURN (res);
- }
- if ((sig_y.w[1] == 0) && (sig_y.w[0] == 0)) {
- res = x;
- BID_RETURN (res);
- }
- // REDUNDANT REPRESENTATIONS (CASE6)
- if (exp_y == exp_x && sig_x.w[1] == sig_y.w[1]
- && sig_x.w[0] == sig_y.w[0]) {
- // check if exponents are the same and significands are the same
- if (x.w[1] & 0x8000000000000000ull) { // x is negative
- res = y;
- BID_RETURN (res);
- } else {
- res = x;
- BID_RETURN (res);
- }
- } else if (((sig_x.w[1] > sig_y.w[1] || (sig_x.w[1] == sig_y.w[1]
- && sig_x.w[0] > sig_y.w[0]))
- && exp_x == exp_y)
- || ((sig_x.w[1] > sig_y.w[1]
- || (sig_x.w[1] == sig_y.w[1]
- && sig_x.w[0] >= sig_y.w[0]))
- && exp_x > exp_y)) {
- // if both components are either bigger or smaller, it is clear what
- // needs to be done; also if the magnitudes are equal
- res = x;
- BID_RETURN (res);
- } else if (((sig_y.w[1] > sig_x.w[1] || (sig_y.w[1] == sig_x.w[1]
- && sig_y.w[0] > sig_x.w[0]))
- && exp_y == exp_x)
- || ((sig_y.w[1] > sig_x.w[1]
- || (sig_y.w[1] == sig_x.w[1]
- && sig_y.w[0] >= sig_x.w[0]))
- && exp_y > exp_x)) {
- res = y;
- BID_RETURN (res);
- } else {
- ; // continue
- }
- diff = exp_x - exp_y;
- // if |exp_x - exp_y| < 33, it comes down to the compensated significand
- if (diff > 0) { // to simplify the loop below,
- // if exp_x is 33 greater than exp_y, no need for compensation
- if (diff > 33) {
- res = x; // difference cannot be greater than 10^33
- BID_RETURN (res);
- }
- if (diff > 19) { //128 by 128 bit multiply -> 256 bits
- __mul_128x128_to_256 (sig_n_prime256, sig_x, ten2k128[diff - 20]);
- // if postitive, return whichever significand is larger
- // (converse if negative)
- if (sig_n_prime256.w[3] == 0 && (sig_n_prime256.w[2] == 0)
- && sig_n_prime256.w[1] == sig_y.w[1]
- && (sig_n_prime256.w[0] == sig_y.w[0])) {
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? x : y; // if equal
- BID_RETURN (res);
- }
- res = (((sig_n_prime256.w[3] > 0) || sig_n_prime256.w[2] > 0)
- || (sig_n_prime256.w[1] > sig_y.w[1])
- || (sig_n_prime256.w[1] == sig_y.w[1]
- && sig_n_prime256.w[0] > sig_y.w[0])) ? x : y;
- BID_RETURN (res);
- }
- __mul_64x128_to_192 (sig_n_prime192, ten2k64[diff], sig_x);
- // if postitive, return whichever significand is larger (converse if negative)
- if ((sig_n_prime192.w[2] == 0) && sig_n_prime192.w[1] == sig_y.w[1]
- && (sig_n_prime192.w[0] == sig_y.w[0])) {
- // if equal, return positive magnitude
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? x : y;
- BID_RETURN (res);
- }
- res = ((sig_n_prime192.w[2] > 0)
- || (sig_n_prime192.w[1] > sig_y.w[1])
- || (sig_n_prime192.w[1] == sig_y.w[1]
- && sig_n_prime192.w[0] > sig_y.w[0])) ? x : y;
- BID_RETURN (res);
- }
- diff = exp_y - exp_x;
- // if exp_x is 33 less than exp_y, no need for compensation
- if (diff > 33) {
- res = y;
- BID_RETURN (res);
- }
- if (diff > 19) { //128 by 128 bit multiply -> 256 bits
- // adjust the y significand upwards
- __mul_128x128_to_256 (sig_n_prime256, sig_y, ten2k128[diff - 20]);
- // if postitive, return whichever significand is larger
- // (converse if negative)
- if (sig_n_prime256.w[3] == 0 && (sig_n_prime256.w[2] == 0)
- && sig_n_prime256.w[1] == sig_x.w[1]
- && (sig_n_prime256.w[0] == sig_x.w[0])) {
- // if equal, return positive (if possible)
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? x : y;
- BID_RETURN (res);
- }
- res = (sig_n_prime256.w[3] == 0 && sig_n_prime256.w[2] == 0
- && (sig_n_prime256.w[1] < sig_x.w[1]
- || (sig_n_prime256.w[1] == sig_x.w[1]
- && sig_n_prime256.w[0] < sig_x.w[0]))) ? x : y;
- BID_RETURN (res);
- }
- // adjust the y significand upwards
- __mul_64x128_to_192 (sig_n_prime192, ten2k64[diff], sig_y);
- // if postitive, return whichever significand is larger (converse if negative)
- if ((sig_n_prime192.w[2] == 0) && sig_n_prime192.w[1] == sig_x.w[1]
- && (sig_n_prime192.w[0] == sig_x.w[0])) {
- // if equal, return positive (if possible)
- res = ((y.w[1] & MASK_SIGN) == MASK_SIGN) ? x : y;
- BID_RETURN (res);
- }
- res = (sig_n_prime192.w[2] == 0
- && (sig_n_prime192.w[1] < sig_x.w[1]
- || (sig_n_prime192.w[1] == sig_x.w[1]
- && sig_n_prime192.w[0] < sig_x.w[0]))) ? x : y;
- BID_RETURN (res);
- }
|