Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/compiler/MiscUtil/util/literal-to-num.sml
ViewVC logotype

Annotation of /sml/trunk/src/compiler/MiscUtil/util/literal-to-num.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 93 - (view) (download)
Original Path: sml/branches/SMLNJ/src/compiler/MiscUtil/util/literal-to-num.sml

1 : monnier 16 (* literal-to-num.sml
2 :     *
3 :     * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
4 :     *
5 :     * Conversions from integer/word literals (which are represented as
6 :     * arbitrary precision ints) to fixed size.
7 :     *
8 :     * This structure is a hack, which should be replaced by a parameterized
9 :     * numeric types.
10 :     *)
11 :    
12 :     signature LITERAL_TO_NUM =
13 :     sig
14 :     val int : IntInf.int -> int
15 :     val int32 : IntInf.int -> Int32.int
16 :     val word : IntInf.int -> word
17 :     val word8 : IntInf.int -> word
18 :     val word32 : IntInf.int -> Word32.word
19 :     end
20 :    
21 :     structure LiteralToNum : LITERAL_TO_NUM =
22 :     struct
23 :    
24 :     val zero = IntInf.fromInt 0
25 :     val one = IntInf.fromInt 1
26 :     val two = IntInf.fromInt 2
27 :     val two_8 = IntInf.pow(two, 8)
28 :     val two_30 = IntInf.pow(two, 30)
29 :     val two_31 = IntInf.pow(two, 31)
30 :     val two_32 = IntInf.pow(two, 32)
31 :     val neg_two_30 = IntInf.~ two_31
32 :     val neg_two_31 = IntInf.~ two_31
33 :    
34 :     (* return n if it is in the range [lo..hi-1]; otherwise raise Overflow *)
35 :     fun chkIntRange (lo, hi) n =
36 :     if (IntInf.<=(lo, n) andalso IntInf.<(n, hi))
37 :     then n
38 :     else raise Overflow
39 :    
40 :     (* return n if it is in the range [0..hi-1]; otherwise raise Overflow *)
41 :     fun chkWordRange hi n = if (IntInf.<(n, hi)) then n else raise Overflow
42 :    
43 :     fun int i = IntInf.toInt(chkIntRange (neg_two_30, two_30) i)
44 :     fun int32 i = IntInf.toLarge(chkIntRange (neg_two_31, two_31) i)
45 :     fun word w = Word.fromLargeInt(IntInf.toLarge(chkWordRange two_31 w))
46 :     fun word8 w = Word.fromInt(IntInf.toInt(chkWordRange two_8 w))
47 :     fun word32 w =
48 :     if (IntInf.>=(w, two_32))
49 :     then raise Overflow
50 :     else Word32.fromLargeInt(IntInf.toLarge w)
51 :     handle Overflow => let
52 :     val (d, m) = IntInf.divmod(w, two)
53 :     val d = Word32.fromLargeInt(IntInf.toLarge d)
54 :     in
55 :     if (m = zero) then Word32.<<(d, 0w1) else Word32.<<(d, 0w1)+0w1
56 :     end
57 :    
58 :     end
59 :    
60 :    
61 :     (*
62 :     * $Log: literal-to-num.sml,v $
63 : monnier 93 * Revision 1.1.1.1 1998/04/08 18:39:16 george
64 :     * Version 110.5
65 :     *
66 : monnier 16 *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0