File: int.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (172 lines) | stat: -rw-r--r-- 5,959 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
(* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
 *    Jagannathan, and Stephen Weeks.
 * Copyright (C) 1997-2000 NEC Research Institute.
 *
 * MLton is released under a HPND-style license.
 * See the file MLton-LICENSE for details.
 *)

functor Integer (I: PRIM_INTEGER): INTEGER_EXTRA =
struct

open I
type t = int

val precision': Int.int = Primitive.Int32.zextdToInt sizeInBits
val precision: Int.int option = SOME precision'
val sizeInBitsWord = Primitive.Word32.zextdToWord sizeInBitsWord

val maxInt: int option = SOME maxInt'
val minInt: int option = SOME minInt'

val sign: int -> Int.int =
  fn i => if i = zero
            then (0: Int.int)
          else if i < zero
            then (~1: Int.int)
          else (1: Int.int)

fun sameSign (x, y) = sign x = sign y

fun << (i, n) = 
   if Word.>= (n, sizeInBitsWord)
      then zero
      else I.<<? (i, Primitive.Word32.zextdFromWord n)
fun >> (i, n) = 
   if Word.>= (n, sizeInBitsWord)
      then zero
      else I.>>? (i, Primitive.Word32.zextdFromWord n)
fun ~>> (i, n) =
   if Word.< (n, sizeInBitsWord)
      then I.~>>? (i, Primitive.Word32.zextdFromWord n)
      else I.~>>? (i, Primitive.Word32.- (I.sizeInBitsWord, 0w1))
fun rol (i, n) = I.rolUnsafe (i, Primitive.Word32.zextdFromWord n)
fun ror (i, n) = I.rorUnsafe (i, Primitive.Word32.zextdFromWord n)

val fromInt = I.schckFromInt
val toInt = I.schckToInt

val fromLargeInt = I.schckFromLargeInt
val toLargeInt = I.schckToLargeInt
val fromLarge = fromLargeInt
val toLarge = toLargeInt

(* fmt constructs a string to represent the integer by building it into a
 * statically allocated buffer.  For the most part, this is a textbook
 * algorithm: loop starting at the end of the buffer; we use rem to
 * extract the next digit to put into the buffer; and we use quot to
 * figure out the part of the integer that we haven't yet formatted.
 * However, this function uses the negative absolute value of the input
 * number, which allows it to take into account minInt without any
 * special-casing.  This requires the rem function to behave in a very
 * specific way, or else things will go terribly wrong.  This may be a
 * concern when porting to platforms where the division hardware has a
 * different interpretation than SML about what happens when doing
 * division of negative numbers.
 *)
local
   (* Allocate a buffer large enough to hold any formatted integer in any radix.
    * The most that will be required is for minInt in binary.
    *)
   val maxNumDigits = Int.+ (precision', 1)
   val oneBuf = One.make (fn () => CharArray.array (maxNumDigits, #"\000"))
in
   fun fmt radix (n: int): string =
      One.use
      (oneBuf, fn buf => 
      let
         val radix = fromInt (StringCvt.radixToInt radix)
         fun loop (q, i: Int.int) =
            let
               val _ =
                  CharArray.update
                  (buf, i, StringCvt.digitToChar (toInt (~! (rem (q, radix)))))
               val q = quot (q, radix)
            in
               if q = zero
                  then
                     let
                        val start =
                           if n < zero
                              then
                                 let
                                    val i = Int.- (i, 1)
                                    val () = CharArray.update (buf, i, #"~")
                                 in
                                    i
                                 end
                           else i
                     in
                        CharArraySlice.vector
                        (CharArraySlice.slice (buf, start, NONE))
                     end
               else loop (q, Int.- (i, 1))
            end
      in
         loop (if n < zero then n else ~! n, Int.- (maxNumDigits, 1))
      end)
end      

val toString = fmt StringCvt.DEC

fun scan radix reader s =
   let
      (* Works with the negative of the number so that minInt can be scanned. *)
      val s = StringCvt.skipWS reader s
      fun charToDigit c =
         case StringCvt.charToDigit radix c of
            NONE => NONE
          | SOME n => SOME (fromInt n)
      val radixInt = fromInt (StringCvt.radixToInt radix)
      fun finishNum (s, n) =
         case reader s of
            NONE => SOME (n, s)
          | SOME (c, s') =>
               case charToDigit c of
                  NONE => SOME (n, s)
                | SOME n' => finishNum (s', n * radixInt - n')
      fun num s =
         case (reader s, radix) of
            (NONE, _) => NONE
          | (SOME (#"0", s), StringCvt.HEX) =>
               (case reader s of
                   NONE => SOME (zero, s)
                 | SOME (c, s') =>
                      if c = #"x" orelse c = #"X" then
                         case reader s' of
                            NONE => SOME (zero, s)
                          | SOME (c, s') =>
                               case charToDigit c of
                                  NONE => SOME (zero, s)
                                | SOME n => finishNum (s', ~! n)
                      else
                         case charToDigit c of
                            NONE => SOME (zero, s)
                          | SOME n => finishNum (s', ~! n))
          | (SOME (c, s), _) =>
               case charToDigit c of
                  NONE => NONE
                | SOME n => finishNum (s, ~! n)
    fun negate s =
       case num s of
          NONE => NONE
        | SOME (n, s) => SOME (~ n, s)
  in
     case reader s of
        NONE => NONE
      | SOME (c, s') =>
           case c of
              #"~" => num s'
            | #"-" => num s'
            | #"+" => negate s'
            | _ => negate s
  end

val fromString = StringCvt.scanString (scan StringCvt.DEC)

end

structure Int8 = Integer (Primitive.Int8)
structure Int16 = Integer (Primitive.Int16)
structure Int32 = Integer (Primitive.Int32)
structure Int64 = Integer (Primitive.Int64)