summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/alu-ralph.c578
1 files changed, 578 insertions, 0 deletions
diff --git a/lib/alu-ralph.c b/lib/alu-ralph.c
new file mode 100644
index 0000000..a896c3c
--- /dev/null
+++ b/lib/alu-ralph.c
@@ -0,0 +1,578 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "alu.h"
+int errRet = 0;
+
+
+ /*On compte de 0 à 31 */
+
+ /*Return la valeur du ieme bite en position 0 */
+Uint32 ValeurIBitesAuDeb(Uint32 nb, int i)
+{
+ Uint32 val, un = 1;
+
+ if ((i > 31) || (i < 0)) {
+ fprintf(stderr, "erreur, ValeurIBitesAuDeb: la position demande n est pas dans l'intervalle");
+ return (0);
+ }
+ val = nb >> i;
+ val = val & un;
+ return (val);
+}
+
+
+ /*Return la valeur du ieme bite en sa position d origine */
+/*
+Uint32 ValeurIBite(Uint32 nb, int i) {
+ Uint32 val = 0, un = 1;
+
+ if ((i > 31) || (i < 0)) {
+ fprintf(stderr, "erreur, ValeurIBite: la position demande n est pas dans l'intervalle");
+ return (0);
+ }
+ val = un << i;
+ val = AndBit(un, nb);
+ return (val);
+}
+*/
+
+ /*Met a vrai le ieme bite */
+Uint32 MettreAVIBite(Uint32 nb, int i)
+{
+ Uint32 val = 1;
+
+ return (OrBit(nb, (val << i)));
+}
+
+
+ /* Met a Vrai le Uint32 */
+Uint32 MetAVUint32(void)
+{
+ int i;
+
+ Uint32 val = 0, un = 1;
+
+ for (i = 0; i <= 31; i++) {
+ val = (val | un);
+ un <<= 1;
+ }
+ return (val);
+}
+
+
+ /* Met a Faux le Uint32 */
+Uint32 MetAFUint32(void)
+{
+ return (0);
+}
+
+
+ /* Inverse la valeur du i eme bite */
+Uint32 InverseIBite(Uint32 nb, int i)
+{
+ Uint32 un = 1;
+
+ if ((i > 31) || (i < 0)) {
+ fprintf(stderr, "erreur, InverseIBite: la position demande n est pas dans l'intervalle");
+ return (0);
+ }
+ un = (un << i);
+ return (nb ^ un);
+}
+
+
+ /* Inverse le Uint32 */
+Uint32 InverseUint32(Uint32 x)
+{
+ int i;
+
+ Uint32 val = x;
+
+ for (i = 0; i <= 31; i++) {
+ val = InverseIBite(val, i);
+ }
+ return (val);
+}
+
+Uint32 AndBit(Uint32 x, Uint32 y)
+{
+ Uint32 m = 1, z = 0;
+ int i;
+
+ for (i = 0; i < 32; i++) {
+ z |= (x & m) & (y & m);
+ m <<= 1;
+ }
+ return (z);
+}
+
+Uint32 OrBit(Uint32 x, Uint32 y)
+{
+ Uint32 m = 1, z = 0;
+ int i;
+
+ for (i = 0; i < 32; i++) {
+ z |= (x & m) | (y & m);
+ m <<= 1;
+ }
+ return (z);
+}
+
+Uint32 ShlUint32(Uint32 x, int i)
+{
+ return (x << i);
+}
+
+Uint32 ShrUint32(Uint32 x, int i)
+{
+ return (x >> i);
+}
+
+Uint32 AddNonSigUint32(Uint32 x, Uint32 y)
+{
+ int i;
+
+ Uint32 a, b, tp, add = 0, ret = 0;
+
+ if (x == 0)
+ return (y);
+ if (y == 0)
+ return (x);
+ for (i = 0; i < 32; i++) {
+ a = ValeurIBitesAuDeb(x, i);
+ b = ValeurIBitesAuDeb(y, i);
+ tp = a + b + ret;
+ if (tp == 3) {
+ ret = 1;
+ tp = 1;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ if (tp == 2) {
+ ret = 1;
+ tp = 0;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ if (tp == 1) {
+ ret = 0;
+ tp = 1;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ ret = 0;
+ }
+ }
+ }
+ if (i == 31) {
+ if (ret == 1) {
+ fprintf(stderr, "Erreur AddNonSig: erreur de depassement.\n");
+ errRet = 1;
+ }
+ }
+ }
+ return (add);
+}
+
+Uint32 AddSigUint32(Uint32 x, Uint32 y)
+{
+ int i;
+
+ Uint32 a, b, tp, add = 0, ret = 0;
+
+ if (x == 0)
+ return (y);
+ if (y == 0)
+ return (x);
+ for (i = 0; i < 32; i++) {
+ a = ValeurIBitesAuDeb(x, i);
+ b = ValeurIBitesAuDeb(y, i);
+ tp = a + b + ret;
+ if (tp == 3) {
+ ret = 1;
+ tp = 1;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ if (tp == 2) {
+ ret = 1;
+ tp = 0;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ if (tp == 1) {
+ ret = 0;
+ tp = 1;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ ret = 0;
+ }
+ }
+ }
+ if (i == 30) {
+ if (ret == 1) {
+ fprintf(stderr, "Erreur AddNonSig: erreur de depassement.\n");
+ errRet = 1;
+ }
+ }
+ }
+ return (add);
+}
+
+Uint32 SousNonSigUint32(Uint32 x, Uint32 y)
+{ /* x - y */
+ int i;
+
+ Uint32 a, b, tp, sou = 0, ret = 0;
+
+ for (i = 0; i < 32; i++) {
+ a = ValeurIBitesAuDeb(x, i);
+ b = ValeurIBitesAuDeb(y, i);
+ if (((a == 0) && (b == 0) && (ret == 0)) ||
+ ((a == 1) && (b == 1) && (ret == 0)) || ((a == 1) && (b == 0) && (ret == 1))) {
+ ret = 0;
+ tp = 0;
+ tp <<= i;
+ sou |= tp;
+ }
+
+ else {
+ if (((a == 0) && (b == 0) && (ret == 1)) ||
+ ((a == 0) && (b == 1) && (ret == 0)) || ((a == 1) && (b == 1) && (ret == 1))) {
+ ret = 1;
+ tp = 1;
+ tp <<= i;
+ sou |= tp;
+ }
+
+ else {
+ if ((a == 1) && (b == 0) && (ret == 0) || 0) {
+ ret = 0;
+ tp = 1;
+ tp <<= i;
+ sou |= tp;
+ }
+
+ else {
+ ret = 1;
+ tp = 0;
+ tp <<= i;
+ sou |= tp;
+ }
+ }
+ }
+ if (i == 31) {
+ if (ret == 1) {
+ fprintf(stdout, "Erreur SousNonSig: erreur de depassement.\n");
+ errRet = 1;
+ }
+ }
+ }
+ return (sou);
+}
+
+Uint32 SousSigUint32(Uint32 x, Uint32 y)
+{ /* x - y */
+ int i;
+
+ Uint32 a, b, tp, sou = 0, ret = 0;
+
+ for (i = 0; i < 32; i++) {
+ a = ValeurIBitesAuDeb(x, i);
+ b = ValeurIBitesAuDeb(y, i);
+ if (((a == 0) && (b == 0) && (ret == 0)) ||
+ ((a == 1) && (b == 1) && (ret == 0)) || ((a == 1) && (b == 0) && (ret == 1))) {
+ ret = 0;
+ tp = 0;
+ tp <<= i;
+ sou |= tp;
+ }
+
+ else {
+ if (((a == 0) && (b == 0) && (ret == 1)) ||
+ ((a == 0) && (b == 1) && (ret == 0)) || ((a == 1) && (b == 1) && (ret == 1))) {
+ ret = 1;
+ tp = 1;
+ tp <<= i;
+ sou |= tp;
+ }
+
+ else {
+ if ((a == 1) && (b == 0) && (ret == 0) || 0) {
+ ret = 0;
+ tp = 1;
+ tp <<= i;
+ sou |= tp;
+ }
+
+ else {
+ ret = 1;
+ tp = 0;
+ tp <<= i;
+ sou |= tp;
+ }
+ }
+ }
+ if (i == 30) {
+ if (ret == 1) {
+ fprintf(stdout, "Erreur SousNonSig: erreur de depassement.\n");
+ errRet = 1;
+ }
+ }
+ }
+ return (sou);
+}
+
+couple Addition(Uint32 x, Uint32 y)
+{ /* Renvoye le resultat + eventuellement une retenue */
+ int i;
+
+ couple z;
+ Uint32 a, b, tp, add = 0, ret = 0;
+
+ for (i = 0; i < 32; i++) {
+ a = ValeurIBitesAuDeb(x, i);
+ b = ValeurIBitesAuDeb(y, i);
+ tp = a + b + ret;
+ if (tp == 3) {
+ ret = 1;
+ tp = 1;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ if (tp == 2) {
+ ret = 1;
+ tp = 0;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ if (tp == 1) {
+ ret = 0;
+ tp = 1;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ ret = 0;
+ }
+ }
+ }
+ if (i == 31) {
+ if (ret == 1) {
+ z.deb = add;
+ z.fin = 1;
+ return (z);
+ }
+ }
+ }
+ z.deb = add;
+ z.fin = 0;
+ return (z);
+}
+
+couple Add30Mul(Uint32 x, Uint32 y)
+{
+ int i;
+
+ couple z;
+ Uint32 a, b, tp, add = 0, ret = 0;
+
+ for (i = 0; i < 31; i++) {
+ a = ValeurIBitesAuDeb(x, i);
+ b = ValeurIBitesAuDeb(y, i);
+ tp = a + b + ret;
+ if (tp == 3) {
+ ret = 1;
+ tp = 1;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ if (tp == 2) {
+ ret = 1;
+ tp = 0;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ if (tp == 1) {
+ ret = 0;
+ tp = 1;
+ tp <<= i;
+ add |= tp;
+ }
+
+ else {
+ ret = 0;
+ }
+ }
+ }
+ if (i == 30) {
+ if (ret == 1) {
+ z.deb = add;
+ z.fin = 1;
+ return (z);
+ }
+ }
+ }
+ z.deb = add;
+ z.fin = 0;
+ return (z);
+}
+
+Uint32 MultChifNomb(Uint32 x, Uint32 y)
+{
+ int i;
+
+ Uint32 mul = 0, a, un = 1;
+
+ if (y == 0)
+ return (0);
+ if (x == 0)
+ return (0);
+ if (y == 1)
+ return (x);
+ for (i = 0; i < 32; i++) {
+ a = ValeurIBitesAuDeb(x, i);
+ if (a == 1) {
+ un <<= i;
+ mul |= un;
+ }
+ }
+ return (mul);
+}
+
+couple MultipliNonSig(Uint32 x, Uint32 y)
+{
+ couple z, w;
+ int i;
+
+ Uint32 tp, dec, add, a, pa1 = 0, pa2 = 0;
+
+ for (i = 0; i < 32; i++) {
+ a = ValeurIBitesAuDeb(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);
+ add = tp << i;
+ z = Addition(pa1, add);
+ pa1 = z.deb;
+ if (z.fin == 1) {
+ w = Addition(pa2, 1);
+ pa2 = w.deb;
+ }
+ }
+ z.deb = pa1;
+ z.fin = pa2;
+
+ printf("\n");
+ return (z);
+}
+
+couple MultipliSig(Uint32 x, Uint32 y)
+{
+ couple z, w;
+ int i;
+
+ Uint32 tp, dec, add, a, b, pa1 = 0, pa2 = 0, x1 = x, y1 = y;
+
+ a = ValeurIBitesAuDeb(x, 31);
+ b = ValeurIBitesAuDeb(y, 31);
+ if (((a == 0) && (b == 0)) || ((a == 1) && (b == 1))) { /* Le resultat sera pos */
+ if ((a == 1) && (b == 1)) {
+ x1 = InverseIBite(x, 31);
+ y1 = InverseIBite(y, 31);
+ }
+ for (i = 0; i < 31; i++) {
+ a = ValeurIBitesAuDeb(y1, i);
+ tp = MultChifNomb(x1, a);
+ dec = tp >> (31 - i);
+ w = Addition(pa2, dec);
+ pa2 = w.deb;
+ add = tp << i;
+ z = Add30Mul(pa1, add);
+ pa1 = z.deb;
+ if (z.fin == 1) {
+ w = Addition(pa2, 1);
+ pa2 = w.deb;
+ }
+ }
+ z.deb = pa1;
+ z.fin = pa2;
+ return (z);
+ }
+ if (a == 1)
+ x1 = InverseIBite(x, 31);
+ if (b == 1)
+ y1 = InverseIBite(y, 31);
+ for (i = 0; i < 31; i++) {
+ a = ValeurIBitesAuDeb(y1, i);
+ tp = MultChifNomb(x1, a);
+ dec = tp >> (31 - i);
+ pa2 = AddNonSigUint32(pa2, dec);
+ add = tp << i;
+ z = Add30Mul(pa1, add);
+ pa1 = z.deb;
+ if (z.fin == 1) {
+ pa2 = AddNonSigUint32(pa2, 1);
+ }
+ }
+ pa1 = MettreAVIBite(pa1, 31);
+ pa2 = MettreAVIBite(pa2, 31);
+ z.deb = pa1;
+ z.fin = pa2;
+ return (z);
+}
+
+Uint32 AndEnt(Uint32 x, Uint32 y)
+{
+ return (x & y);
+}
+
+Uint32 OrEnt(Uint32 x, Uint32 y)
+{
+ return (x | y);
+}
+
+Uint32 AddEnt(Uint32 x, Uint32 y)
+{
+ return (x + y);
+}
+
+Uint32 SousEnt(Uint32 x, Uint32 y)
+{
+ return (x - y);
+}
+
+Uint32 DivEnt(Uint32 x, Uint32 y)
+{
+ return (x / y);
+}
+
+Uint32 RestDivEnt(Uint32 x, Uint32 y)
+{
+ return (x % y);
+}