From: Jonathan BAILLEUL
Subject: Consing problem with TRUNCATE
Date: 
Message-ID: <38FDAA0C.AAD7E815@emi.u-bordeaux.fr>
Hello.

We noticed that the 'truncate' function performed some expensive consing
when applied to a float number.
We are really worried since we use it extensively and then we are
disturbed by garbage-collecting.

Do you have any idea to suppress that consing? 
Looks hard, as far as this consing is probably done for 'truncate'
intern needs.
A relevant solution would be to find or devise an alternate way to
recuperate the 'integer part' of a float number.

Thank you very much for your help.

-- 
----------------------------------------------
Jonathan BAILLEUL (········@emi.u-bordeaux.fr)
Maitrise Informatique, Universite Bordeaux I

From: Tim Bradshaw
Subject: Re: Consing problem with TRUNCATE
Date: 
Message-ID: <ey3r9c2b4vd.fsf@cley.com>
* Jonathan BAILLEUL wrote:

> Do you have any idea to suppress that consing? 
> Looks hard, as far as this consing is probably done for 'truncate'
> intern needs.
> A relevant solution would be to find or devise an alternate way to
> recuperate the 'integer part' of a float number.

This is obviously implementation-dependent. Which implementation are
you using?  Both Allegro and CMUCL can compile cons-free (inline) versions of
TRUNCATE with suitably-good declarations.  I suspect they both need to
know the type of the float it's been given, and probably (certainly
for CMUCL) its range so they can be sure that the result coming back
is a fixnum.  CLISP seems to be cons-free too.

--tim
From: Raymond Toy
Subject: Re: Consing problem with TRUNCATE
Date: 
Message-ID: <4ng0sixl9d.fsf@rtp.ericsson.se>
>>>>> "Jonathan" == Jonathan BAILLEUL <········@emi.u-bordeaux.fr> writes:

    Jonathan> Do you have any idea to suppress that consing? Looks
    Jonathan> hard, as far as this consing is probably done for
    Jonathan> 'truncate' intern needs.  A relevant solution would be
    Jonathan> to find or devise an alternate way to recuperate the
    Jonathan> 'integer part' of a float number.

If your compiler can prove (or you can tell the compiler) that your
floating point number is "small" enough, no consing is needed.  For
example, with CMUCL, you could say something like

	(defun tst (x)
	  (declare (type (single-float #.(float most-negative-fixnum)
				       #.(float most-positive-fixnum))
			 x))
	  (truncate x))

and no consing would occur because CMUCL would know that truncate
would return a fixnum.

If you can't prove this, then truncate will have to cons since the
result may not fit in an fixnum.

Ray
From: Jonathan BAILLEUL
Subject: Re: Consing problem with TRUNCATE
Date: 
Message-ID: <38FED17D.512A6E13@emi.u-bordeaux.fr>
Raymond Toy wrote:
> 
> >>>>> "Jonathan" == Jonathan BAILLEUL <········@emi.u-bordeaux.fr> writes:
> 
>     Jonathan> Do you have any idea to suppress that consing? Looks
>     Jonathan> hard, as far as this consing is probably done for
>     Jonathan> 'truncate' intern needs.  A relevant solution would be
>     Jonathan> to find or devise an alternate way to recuperate the
>     Jonathan> 'integer part' of a float number.
> 
> If your compiler can prove (or you can tell the compiler) that your
> floating point number is "small" enough, no consing is needed.  For
> example, with CMUCL, you could say something like
> 
>         (defun tst (x)
>           (declare (type (single-float #.(float most-negative-fixnum)
>                                        #.(float most-positive-fixnum))
>                          x))
>           (truncate x))
> 
> and no consing would occur because CMUCL would know that truncate
> would return a fixnum.
> 
> If you can't prove this, then truncate will have to cons since the
> result may not fit in an fixnum.
> 
> Ray

Thank you.
This works fine on our compiler (CmuCL/Python).

Now, it looks like we have other consing problems with functions like
'expt' and 'sqrt', but we will wait for further testing before
requesting help again.

Best Regards.

-- 
----------------------------------------------
Jonathan BAILLEUL (········@emi.u-bordeaux.fr)
Maitrise Informatique, Universite Bordeaux I
From: Raymond Toy
Subject: Re: Consing problem with TRUNCATE
Date: 
Message-ID: <4nog74x62q.fsf@rtp.ericsson.se>
>>>>> "Jonathan" == Jonathan BAILLEUL <········@emi.u-bordeaux.fr> writes:

    Jonathan> Thank you.
    Jonathan> This works fine on our compiler (CmuCL/Python).

But are the declarations right?  If not, the compiler may generate
code that WON'T work if the declarations are false.
 
    Jonathan> Now, it looks like we have other consing problems with
    Jonathan> functions like 'expt' and 'sqrt', but we will wait for
    Jonathan> further testing before requesting help again.

Without knowing more, I think you'll have to do the same things for
expt and sqrt.  For sqrt, all you really need to say is something like

(declare (single-float 0f0))

For expt, you need to be more careful, but basically, (expt x y) will
not cons if the compiler can prove that (expt x y) is a single-float
so if y is an integer, x can be any single-float.  If y is a
single-float, then x must be a positive single-float.  

Ray