Commit 4c5bba5a authored by Cerlane Leong's avatar Cerlane Leong 💬

Fix bug when subtracting positive product from negative quire

parent 9223c7e3
......@@ -41,11 +41,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
quire32_t q32_fdp_sub( quire32_t q, posit32_t pA, posit32_t pB ){
union ui32_p32 uA, uB;
union ui32_p32 uA, uB;
union ui512_q32 uZ, uZ1, uZ2;
uint_fast32_t uiA, uiB;
uint_fast32_t regA, fracA, regime, tmp;
bool signA, signB, signZ2, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
bool signA, signB, signZ1, signZ2, signZ=0, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
int_fast32_t expA, expB;
int_fast16_t kA=0, shiftRight=0;
uint_fast64_t frac64Z;
......@@ -73,6 +73,7 @@ quire32_t q32_fdp_sub( quire32_t q, posit32_t pA, posit32_t pB ){
signA = signP32UI( uiA );
signB = signP32UI( uiB );
signZ2 = signA ^ signB;
signZ1 = uZ1.ui[0]>>63;
if(signA) uiA = (-uiA & 0xFFFFFFFF);
if(signB) uiB = (-uiB & 0xFFFFFFFF);
......@@ -152,7 +153,21 @@ quire32_t q32_fdp_sub( quire32_t q, posit32_t pA, posit32_t pB ){
//This is the only difference from ADD (signZ2) and (!signZ2)
if (!signZ2){
if (signZ1 && !signZ2){//sum of negative numbers
signZ=1;
for (i=7; i>=0; i--){
if (uZ1.ui[i]>0){
uZ1.ui[i] = - uZ1.ui[i];
i--;
while(i>=0){
uZ1.ui[i] = ~uZ1.ui[i];
i--;
}
break;
}
}
}
else if (!signZ2){
for (i=7; i>=0; i--){
if (uZ2.ui[i]>0){
uZ2.ui[i] = - uZ2.ui[i];
......@@ -187,10 +202,21 @@ quire32_t q32_fdp_sub( quire32_t q, posit32_t pA, posit32_t pB ){
}
}
if(signZ){
for (i=7; i>=0; i--){
if (uZ.ui[i]>0){
uZ.ui[i] = - uZ.ui[i];
i--;
while(i>=0){
uZ.ui[i] = ~uZ.ui[i];
i--;
}
break;
}
}
}
//Exception handling
if (isNaRQ32(uZ.q) ) uZ.q = q32_clr(uZ.q);
return uZ.q;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment