caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Int32 vs float unboxing
@ 2011-08-22  7:19 Dmitry Bely
  2011-08-22  7:58 ` Alain Frisch
  2011-08-24  8:10 ` [Caml-list] " Dmitry Bely
  0 siblings, 2 replies; 4+ messages in thread
From: Dmitry Bely @ 2011-08-22  7:19 UTC (permalink / raw)
  To: Caml List

In the code below "s" reference is unboxed in sum_float loop, but not
in sum_in32. Why?

let sum_int32 v =
  let s = ref 0l in
  for i=0 to (Array.length v)-1 do
    s := Int32.add !s (Array.unsafe_get v i)
  done;
  Int32.add !s Int32.zero

let sum_float v =
  let s = ref 0. in
  for i=0 to (Array.length v)-1 do
    s := !s +. (Array.unsafe_get v i)
  done;
  !s +. 0.

- Dmitry Bely

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Int32 vs float unboxing
  2011-08-22  7:19 [Caml-list] Int32 vs float unboxing Dmitry Bely
@ 2011-08-22  7:58 ` Alain Frisch
  2011-08-22  8:33   ` Dmitry Bely
  2011-08-24  8:10 ` [Caml-list] " Dmitry Bely
  1 sibling, 1 reply; 4+ messages in thread
From: Alain Frisch @ 2011-08-22  7:58 UTC (permalink / raw)
  To: Dmitry Bely; +Cc: Caml List

On 08/22/2011 09:19 AM, Dmitry Bely wrote:
> In the code below "s" reference is unboxed in sum_float loop, but not
> in sum_in32. Why?
>
> let sum_int32 v =
>    let s = ref 0l in
>    for i=0 to (Array.length v)-1 do
>      s := Int32.add !s (Array.unsafe_get v i)
>    done;
>    Int32.add !s Int32.zero
>
> let sum_float v =
>    let s = ref 0. in
>    for i=0 to (Array.length v)-1 do
>      s := !s +. (Array.unsafe_get v i)
>    done;
>    !s +. 0.

A random guess: since adding Int32.zero is the identity (contrary to 
adding 0.), the compiler is clever enough to optimize away this useless 
operation; unfortunately, this optimization breaks the unboxing pass.

-- Alain

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Int32 vs float unboxing
  2011-08-22  7:58 ` Alain Frisch
@ 2011-08-22  8:33   ` Dmitry Bely
  0 siblings, 0 replies; 4+ messages in thread
From: Dmitry Bely @ 2011-08-22  8:33 UTC (permalink / raw)
  To: Alain Frisch; +Cc: Caml List

On Mon, Aug 22, 2011 at 11:58 AM, Alain Frisch <alain.frisch@lexifi.com> wrote:
> On 08/22/2011 09:19 AM, Dmitry Bely wrote:
>>
>> In the code below "s" reference is unboxed in sum_float loop, but not
>> in sum_int32. Why?
>>
>> let sum_int32 v =
>>   let s = ref 0l in
>>   for i=0 to (Array.length v)-1 do
>>     s := Int32.add !s (Array.unsafe_get v i)
>>   done;
>>   Int32.add !s Int32.zero
>>
>> let sum_float v =
>>   let s = ref 0. in
>>   for i=0 to (Array.length v)-1 do
>>     s := !s +. (Array.unsafe_get v i)
>>   done;
>>   !s +. 0.
>
> A random guess: since adding Int32.zero is the identity (contrary to adding
> 0.), the compiler is clever enough to optimize away this useless operation;
> unfortunately, this optimization breaks the unboxing pass.

Probably no. If you replace

   Int32.add !s Int32.zero

with

   Int32.add !s Int32.one

s is still boxed. BTW, even zero addition is not optimized away.

- Dmitry Bely


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Caml-list] Re: Int32 vs float unboxing
  2011-08-22  7:19 [Caml-list] Int32 vs float unboxing Dmitry Bely
  2011-08-22  7:58 ` Alain Frisch
@ 2011-08-24  8:10 ` Dmitry Bely
  1 sibling, 0 replies; 4+ messages in thread
From: Dmitry Bely @ 2011-08-24  8:10 UTC (permalink / raw)
  To: Caml List

On Mon, Aug 22, 2011 at 11:19 AM, Dmitry Bely <dmitry.bely@gmail.com> wrote:
> In the code below "s" reference is unboxed in sum_float loop, but not
> in sum_in32. Why?

Answering to myself: because there are some specific float-oriented
optimizations that does not affect Int32.t (although I don't see why
they cannot be applied to Int32.t as well). Anyway, I decided that
implementing XTEA cipher in pure OCaml is a bad idea, so I still can
live without efficient Int32 calculations ;-)

- Dmitry Bely

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-08-24  8:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-22  7:19 [Caml-list] Int32 vs float unboxing Dmitry Bely
2011-08-22  7:58 ` Alain Frisch
2011-08-22  8:33   ` Dmitry Bely
2011-08-24  8:10 ` [Caml-list] " Dmitry Bely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).