<> <> <> <<>> DIRECTORY Basics, TamarinDivide; TamarinDivideImpl: CEDAR PROGRAM IMPORTS Basics EXPORTS TamarinDivide = BEGIN priorityOfByte: ARRAY [0..256) OF INT = [-1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; <<>> Priority: PUBLIC PROC [inNum: INT] RETURNS [priority: NAT _ 0, isZero: BOOL] ~ { bytePriority: INT; i: NAT _ 3; zeroByte: BOOL; bytePriority _ priorityOfByte[Basics.DoubleShiftRight[[li[inNum]], 8*i].li]; zeroByte _ bytePriority = -1; THROUGH [0..2] WHILE zeroByte DO i _ i -1; bytePriority _ priorityOfByte[Basics.DoubleShiftRight[[li[inNum]], 8*i].li]; zeroByte _ bytePriority = -1; ENDLOOP; IF (bytePriority # -1) THEN RETURN [bytePriority + 8*i, FALSE] ELSE RETURN [0, TRUE]; }; OldPriority: PUBLIC PROC [inNum: INT] RETURNS [priority: INT _ 0] ~ { IF (inNum < 0) THEN inNum _ - inNum; WHILE (inNum > 0) DO IF inNum > 256 THEN BEGIN priority _ priority + 8; inNum _ Basics.DoubleShiftRight[[li[inNum]], 8].li; END ELSE BEGIN IF inNum > 16 THEN BEGIN priority _ priority + 4; inNum _ Basics.DoubleShiftRight[[li[inNum]], 4].li; END ELSE BEGIN priority _ priority + 1; inNum _ Basics.DoubleShiftRight[[li[inNum]], 1].li; END; END; ENDLOOP; RETURN[priority]; }; Divide: PUBLIC PROC [inD1, inD2: INT] RETURNS [quotient, remainder: INT, overFlow: BOOL _ FALSE] ~ { <> nextExponent: NAT; priorDivisor: NAT; divisorNeg: BOOL _ inD1 < 0; divisorPos: BOOL _ inD1 > 0; isZero: BOOL; [priorDivisor, isZero] _ Priority[inD1]; IF isZero THEN RETURN[0, 0, TRUE]; quotient _ 0; remainder _ inD2; [nextExponent, isZero] _ Priority[remainder]; nextExponent _ nextExponent - priorDivisor; WHILE (nextExponent > 0) DO IF (remainder < 0) # divisorNeg THEN BEGIN quotient _ quotient - Basics.DoubleShiftLeft[[li[1]], nextExponent].li; remainder _ remainder + Basics.DoubleShiftLeft[[li[inD1]], nextExponent].li; END ELSE BEGIN quotient _ quotient + Basics.DoubleShiftLeft[[li[1]], nextExponent].li; remainder _ remainder - Basics.DoubleShiftLeft[[li[inD1]], nextExponent].li; END; [nextExponent, isZero] _ Priority[remainder]; nextExponent _ nextExponent - priorDivisor; ENDLOOP; IF (nextExponent = 0) THEN BEGIN IF (remainder < 0) # divisorNeg THEN BEGIN quotient _ quotient - 1; remainder _ remainder + inD1; END ELSE BEGIN quotient _ quotient + 1; remainder _ remainder - inD1; END; END; <> IF (remainder < 0) # (inD2 < 0) THEN BEGIN IF ((remainder < 0) AND divisorPos) OR ((remainder > 0) AND divisorNeg) THEN BEGIN quotient _ quotient - 1; remainder _ remainder + inD1; END ELSE BEGIN quotient _ quotient + 1; remainder _ inD1 - remainder; END; END; RETURN[quotient, remainder, FALSE]; }; END.