From: Remi Vanicat <remi.vanicat@laposte.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] ASM code generated by OCAML
Date: Sat, 20 Dec 2003 17:50:54 +0100 [thread overview]
Message-ID: <87y8t78mc1.dlv@wanadoo.fr> (raw)
In-Reply-To: <20031220105040.7a079f67.segfault@email.it> (Francesco Abbate's message of "Sat, 20 Dec 2003 10:50:40 +0100")
Francesco Abbate <segfault@email.it> writes:
> Dear Ocamlers,
>
> ok, I confess that I'm a little bit paranoid and I often
> look to the assembler code generated by Ocaml to get an
> idea of real efficience of the compiler.
>
> While, generally speaking, the ASM code generated by ocaml
> is pretty good, I wonder why the following function
> is not decently assembled by ocaml :
> -----------------------------------------
> let rec conv n =
> let r = n mod 10
> and n' = n / 10 in
> if n' = 0 then r
> else r + 8 * (conv n')
> -----------------------------------------
> nor the C version is decently assembled by GCC
> -----------------------------------------
> int
> conv (int n)
> {
> int m = n / 10, r = n % 10;
> if (m > 0)
> return r + 8 * conv (m);
> return m;
> }
> -----------------------------------------
> So my answer is why nor Ocaml nor GCC does generate efficient
> assembler code ?
>
> I will attempt to give a tentative answer
> - for some reason the compiler does not understands the (n mod 10)
> and (n /10) both can be avaluated with a simgle "idiv"
> instruction
This require some analysis that isn't needed in general
> - for some reason the compilers does not conceive to have a loop
> which push something on the stack at each cycle.
Ocaml (and I believe GCC) only optimize code witch is tail recursive,
that is the result of the function is the result of the recursive
case.
You should transform your code into a tail rec function by hand :
let conv n =
let rec helper n mult accu =
if n = 0 then accu
else
let r = n mod 10
and n' = n / 10 in
helper n' (mult * 8) (accu + r * mult) in
helper n 1 0
As you can see, the result of the recursive function helper is the
result of the recursive call.
--
Rémi Vanicat
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
prev parent reply other threads:[~2003-12-20 16:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-12-20 9:50 Francesco Abbate
2003-12-20 16:50 ` Remi Vanicat [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87y8t78mc1.dlv@wanadoo.fr \
--to=remi.vanicat@laposte.net \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox