1 (* Copyright (C) 2009 Matthew Fluet.
2 * Copyright (C) 2004-2007 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
9 functor IntSize (S: INT_SIZE_STRUCTS): INT_SIZE =
14 datatype t = T of {bits: Bits.t}
16 fun bits (T {bits, ...}) = bits
18 fun compare (s, s') = Bits.compare (bits s, bits s')
20 val {equals, ...} = Relation.compare compare
22 fun isValidSize (i: int) =
23 (1 <= i andalso i <= 32) orelse i = 64
25 val sizes: Bits.t list =
28 (Vector.tabulate (65, fn i => if isValidSize i
29 then SOME (Bits.fromInt i)
33 fun make i = T {bits = i}
35 val allVector = Vector.tabulate (65, fn i =>
37 then SOME (make (Bits.fromInt i))
40 fun fromBits (b: Bits.t): t =
41 case Vector.sub (allVector, Bits.toInt b) handle Subscript => NONE of
42 NONE => Error.bug (concat ["IntSize.fromBits: strange int size: ", Bits.toString b])
45 val all = List.map (sizes, fromBits)
47 val memoize: (t -> 'a) -> t -> 'a =
50 val v = Vector.map (allVector, fn opt => Option.map (opt, f))
52 fn T {bits = b, ...} => valOf (Vector.sub (v, Bits.toInt b))