[m-dev.] for review: passing boxed floats to pragma C code

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Dec 29 03:13:38 AEDT 1999


On 29-Dec-1999, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> On 28-Dec-1999, Zoltan Somogyi <zs at cs.mu.OZ.AU> wrote:
> > 
> > Add support for passing boxed float arguments to pragma C codes, i.e. as Words.
> > This requires the C code to call word_to_float on inputs and float_to_word
> > on outputs as necessary, but also allows C code to store *already boxed*
> > floats in data structures, and return them to C code without the memory
> > allocation inherent in reboxing.
> ...
> > compiler/prog_data.m:
> > 	Add an extra kind of attribute to pragma_c_code goals: the
> > 	pragma_float_format, with two values: pragma_unboxed_float and
> > 	pragma_boxed_float. With the former, which is the default, float
> > 	arguments are passed to and from pragma C code as type Float, as now.
> > 	With the latter, they are passed in boxed form, as type Word.
> 
> You can already achieve that, by declaring the C code
> to have a polymorphic type.  Instead of
> 
> 	:- func foo(float) = float.
> 	:- pragma c_code(foo(X::in) = (Y::out), pragma_boxed_float, ...).
> 	
> you can write
> 
> 	:- func foo(float) = float.
> 	foo(X) = foo_boxed(X).
> 
> 	:- func foo_boxed(Float1) = Float2.
> 	:- pragma c_code(foo_boxed(X::in) = (Y::out) is det,
> 		% The C code below assumes Float1 = Float2 = float.
> 		% This is OK because foo_boxed/1 is called only from foo/1.
> 		...
> 	).

On second thoughts, a better way to achieve this is to use a (polymorphic)
no_tag type:

	:- type boxed(T) ---> box(T).

	:- func foo(float) = float.
	foo(X) = Y :- foo_boxed(box(X)) = box(Y).

	:- func foo_boxed(boxed(float)) = boxed(float).
	:- pragma c_code(foo_boxed(X::in) = (Y::out) is det,
		...
	).

This method is clearer and preserves type safety.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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