From 58dbd2ed918bb9e6f93aed076f8f3de92d41d2f9 Mon Sep 17 00:00:00 2001 From: Pixel <> Date: Tue, 17 Apr 2001 02:20:40 +0000 Subject: Grou --- lib/alu.c | 357 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 278 insertions(+), 79 deletions(-) (limited to 'lib/alu.c') diff --git a/lib/alu.c b/lib/alu.c index 4047b72..4ff01f3 100644 --- a/lib/alu.c +++ b/lib/alu.c @@ -1,8 +1,7 @@ #include "alu.h" #include "config.h" #include "exceptions.h" - -// rajouter les overflow... +#include "registre.h" /*****************************************/ /** **/ @@ -16,78 +15,262 @@ Uint32 SecondResult = 0; /* ALU rapide */ +Uint32 RNot(Uint32 a) { + return (~a) + 1; +} + Uint32 RAdditionNonSigne(Uint32 a, Uint32 b) { - return (a + b); + unsigned long long tr = a, masq = 1; + tr += b; + + masq <<= 32; + if (masq & tr) { + SetOverflow(); + } else { + ResetOverflow(); + } + + if (tr) { + SetZero(); + } else { + ResetZero(); + } + + if (tr & 1) { + SetParity(); + } else { + ResetParity(); + } + + ResetSign(); + return tr; } -Uint32 RAdditionSigne(Uint32 a, Uint32 b) +Uint32 RAdditionSigne(long int a, long int b) { - return (a + b); + long int tr = a; + tr += b; + + if (((a & 0x80000000) && (b & 0x80000000) && !(tr & 0x80000000)) || + (!(a & 0x80000000) && !(b & 0x80000000) && (tr & 0x80000000))) { + SetOverflow(); + } else { + ResetOverflow(); + } + + if (tr) { + SetZero(); + } else { + ResetZero(); + } + + if (tr & 1) { + SetParity(); + } else { + ResetParity(); + } + + if (tr & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + return tr; } Uint32 RSoustractionNonSigne(Uint32 a, Uint32 b) { - return (a - b); + return RAdditionNonSigne(a, RNot(b)); } Uint32 RSoustractionSigne(Uint32 a, Uint32 b) { - return (a - b); + return RAdditionSigne(a, RNot(b)); } Uint32 RMultiplicationNonSigne(Uint32 a, Uint32 b) { - unsigned long long temp; + unsigned long long temp = a; - temp = a * b; + temp *= b; SecondResult = temp >> 32; + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + ResetOverflow(); + ResetSign(); return (temp); } -Uint32 RMultiplicationSigne(Uint32 a, Uint32 b) +Uint32 RMultiplicationSigne(long int a, long int b) { - unsigned long long temp; + long long temp = a; - temp = a * b; + temp *= b; SecondResult = temp >> 32; + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + ResetOverflow(); return (temp); } Uint32 RDivisionNonSigne(Uint32 a, Uint32 b) { + unsigned long long temp = a; + + temp /= b; + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + ResetOverflow(); + ResetSign(); SecondResult = a % b; - return (a / b); + return temp; } -Uint32 RDivisionSigne(Uint32 a, Uint32 b) +Uint32 RDivisionSigne(long int a, long int b) { + long long temp = a; + + temp /= b; + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + ResetOverflow(); SecondResult = a % b; - return (a / b); + return temp; } Uint32 RAND(Uint32 a, Uint32 b) { - return (a & b); + Uint32 temp = a & b; + + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + return temp; } Uint32 ROR(Uint32 a, Uint32 b) { - return (a | b); + Uint32 temp = a | b; + + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + return temp; } Uint32 RSHL(Uint32 a) { - return (a >> 1); + Uint32 temp = a << 1; + + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + return temp; } Uint32 RSHR(Uint32 a) { - return (a << 1); + Uint32 temp = a >> 1; + + if (temp & 1) { + SetParity(); + } else { + ResetParity(); + } + if (temp) { + ResetZero(); + } else { + SetZero(); + } + if (temp & 0x80000000) { + SetSign(); + } else { + ResetSign(); + } + return temp; } -#if 0 +/* ALU normale (binaire) */ /*On compte de 0 à 31 */ @@ -97,7 +280,7 @@ Uint32 ValeurIbitsAuDeb(Uint32 nb, int i) Uint32 val, un = 1; if ((i > 31) || (i < 0)) { - exception(_("ValeurIbitsAuDeb: position not in interval")); + exception(1, _("ValeurIbitsAuDeb: position not in interval")); } val = nb >> i; val = val & un; @@ -125,7 +308,7 @@ Uint32 MettreAVIbit(Uint32 nb, int i) { Uint32 val = 1; - return (OrBit(nb, (val << i))); + return (nb | (val << i)); } @@ -157,7 +340,7 @@ Uint32 InverseIbit(Uint32 nb, int i) Uint32 un = 1; if ((i > 31) || (i < 0)) { - exception(_("InverseIbit: position not in interval")); + exception(1, _("InverseIbit: position not in interval")); } un = (un << i); return (nb ^ un); @@ -265,7 +448,6 @@ Uint32 NAdditionNonSigne(Uint32 x, Uint32 y) } if (i == 31) { if (ret == 1) { - fprintf(stderr, "Erreur AddNonSig: erreur de depassement.\n"); errRet = 1; } } @@ -317,7 +499,6 @@ Uint32 NAdditionSigne(Uint32 x, Uint32 y) } if (i == 30) { if (ret == 1) { - fprintf(stderr, "Erreur AddNonSig: erreur de depassement.\n"); errRet = 1; } } @@ -369,7 +550,6 @@ Uint32 NSoustractionNonSigne(Uint32 x, Uint32 y) } if (i == 31) { if (ret == 1) { - fprintf(stdout, "Erreur SousNonSig: erreur de depassement.\n"); errRet = 1; } } @@ -377,7 +557,7 @@ Uint32 NSoustractionNonSigne(Uint32 x, Uint32 y) return (sou); } -Uint32 NSoustractionSignee(Uint32 x, Uint32 y) +Uint32 NSoustractionSigne(Uint32 x, Uint32 y) { /* x - y */ int i; @@ -421,7 +601,6 @@ Uint32 NSoustractionSignee(Uint32 x, Uint32 y) } if (i == 30) { if (ret == 1) { - fprintf(stdout, "Erreur SousNonSig: erreur de depassement.\n"); errRet = 1; } } @@ -565,11 +744,8 @@ couple MultipliNonSig(Uint32 x, Uint32 y) for (i = 0; i < 32; i++) { a = ValeurIbitsAuDeb(y, i); tp = MultChifNomb(x, a); - fprintf(stdout, "tp = %Lu\n", tp); dec = (tp >> (32 - i)); - fprintf(stdout, "dec = %Lu\n", dec); - pa2 = AddNonSigUint32(pa2, dec); - fprintf(stdout, "pa2 = %Lu\n", pa2); + pa2 = NAdditionNonSigne(pa2, dec); add = tp << i; z = Addition(pa1, add); pa1 = z.deb; @@ -625,12 +801,12 @@ couple MultipliSig(Uint32 x, Uint32 y) a = ValeurIbitsAuDeb(y1, i); tp = MultChifNomb(x1, a); dec = tp >> (31 - i); - pa2 = AddNonSigUint32(pa2, dec); + pa2 = NAdditionNonSigne(pa2, dec); add = tp << i; z = Add30Mul(pa1, add); pa1 = z.deb; if (z.fin == 1) { - pa2 = AddNonSigUint32(pa2, 1); + pa2 = NAdditionNonSigne(pa2, 1); } } pa1 = MettreAVIbit(pa1, 31); @@ -640,65 +816,87 @@ couple MultipliSig(Uint32 x, Uint32 y) return (z); } -Uint32 NMultiplactionNonSigne(Uint32 a, Uint32 b) +Uint32 NDivisionNonSigne(Uint32 a, Uint32 b) +{ + Uint32 quot, rest; + if (b>a) + { + quot=0; + rest=a; + } + else + { + if (b==a) + { + quot=1; + rest=0; + } + else + { + quot=0; + rest=a; + while (rest>=b) + { + rest=NSoustractionNonSigne(rest,b); + quot++; + } + } + } + SecondResult = rest; + return quot; +} + +Uint32 NDivisionSigne(long int a, long int b) +{ + long int quot, rest; + + if (b>a) + { + quot=0; + rest=a; + } + else + { + if (b==a) + { + quot=1; + rest=0; + } + else + { + quot=0; + rest=a; + while (rest>=b) + { + rest=NSoustractionNonSigne(rest,b); + quot++; + } + } + } + SecondResult = rest; + return quot; +} + +Uint32 NMultiplicationNonSigne(Uint32 a, Uint32 b) { couple z; z = MultipliNonSig(a, b); - SecondResult = z.deb; - return z.fin; + SecondResult = z.fin; + return z.deb; } -Uint32 NMultiplactionSigne(Uint32 a, Uint32 b) +Uint32 NMultiplicationSigne(Uint32 a, Uint32 b) { couple z; z = MultipliNonSig(a, b); - SecondResult = z.deb; - return z.fin; + SecondResult = z.fin; + return z.deb; } -#else -Uint32 NAdditionNonSigne(Uint32 a, Uint32 b) -{ -} -Uint32 NAdditionSigne(Uint32 a, Uint32 b) -{ -} -Uint32 NSoustractionNonSigne(Uint32 a, Uint32 b) -{ -} -Uint32 NSoustractionSigne(Uint32 a, Uint32 b) -{ -} -Uint32 NMultiplicationNonSigne(Uint32 a, Uint32 b) -{ -} -Uint32 NMultiplicationSigne(Uint32 a, Uint32 b) -{ -} -Uint32 NDivisionNonSigne(Uint32 a, Uint32 b) -{ -} -Uint32 NDivisionSigne(Uint32 a, Uint32 b) -{ -} -Uint32 NAND(Uint32 a, Uint32 b) -{ -} -Uint32 NOR(Uint32 a, Uint32 b) -{ -} -Uint32 NSHR(Uint32 a) -{ -} -Uint32 NSHL(Uint32 a) -{ -} -#endif - Uint32 AdditionNonSigne(Uint32 a, Uint32 b) { if (Rapide) { @@ -806,3 +1004,4 @@ Uint32 SHR(Uint32 a) return NSHR(a); } } + -- cgit v1.2.3