| 1 |
(* bitop-fn.sml |
| 2 |
* 2005 Matthew Fluet ([email protected]) |
| 3 |
* Adapted for MLton. |
| 4 |
*) |
| 5 |
|
| 6 |
(* bitop-fn.sml |
| 7 |
* |
| 8 |
* Bit operations on integers as if they were words |
| 9 |
* (based on suggestions from Allen Leung). |
| 10 |
* |
| 11 |
* Copyright (c) 2004 by The Fellowship of SML/NJ |
| 12 |
* |
| 13 |
* Author: Matthias Blume ([email protected]) |
| 14 |
*) |
| 15 |
functor IntBitOps (structure I : INTEGER structure W : WORD) : sig |
| 16 |
|
| 17 |
(* We use a functor to express this stuff generically. |
| 18 |
* If efficiency is a concern, it may be necessary to |
| 19 |
* expand this "by hand".... *) |
| 20 |
|
| 21 |
type int = I.int |
| 22 |
|
| 23 |
(* unsigned arithmetic. |
| 24 |
* non-overflow trapping |
| 25 |
*) |
| 26 |
val ++ : int * int -> int |
| 27 |
val -- : int * int -> int |
| 28 |
val ** : int * int -> int |
| 29 |
val udiv : int * int -> int |
| 30 |
val umod : int * int -> int |
| 31 |
val umin : int * int -> int |
| 32 |
val umax : int * int -> int |
| 33 |
|
| 34 |
(* bit ops *) |
| 35 |
val notb : int -> int |
| 36 |
val andb : int * int -> int |
| 37 |
val orb : int * int -> int |
| 38 |
val xorb : int * int -> int |
| 39 |
val << : int * Word.word -> int |
| 40 |
val >> : int * Word.word -> int |
| 41 |
val ~>> : int * Word.word -> int |
| 42 |
|
| 43 |
(* unsigned comparisons *) |
| 44 |
val ule : int * int -> bool |
| 45 |
val ulg : int * int -> bool |
| 46 |
val ugt : int * int -> bool |
| 47 |
val uge : int * int -> bool |
| 48 |
val ucompare : int * int -> order |
| 49 |
|
| 50 |
end = struct |
| 51 |
|
| 52 |
type int = I.int |
| 53 |
|
| 54 |
local |
| 55 |
val to = W.fromLargeInt o I.toLarge |
| 56 |
val from = I.fromLarge o W.toLargeIntX |
| 57 |
fun bop f (x, y) = from (f (to x, to y)) (* binary op *) |
| 58 |
fun uop f x = from (f (to x)) (* unary op *) |
| 59 |
fun sop f (x, y) = from (f (to x, y)) (* shift-like op *) |
| 60 |
fun cop f (x, y) = f (to x, to y) (* comparison-like op *) |
| 61 |
in |
| 62 |
val ++ = bop W.+ |
| 63 |
val -- = bop W.- |
| 64 |
val ** = bop W.* |
| 65 |
val udiv = bop W.div |
| 66 |
val umod = bop W.mod |
| 67 |
val andb = bop W.andb |
| 68 |
val orb = bop W.orb |
| 69 |
val xorb = bop W.xorb |
| 70 |
val notb = uop W.notb |
| 71 |
|
| 72 |
val umax = bop W.max |
| 73 |
val umin = bop W.min |
| 74 |
|
| 75 |
val << = sop W.<< |
| 76 |
val >> = sop W.>> |
| 77 |
val ~>> = sop W.~>> |
| 78 |
|
| 79 |
val ulg = cop W.< |
| 80 |
val ule = cop W.<= |
| 81 |
val ugt = cop W.> |
| 82 |
val uge = cop W.>= |
| 83 |
val ucompare = cop W.compare |
| 84 |
end |
| 85 |
end |