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 /MLRISC/releases/release-110.84/library/fixed-point.sml
ViewVC logotype

Annotation of /MLRISC/releases/release-110.84/library/fixed-point.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 245 - (view) (download)
Original Path: sml/branches/SMLNJ/src/MLRISC/library/fixed-point.sml

1 : monnier 245 functor FixedPointFn (val decimal_bits : int) : FIXED_POINT =
2 :     struct
3 :    
4 :     structure W = Word31
5 :    
6 :     infix << >>
7 :     infix & ||
8 :    
9 :     val decimal_places = (decimal_bits + 2) div 3
10 :     val realToString = Real.fmt (StringCvt.FIX(SOME decimal_places))
11 :    
12 :     val op<< = W.<<
13 :     val op>> = W.>>
14 :     val op& = W.andb
15 :     val op|| = W.orb
16 :     val op div = W.div
17 :     val word = W.fromInt
18 :     val int = W.toInt
19 :    
20 :     type fixed_point = W.word
21 :    
22 :     val bits = word decimal_bits
23 :     val bits2 = bits >> 0w1
24 :     val bits4 = bits >> 0w2
25 :    
26 :     val zero = 0w0
27 :     val one = 0w1 << bits
28 :     val realOne = Real.fromInt(int one)
29 :    
30 :     val op+ = W.+
31 :     val op- = W.-
32 :     val op* = fn(i:fixed_point,j:fixed_point) => W.*(i >> bits2,j >> bits2)
33 :     val op/ = fn(i:fixed_point,j:fixed_point) => (i << bits) div j
34 :     val op< = W.<
35 :     val op> = W.>
36 :     val op>= = W.>=
37 :     val op<= = W.<=
38 :     val != = fn(i:fixed_point,j:fixed_point) => i <> j
39 :     val == = fn(i:fixed_point,j:fixed_point) => i = j
40 :     val compare = W.compare
41 :    
42 :     val mask = one - 0w1
43 :    
44 :     fun fromInt i = (word i) << bits
45 :     fun fixed_point(a,b) = let val b' = word b
46 :     in (((word a) << bits) + (b' >> 0w1)) div b' end
47 :     fun fromReal r = word(Real.round(Real.*(r, realOne)))
48 :     fun toReal p = Real./(Real.fromInt(int p), realOne)
49 :     fun toWord f = f
50 :     fun toString p = realToString(toReal p)
51 :     handle Overflow => "inf"
52 :    
53 :     fun scale(i,j) = W.*(i, word j)
54 :     fun i div j = W.div(i,word j)
55 :    
56 :     val max = W.max
57 :     val min = W.min
58 :    
59 :     end
60 :    
61 :     (*
62 :     * $Log$
63 :     *)

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