caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* more on nativeint interface
@ 2001-01-18 19:07 Norman Ramsey
  2001-01-21 10:45 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Norman Ramsey @ 2001-01-18 19:07 UTC (permalink / raw)
  To: caml-list

It would be useful to have
  val width : int   (* number of bits in an integer of type nativeint *)
so that, for example, I could do sign extension by suitable shifting.

Meanwhile, I could play the following sorts of games:

  module N = Nativeint
  let width =
    if N.shift_right_logical N.minus_one 32 = N.zero then 32 else 64

  let sx k n = N.shift_right (N.shift_left n (width-k)) (width-k)
    (* sign-extend the least significant k bits of integer n *)

  let sx13 = sx 13

Unfortunately, the code that the ocamlopt produces for this function
(on x86) is pretty bad.

I can get significantly better code by using this source:

  let width=32
  let sx13b n = N.shift_right (N.shift_left n (width-13)) (width-13)

But of course this code isn't portable.


Is there anything I can do to get the compiler to do a better job with
nativeint? 


Norman



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

* Re: more on nativeint interface
  2001-01-18 19:07 more on nativeint interface Norman Ramsey
@ 2001-01-21 10:45 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2001-01-21 10:45 UTC (permalink / raw)
  To: Norman Ramsey; +Cc: caml-list

> It would be useful to have
>   val width : int   (* number of bits in an integer of type nativeint *)
> so that, for example, I could do sign extension by suitable shifting.

Good idea.  Actually, this information is already available in
Sys.word_size, but repeating it in Nativeint could help.

>   let sx k n = N.shift_right (N.shift_left n (width-k)) (width-k)
>     (* sign-extend the least significant k bits of integer n *)
> 
>   let sx13 = sx 13
> 
> Unfortunately, the code that the ocamlopt produces for this function
> (on x86) is pretty bad.
> 
> I can get significantly better code by using this source:
> 
>   let width=32
>   let sx13b n = N.shift_right (N.shift_left n (width-13)) (width-13)
> 
> But of course this code isn't portable.
> 
> Is there anything I can do to get the compiler to do a better job with
> nativeint? 

The compiler support for int32/int64/nativeint in OCaml 3.00 was
pretty minimal; the next release will have more optimizations,
especially w.r.t. unboxing.  (Constant propagation for these integer
types would be nice too, except that the compiler must be very careful
with nativeint, since the compiler can run on a machine with a
different word size than the machine running the final program.)
Still, I don't think variable-sized shifts will improve.

One trick you can play is specialize the function at link-time:

let sx13b =
  match Sys.word_size with
    32 ->
     (fun n -> N.shift_right (N.shift_left n (32-13)) (32-13))
  | 64 ->
     (fun n -> N.shift_right (N.shift_left n (64-13)) (64-13))
  |  _ -> assert false

The shifts will be compiled better, however sx13b is no longer a
"known function", so calls to it will always be indirect, i.e. slower.
So this may not be a performance win after all.

- Xavier Leroy



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

end of thread, other threads:[~2001-01-21 21:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-01-18 19:07 more on nativeint interface Norman Ramsey
2001-01-21 10:45 ` Xavier Leroy

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).