[m-dev.] for review: Add new library predicate to reversibly encode two ints in a single int.

Peter Schachte schachte at cs.mu.OZ.AU
Tue Jan 18 16:22:04 AEDT 2000


Sorry about that last message.  This is the real diff.

Add new library predicate to reversibly encode two ints in a single int.

Estimated hours taken: 0.5


int.m:
	Add new code/4 predicate.

Index: int.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/int.m,v
retrieving revision 1.65
diff -u -r1.65 int.m
--- int.m	2000/01/16 04:34:28	1.65
+++ int.m	2000/01/18 04:47:26
@@ -170,6 +170,16 @@
 :- mode is(uo, di) is det.
 :- mode is(out, in) is det.
 
+	% reversibly encode two ints in a single int.
+	% code(Factor, Value, Residue, Coding)
+	% holds iff Factor*Value+Residue = Coding, and 0 <= Value < Factor.
+	% This is useful for things like converting between time as 
+	% hours, minutes, and seconds, and raw seconds.
+
+:- pred code(int, int, int, int).
+:- mode code(in, in, in, out) is det.
+:- mode code(in, out, out, in) is det.
+
 	% int__max_int(Max) binds Max to the maximum value of an int
 	% on this machine.
 :- pred int__max_int(int::out) is det.
@@ -400,6 +410,21 @@
 		will_not_call_mercury,
 "
 	FloatVal = IntVal;
+").
+
+%-----------------------------------------------------------------------------%
+
+% XXX we really should check that 0 <= Value < Factor and raise an exception
+% if not.  But for reasons of efficiency, we don't.
+:- pragma c_code(code(Factor::in, Value::in, Residue::in, Coding::out),
+		 will_not_call_mercury, "
+	Coding = Factor * Residue + Value;
+").
+
+:- pragma c_code(code(Factor::in, Value::out, Residue::out, Coding::in),
+		 will_not_call_mercury, "
+	Value = Coding % Factor;
+	Residue = Coding / Factor;
 ").
 
 %-----------------------------------------------------------------------------%

--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list