Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] Diff of /trunk/sml3d/src/common/data-buffer.sml
ViewVC logotype

Diff of /trunk/sml3d/src/common/data-buffer.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 321, Thu Oct 2 21:54:44 2008 UTC revision 322, Fri Oct 3 08:17:50 2008 UTC
# Line 34  Line 34 
34    
35    (* sizes of element types *)    (* sizes of element types *)
36      datatype 'a size = SZ of {      datatype 'a size = SZ of {
37            elemWid : int,                  (* number of channels in element *)
38          elemSz : c_size,                (* element size in bytes *)          elemSz : c_size,                (* element size in bytes *)
39          get : Ptr.t * int -> 'a,        (* get an element *)          get : Ptr.t * int -> 'a,        (* get an element *)
40          set : Ptr.t * int * 'a -> unit  (* set an element *)          set : Ptr.t * int * 'a -> unit  (* set an element *)
41        }        }
42    
43      local      local
44        fun size2 (SZ{elemSz, get, set}) = SZ{        fun size2 (SZ{elemWid=1, elemSz, get, set}) = SZ{
45                  elemWid = 2,
46                elemSz = 0w2 * elemSz,                elemSz = 0w2 * elemSz,
47                get = fn (p, i) => (get(p, i), get(p, i+1)),                get = fn (p, i) => let
48                set = fn (p, i, (a, b)) => (set(p, i, a); set(p, i+1, b))                        val j = i+i
49                          in
50                            (get(p, j), get(p, j+1))
51                          end,
52                  set = fn (p, i, (a, b)) => let
53                          val j = i+i
54                          in
55                            set(p, j, a); set(p, j+1, b)
56                          end
57              }              }
58        fun size3 (SZ{elemSz, get, set}) = SZ{        fun size3 (SZ{elemWid=1, elemSz, get, set}) = SZ{
59                  elemWid = 3,
60                elemSz = 0w3 * elemSz,                elemSz = 0w3 * elemSz,
61                get = fn (p, i) => (get(p, i), get(p, i+1), get(p, i+2)),                get = fn (p, i) => let
62                set = fn (p, i, (a, b, c)) => (set(p, i, a); set(p, i+1, b); set(p, i+2, c))                        val j = 3*i
63                          in
64                            (get(p, j), get(p, j+1), get(p, j+2))
65                          end,
66                  set = fn (p, i, (a, b, c)) => let
67                          val j = 3*i
68                          in
69                            set(p, j, a); set(p, j+1, b); set(p, j+2, c)
70                          end
71              }              }
72        fun size4 (SZ{elemSz, get, set}) = SZ{        fun size4 (SZ{elemWid=1, elemSz, get, set}) = SZ{
73                  elemWid = 4,
74                elemSz = 0w4 * elemSz,                elemSz = 0w4 * elemSz,
75                get = fn (p, i) => (get(p, i), get(p, i+1), get(p, i+2), get(p, i+3)),                get = fn (p, i) => let
76                set = fn (p, i, (a, b, c, d)) => (set(p, i, a); set(p, i+1, b); set(p, i+2, c); set(p, i+3, d))                        val j = 4*i
77                          in
78                            (get(p, j), get(p, j+1), get(p, j+2), get(p, j+3))
79                          end,
80                  set = fn (p, i, (a, b, c, d)) =>let
81                          val j = 4*i
82                          in
83                            set(p, j, a); set(p, j+1, b); set(p, j+2, c); set(p, j+3, d)
84                          end
85              }              }
86      in      in
87      val sizeb   = SZ{elemSz = 0w1, get = Ptr.getInt8, set = Ptr.setInt8}      val sizeb   = SZ{elemWid=1, elemSz = 0w1, get = Ptr.getInt8, set = Ptr.setInt8}
88      val size2b  = size2 sizeb      val size2b  = size2 sizeb
89      val size3b  = size3 sizeb      val size3b  = size3 sizeb
90      val size4b  = size4 sizeb      val size4b  = size4 sizeb
91      val sizeub  = SZ{elemSz = 0w1, get = Ptr.getWord8, set = Ptr.setWord8}      val sizeub  = SZ{elemWid=1, elemSz = 0w1, get = Ptr.getWord8, set = Ptr.setWord8}
92      val size2ub = size2 sizeub      val size2ub = size2 sizeub
93      val size3ub = size3 sizeub      val size3ub = size3 sizeub
94      val size4ub = size4 sizeub      val size4ub = size4 sizeub
95      val sizes   = SZ{elemSz = 0w2, get = Ptr.getInt16, set = Ptr.setInt16}      val sizes   = SZ{elemWid=1, elemSz = 0w2, get = Ptr.getInt16, set = Ptr.setInt16}
96      val size2s  = size2 sizes      val size2s  = size2 sizes
97      val size3s  = size3 sizes      val size3s  = size3 sizes
98      val size4s  = size4 sizes      val size4s  = size4 sizes
99      val sizeus  = SZ{elemSz = 0w2, get = Ptr.getWord16, set = Ptr.setWord16}      val sizeus  = SZ{elemWid=1, elemSz = 0w2, get = Ptr.getWord16, set = Ptr.setWord16}
100      val size2us = size2 sizeus      val size2us = size2 sizeus
101      val size3us = size3 sizeus      val size3us = size3 sizeus
102      val size4us = size4 sizeus      val size4us = size4 sizeus
103      val sizei   = SZ{elemSz = 0w4, get = Ptr.getInt32, set = Ptr.setInt32}      val sizei   = SZ{elemWid=1, elemSz = 0w4, get = Ptr.getInt32, set = Ptr.setInt32}
104      val size2i  = size2 sizei      val size2i  = size2 sizei
105      val size3i  = size3 sizei      val size3i  = size3 sizei
106      val size4i  = size4 sizei      val size4i  = size4 sizei
107      val sizeui  = SZ{elemSz = 0w4, get = Ptr.getWord32, set = Ptr.setWord32}      val sizeui  = SZ{elemWid=1, elemSz = 0w4, get = Ptr.getWord32, set = Ptr.setWord32}
108      val size2ui = size2 sizeui      val size2ui = size2 sizeui
109      val size3ui = size3 sizeui      val size3ui = size3 sizeui
110      val size4ui = size4 sizeui      val size4ui = size4 sizeui
111      val sizef   = SZ{elemSz = 0w4, get = Ptr.getReal32, set = Ptr.setReal32}      val sizef   = SZ{elemWid=1, elemSz = 0w4, get = Ptr.getReal32, set = Ptr.setReal32}
112      val size2f  = size2 sizef      val size2f  = size2 sizef
113      val size3f  = size3 sizef      val size3f  = size3 sizef
114      val size4f  = size4 sizef      val size4f  = size4 sizef
115      val sized   = SZ{elemSz = 0w8, get = Ptr.getReal64, set = Ptr.setReal64}      val sized   = SZ{elemWid=1, elemSz = 0w8, get = Ptr.getReal64, set = Ptr.setReal64}
116      val size2d  = size2 sized      val size2d  = size2 sized
117      val size3d  = size3 sized      val size3d  = size3 sized
118      val size4d  = size4 sized      val size4d  = size4 sized
# Line 95  Line 123 
123            else (free (!data); data := Ptr.null)            else (free (!data); data := Ptr.null)
124    
125    (* wrap pre-existing C data *)    (* wrap pre-existing C data *)
126      fun wrapCData free (SZ{elemSz, get, set}, nelems, ptr) = let      fun wrapCData free (SZ{elemWid, elemSz, get, set}, nelems, ptr) = let
127            val len = C_Size.fromInt nelems            val len = C_Size.fromInt nelems
128            val nbytes = len * elemSz            val nbytes = len * elemSz
129            val buf = Final.new(B{            val buf = Final.new(B{
# Line 107  Line 135 
135              buf              buf
136            end            end
137    
138      fun make {alloc, free} (SZ{elemSz, get, set}, nelems) = if (nelems <= 0)      fun make {alloc, free} (SZ{elemWid, elemSz, get, set}, nelems) = if (nelems <= 0)
139            then raise Size            then raise Size
140            else let            else let
141              val len = C_Size.fromInt nelems              val len = C_Size.fromInt nelems
# Line 122  Line 150 
150                buf                buf
151              end              end
152    
153      fun new (SZ{elemSz, get, set}, nelems) = if (nelems <= 0)      fun new (SZ{elemWid, elemSz, get, set}, nelems) = if (nelems <= 0)
154            then raise Size            then raise Size
155            else let            else let
156              val len = C_Size.fromInt nelems              val len = C_Size.fromInt nelems
# Line 144  Line 172 
172      fun length buf = Final.withValue (buf, fn (B{len, ...}) => C_Size.toInt len)      fun length buf = Final.withValue (buf, fn (B{len, ...}) => C_Size.toInt len)
173    
174      fun getFn get (buf, i) = Final.withValue (buf, fn (B{len, data, ...}) =>      fun getFn get (buf, i) = Final.withValue (buf, fn (B{len, data, ...}) =>
175            if (C_Size.<=(len, C_Size.fromInt i)) then raise Subscript            if (!data = Ptr.null) then raise NullBuffer
176            else if (!data = Ptr.null) then raise NullBuffer            else if (C_Size.<=(len, C_Size.fromInt i)) then raise Subscript
177            else get(!data, i))            else get(!data, i))
178      fun setFn (set : (Ptr.t * int * 'a) -> unit) (buf, i, v) =      fun setFn (set : (Ptr.t * int * 'a) -> unit) (buf, i, v) =
179            Final.withValue (buf, fn (B{len, data, ...}) =>            Final.withValue (buf, fn (B{len, data, ...}) =>
180              if (C_Size.<=(len, C_Size.fromInt i)) then raise Subscript              if (!data = Ptr.null) then raise NullBuffer
181              else if (!data = Ptr.null) then raise NullBuffer              else if (C_Size.<=(len, C_Size.fromInt i)) then raise Subscript
182              else set(!data, i, v))              else set(!data, i, v))
183    
184      val getb  = getFn Ptr.getInt8      val getb  = getFn Ptr.getInt8
# Line 170  Line 198 
198      val getd  = getFn Ptr.getReal64      val getd  = getFn Ptr.getReal64
199      val setd  = setFn Ptr.setReal64      val setd  = setFn Ptr.setReal64
200    
201      fun get2Fn get = getFn (fn (ptr, i) => (get(ptr, i), get(ptr, i+1)))      fun get2Fn get = getFn (fn (ptr, i) => let
202      fun set2Fn set = setFn (fn (ptr, i, (a, b)) => (set(ptr, i, a); set(ptr, i+1, b)))            val j = i+i
203              in
204                (get(ptr, j), get(ptr, j+1))
205              end)
206        fun set2Fn set = setFn (fn (ptr, i, (a, b)) => let
207              val j = i+i
208              in
209                set(ptr, j, a); set(ptr, j+1, b)
210              end)
211    
212      val get2b  = get2Fn Ptr.getInt8      val get2b  = get2Fn Ptr.getInt8
213      val set2b  = set2Fn Ptr.setInt8      val set2b  = set2Fn Ptr.setInt8
# Line 190  Line 226 
226      val get2d  = get2Fn Ptr.getReal64      val get2d  = get2Fn Ptr.getReal64
227      val set2d  = set2Fn Ptr.setReal64      val set2d  = set2Fn Ptr.setReal64
228    
229      fun get3Fn get = getFn (fn (ptr, i) => (get(ptr, i), get(ptr, i+1), get(ptr, i+2)))      fun get3Fn get = getFn (fn (ptr, i) => let
230      fun set3Fn set =            val j = 3*i
231            setFn (fn (ptr, i, (a, b, c)) => (set(ptr, i, a); set(ptr, i+1, b); set(ptr, i+2, c)))            in
232                (get(ptr, j), get(ptr, j+1), get(ptr, j+2))
233              end)
234        fun set3Fn set = setFn (fn (ptr, i, (a, b, c)) => let
235              val j = 3*i
236              in
237                set(ptr, j, a); set(ptr, j+1, b); set(ptr, j+2, c)
238              end)
239    
240      val get3b  = get3Fn Ptr.getInt8      val get3b  = get3Fn Ptr.getInt8
241      val set3b  = set3Fn Ptr.setInt8      val set3b  = set3Fn Ptr.setInt8
# Line 211  Line 254 
254      val get3d  = get3Fn Ptr.getReal64      val get3d  = get3Fn Ptr.getReal64
255      val set3d  = set3Fn Ptr.setReal64      val set3d  = set3Fn Ptr.setReal64
256    
257      fun get4Fn get = getFn (fn (ptr, i) => (get(ptr, i), get(ptr, i+1), get(ptr, i+2), get(ptr, i+3)))      fun get4Fn get = getFn (fn (ptr, i) => let
258      fun set4Fn set = setFn            val j = 3*i
259            (fn (ptr, i, (a, b, c, d)) => (            in
260              set(ptr, i, a); set(ptr, i+1, b); set(ptr, i+2, c); set(ptr, i+3, d)))              (get(ptr, j), get(ptr, j+1), get(ptr, j+2), get(ptr, j+3))
261              end)
262        fun set4Fn set = setFn (fn (ptr, i, (a, b, c, d)) => let
263              val j = 3*i
264              in
265                set(ptr, j, a); set(ptr, j+1, b); set(ptr, j+2, c); set(ptr, j+3, d)
266              end)
267    
268      val get4b  = get4Fn Ptr.getInt8      val get4b  = get4Fn Ptr.getInt8
269      val set4b  = set4Fn Ptr.setInt8      val set4b  = set4Fn Ptr.setInt8
# Line 236  Line 285 
285    (* polymorphic operations on buffers *)    (* polymorphic operations on buffers *)
286    
287      fun get (buf, i) = Final.withValue (buf, fn (B{len, data, get=g, ...}) =>      fun get (buf, i) = Final.withValue (buf, fn (B{len, data, get=g, ...}) =>
288            if (C_Size.<=(len, C_Size.fromInt i)) then raise Subscript            if (!data = Ptr.null) then raise NullBuffer
289            else if (!data = Ptr.null) then raise NullBuffer            else if (C_Size.<=(len, C_Size.fromInt i)) then raise Subscript
290            else g(!data, i))            else g(!data, i))
291    
292      fun set (buf, i, v) = Final.withValue (buf, fn (B{len, data, set=s, ...}) =>      fun set (buf, i, v) = Final.withValue (buf, fn (B{len, data, set=s, ...}) =>
293            if (C_Size.<=(len, C_Size.fromInt i)) then raise Subscript            if (!data = Ptr.null) then raise NullBuffer
294            else if (!data = Ptr.null) then raise NullBuffer            else if (C_Size.<=(len, C_Size.fromInt i)) then raise Subscript
295            else s(!data, i, v))            else s(!data, i, v))
296    
297      fun update (buf, i, f) = Final.withValue (buf, fn (B{len, data, get, set, ...}) =>      fun update (buf, i, f) = Final.withValue (buf, fn (B{len, data, get, set, ...}) =>
298            if (C_Size.<=(len, C_Size.fromInt i)) then raise Subscript            if (!data = Ptr.null) then raise NullBuffer
299            else if (!data = Ptr.null) then raise NullBuffer            else if (C_Size.<=(len, C_Size.fromInt i)) then raise Subscript
300            else set(!data, i, f(get(!data, i))))            else set(!data, i, f(get(!data, i))))
301    
302      fun app (f : 'a -> unit) buf = Final.withValue (buf, fn (B{len, data, get, ...}) => let      fun app (f : 'a -> unit) buf = Final.withValue (buf, fn (B{len, data, get, ...}) => let

Legend:
Removed from v.321  
changed lines
  Added in v.322

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