gogyou 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  #vim: set fileencoding:utf-8
2
2
 
3
3
  # gogyou.rb
4
- # - AUTHOR: dearblue <dearblue@users.sourceforge.jp>
5
- # - WEBSIZE: http://sourceforge.jp/projects/rutsubo/
4
+ # - AUTHOR: dearblue <dearblue@users.osdn.me>
5
+ # - WEBSIZE: https://osdn.jp/projects/rutsubo/
6
6
  # - LICENSE: same as 2-clause BSD License
7
7
 
8
8
  #--
@@ -24,42 +24,61 @@
24
24
  #
25
25
  # 原始的な型情報は Gogyou::Primitives で定義してあり、struct や union メソッド内で利用できる型を次の表に示します:
26
26
  #
27
- # ==== C 標準型
28
- # 符号あり 符号なし
29
- # ---- ----
30
- # char 型 char uchar
31
- # unsigned_char
32
- # short short ushort
33
- # unsigned_short
34
- # int int uint
35
- # unsigned_int
36
- # long long ulong
37
- # unsigned_long
38
- # long long 型 longlong ulonglong
39
- # long_long unsigned_long_long
40
- # sizeof ssize_t size_t
41
- # ポインタ整数型 intptr_t uintptr_t
42
- #
43
- # バイトオーダー環境依存 バイトオーダー反転
44
- # 符号あり 符号なし 符号あり 符号なし
45
- # ---- ---- ---- ----
46
- # 8ビット整数型 int8_t uint8_t N/A N/A
47
- # 16ビット整数型 int16_t uint16_t int16_swap uint16_swap
48
- # 32ビット整数型 int32_t uint32_t int32_swap uint32_swap
49
- # 64ビット整数型 int64_t uint64_t int64_swap uint64_swap
50
- # 32ビット浮動小数点数型 float N/A float_swap N/A
51
- # 64ビット浮動小数点数型 double N/A double_swap N/A
52
- #
53
- # ビッグエンディアン リトルエンディアン
54
- # 符号あり 符号なし 符号あり 符号なし
55
- # ---- ---- ---- ----
56
- # 16ビット整数型 int16_be uint16_be int16_le uint16_le
57
- # 24ビット整数型 int24_be uint24_be int24_le uint24_le
58
- # 32ビット整数型 int32_be uint32_be int32_le uint32_le
59
- # 48ビット整数型 int48_be uint48_be int48_le uint48_le
60
- # 64ビット整数型 int64_be uint64_be int64_le uint64_le
61
- # 32ビット浮動小数点数型 float_be N/A float_le N/A
62
- # 64ビット浮動小数点数型 double_be N/A double_le N/A
27
+ # ==== 型名
28
+ #
29
+ # * C の型名
30
+ # 符号あり 符号なし
31
+ # ---- ----
32
+ # char char uchar
33
+ # unsigned_char
34
+ # short short ushort
35
+ # unsigned_short
36
+ # int int uint
37
+ # unsigned_int
38
+ # long long ulong
39
+ # unsigned_long
40
+ # long long longlong ulonglong
41
+ # long_long unsigned_long_long
42
+ # sizeof 型 ssize_t size_t
43
+ # ポインタ整数型 intptr_t uintptr_t
44
+ # float float N/A
45
+ # double double N/A
46
+ #
47
+ # * バイトオーダー環境依存・ビット数環境非依存
48
+ # バイトオーダー環境依存 バイトオーダー反転
49
+ # 符号あり 符号なし 符号あり 符号なし
50
+ # ---- ---- ---- ----
51
+ # 8ビット整数型 int8_t uint8_t N/A N/A
52
+ # 16ビット整数型 int16_t uint16_t int16_swap uint16_swap
53
+ # 32ビット整数型 int32_t uint32_t int32_swap uint32_swap
54
+ # 64ビット整数型 int64_t uint64_t int64_swap uint64_swap
55
+ # 16ビット浮動小数点実数型 float16_t N/A float16_swap N/A
56
+ # 32ビット浮動小数点実数型 float32_t N/A float32_swap N/A
57
+ # 64ビット浮動小数点実数型 float64_t N/A float64_swap N/A
58
+ #
59
+ # * バイトオーダー・ビット数環境非依存
60
+ # ビッグエンディアン リトルエンディアン
61
+ # 符号あり 符号なし 符号あり 符号なし
62
+ # ---- ---- ---- ----
63
+ # 16ビット整数型 int16_be uint16_be int16_le uint16_le
64
+ # 24ビット整数型 int24_be uint24_be int24_le uint24_le
65
+ # 32ビット整数型 int32_be uint32_be int32_le uint32_le
66
+ # 48ビット整数型 int48_be uint48_be int48_le uint48_le
67
+ # 64ビット整数型 int64_be uint64_be int64_le uint64_le
68
+ # 16ビット浮動小数点実数型 float16_be N/A float16_le N/A
69
+ # 32ビット浮動小数点実数型 float32_be N/A float32_le N/A
70
+ # 64ビット浮動小数点実数型 float64_be N/A float64_le N/A
71
+ #
72
+ # * 固定小数点実数型 (ビット数環境非依存)
73
+ #
74
+ # バイトオーダー環境依存 バイトオーダー環境非依存
75
+ #
76
+ # 16ビット固定小数点実数型(小数部8ビット) fixed16q8_t fixed16q8_swap fixed16q8_be fixed16q8_le
77
+ # 32ビット固定小数点実数型(小数部6ビット) fixed32q6_t fixed32q6_swap fixed32q6_be fixed32q6_le
78
+ # 32ビット固定小数点実数型(小数部8ビット) fixed32q8_t fixed32q8_swap fixed32q8_be fixed32q8_le
79
+ # 32ビット固定小数点実数型(小数部12ビット) fixed32q12_t fixed32q12_swap fixed32q12_be fixed32q12_le
80
+ # 32ビット固定小数点実数型(小数部16ビット) fixed32q16_t fixed32q16_swap fixed32q16_be fixed32q16_le
81
+ # 32ビット固定小数点実数型(小数部24ビット) fixed32q24_t fixed32q24_swap fixed32q24_be fixed32q24_le
63
82
  #
64
83
  #
65
84
  # ==== 利用者定義の型情報
@@ -134,12 +153,19 @@
134
153
  # * #\<field> / #\<field>= - 構造体・共用体のフィールドへの参照・代入メソッド。配列の場合は定義されない。
135
154
  # * #[] / []= - 配列の要素への参照・代入メソッド。構造体・共用体の場合は定義されない。
136
155
  #
156
+ # * 構造体のメモリイメージとなるバッファオブジェクト
157
+ #
158
+ # 構造体のバイト列を表現するオブジェクトのことです。
159
+ #
160
+ # * String
161
+ # * Fiddle::Pointer
162
+ #
137
163
  module Gogyou
138
164
  Gogyou = self
139
165
 
140
166
  require_relative "gogyou/version"
141
167
  require_relative "gogyou/typespec"
142
- require_relative "gogyou/mixin"
168
+ require_relative "gogyou/extensions"
143
169
  require_relative "gogyou/model"
144
170
  require_relative "gogyou/primitives"
145
171
  require_relative "gogyou/accessor"
@@ -149,7 +175,7 @@ module Gogyou
149
175
 
150
176
  Gogyou::Primitives.constants.each do |n|
151
177
  prim = Gogyou::Primitives.const_get(n)
152
- next unless prim.kind_of?(Gogyou::Primitives::Primitive)
178
+ next unless prim.kind_of?(Gogyou::Primitive)
153
179
  TYPEMAP[prim.name.to_sym] = prim
154
180
  end
155
181
 
@@ -50,10 +50,12 @@ module Gogyou
50
50
  @buffer__GOGYOU__
51
51
  end
52
52
 
53
- def to_ptr
54
- @buffer__GOGYOU__.to_ptr
53
+ def to_address
54
+ @buffer__GOGYOU__.to_address + @offset__GOGYOU__
55
55
  end
56
56
 
57
+ alias to_ptr to_address
58
+
57
59
  #
58
60
  # call-seq:
59
61
  # slide() -> new_accessor or nil
@@ -182,7 +184,8 @@ module Gogyou
182
184
  define_method(:size__GOGYOU__, -> { fieldsize })
183
185
  alias_method(:elementsize, :size__GOGYOU__)
184
186
  alias_method(:size, :size__GOGYOU__)
185
- model.fields.each do |field|
187
+ const_set(:GOGYOU_FIELD_TYPES, types = [])
188
+ model.fields.each_with_index do |field, ifield|
186
189
  name = field.name
187
190
  name = name.intern
188
191
  raise NameError, "already exist field name - #{name}" if namecheck[name]
@@ -190,24 +193,26 @@ module Gogyou
190
193
 
191
194
  if field.vector
192
195
  subarray = define_subarray(field)
193
- type = subarray
196
+ types << subarray
194
197
  else
195
198
  subarray = nil
196
- type = field.type
199
+ types << field.type
197
200
  end
198
201
 
199
- define_method(field.name, -> {
200
- v = type.aref(@buffer__GOGYOU__, @offset__GOGYOU__ + field.offset)
201
- v.infect_from(self, buffer) unless v.frozen?
202
- v.freeze if frozen? || buffer.frozen? || field.const?
203
- v
204
- })
205
-
206
- define_method("#{field.name}=", ->(value) {
207
- raise TypeError, "immutable object (#<%s:0x%08X>.%s)" % [self.class, __id__, field.name], caller(2) if frozen?
208
- raise TypeError, "immutable field (#<%s:0x%08X>.%s)" % [self.class, __id__, field.name], caller(2) if field.const?
209
- type.aset(@buffer__GOGYOU__, @offset__GOGYOU__ + field.offset, value)
210
- })
202
+ class_eval <<-EOS, __FILE__, __LINE__ + 1
203
+ def #{field.name}
204
+ v = GOGYOU_FIELD_TYPES[#{ifield}].aref(@buffer__GOGYOU__, @offset__GOGYOU__ + #{field.offset})
205
+ v.taint if !v.frozen? && (tainted? || @buffer__GOGYOU__.tainted?)
206
+ v.freeze if #{field.const?} || frozen? || @buffer__GOGYOU__.frozen?
207
+ v
208
+ end
209
+
210
+ def #{field.name}=(value)
211
+ raise TypeError, "immutable object (#<%s:0x%08X>)" % [self.class, __id__], caller(1) if frozen?
212
+ raise TypeError, "immutable field (#<%s:0x%08X>.%s)" % [self.class, __id__, #{field.name.inspect}], caller(1) if #{field.const?}
213
+ GOGYOU_FIELD_TYPES[#{ifield}].aset(@buffer__GOGYOU__, @offset__GOGYOU__ + #{field.offset}, value)
214
+ end
215
+ EOS
211
216
  end
212
217
  end
213
218
  end
@@ -270,6 +275,8 @@ module Gogyou
270
275
  end
271
276
 
272
277
  class Array < BasicArray
278
+ include Enumerable
279
+
273
280
  def self.elements
274
281
  self::ELEMENTS
275
282
  end
@@ -300,32 +307,34 @@ module Gogyou
300
307
  bytesize = type.bytesize
301
308
 
302
309
  if model.bytesize == 0
303
- define_method(:check_index, ->(index) {
304
- index = index.to_i
305
- unless index >= 0 && index < self.elementsize
306
- raise IndexError, "out of element size (index #{index} for 0 ... #{self.elementsize})", caller(2)
310
+ class_eval <<-EOS, __FILE__, __LINE__ + 1
311
+ def check_index(index)
312
+ index = index.to_i
313
+ unless index >= 0 && index < elementsize
314
+ raise IndexError, "out of element size (index \#{index} for 0 ... \#{elementsize})", caller
315
+ end
316
+ index
317
+ end
318
+
319
+ def <<(value)
320
+ raise TypeError, "immutable object (#<%s:0x%08X>)" % [self.class, __id__], caller if frozen?
321
+ voff = (@buffer__GOGYOU__.bytesize - @offset__GOGYOU__).align_floor(SUBTYPE.bytesize)
322
+ expandsize = @offset__GOGYOU__ + voff + SUBTYPE.bytesize
323
+ @buffer__GOGYOU__.resize(expandsize)
324
+ SUBTYPE.aset(@buffer__GOGYOU__, @offset__GOGYOU__ + voff, value)
325
+ self
326
+ end
327
+
328
+ def elementsize
329
+ (@buffer__GOGYOU__.bytesize - @offset__GOGYOU__).unit_floor(SUBTYPE.bytesize)
307
330
  end
308
- index
309
- })
310
-
311
- define_method(:<<, ->(value) {
312
- raise TypeError, "immutable object (#<%s:0x%08X>)" % [self.class, __id__], caller(2) if frozen?
313
- voff = (@buffer__GOGYOU__.bytesize - @offset__GOGYOU__).align_floor(type.bytesize)
314
- expandsize = @offset__GOGYOU__ + voff + type.bytesize
315
- @buffer__GOGYOU__.resize(expandsize)
316
- type.aset(@buffer__GOGYOU__, @offset__GOGYOU__ + voff, value)
317
- self
318
- })
319
-
320
- define_method(:elementsize, -> {
321
- (@buffer__GOGYOU__.bytesize - @offset__GOGYOU__).unit_floor(type.bytesize)
322
- })
323
-
324
- alias_method(:size, :elementsize)
325
-
326
- define_method(:bytesize, -> {
327
- (@buffer__GOGYOU__.bytesize - @offset__GOGYOU__).align_floor(type.bytesize)
328
- })
331
+
332
+ alias size elementsize
333
+
334
+ def bytesize
335
+ (@buffer__GOGYOU__.bytesize - @offset__GOGYOU__).align_floor(SUBTYPE.bytesize)
336
+ end
337
+ EOS
329
338
  else
330
339
  eval <<-EOS
331
340
  def check_index(index)
@@ -348,21 +357,23 @@ module Gogyou
348
357
  EOS
349
358
  end
350
359
 
351
- define_method(:to_s, -> {
352
- @buffer__GOGYOU__.byteslice(@offset__GOGYOU__, self.bytesize)
353
- })
354
-
355
- define_method(:[], ->(index) {
356
- v = type.aref(@buffer__GOGYOU__, @offset__GOGYOU__ + check_index(index) * bytesize)
357
- v.infect_from(self, buffer) unless v.frozen?
358
- v.freeze if frozen? || buffer.frozen? || field.const?
359
- v
360
- })
361
-
362
- define_method(:[]=, ->(index, value) {
363
- raise TypeError, "immutable object (#<%s:0x%08X>)" % [self.class, __id__, index], caller(2) if frozen? || field.const?
364
- type.aset(@buffer__GOGYOU__, @offset__GOGYOU__ + check_index(index) * bytesize, value)
365
- })
360
+ class_eval <<-EOS, __FILE__, __LINE__ + 1
361
+ def to_s
362
+ @buffer__GOGYOU__.byteslice(@offset__GOGYOU__, bytesize)
363
+ end
364
+
365
+ def [](index)
366
+ v = SUBTYPE.aref(@buffer__GOGYOU__, @offset__GOGYOU__ + check_index(index) * #{bytesize})
367
+ v.infect_from(self, @buffer__GOGYOU__) unless v.frozen?
368
+ v.freeze if #{field.const?} || frozen? || @buffer__GOGYOU__.frozen?
369
+ v
370
+ end
371
+
372
+ def []=(index, value)
373
+ raise TypeError, "immutable object (#<%s:0x%08X>)" % [self.class, __id__, index], caller if #{field.const?} || frozen?
374
+ SUBTYPE.aset(@buffer__GOGYOU__, @offset__GOGYOU__ + check_index(index) * #{bytesize}, value)
375
+ end
376
+ EOS
366
377
  end
367
378
  klass
368
379
  end
@@ -388,6 +399,18 @@ module Gogyou
388
399
  self.class::BYTESIZE * @buffer__GOGYOU__.bytesize.unit_floor(self.class::SUBTYPE)
389
400
  end
390
401
 
402
+ def each
403
+ return to_enum unless block_given?
404
+ elementsize.times { |i| yield self[i] }
405
+ self
406
+ end
407
+
408
+ def each_with_index
409
+ return to_enum(:each_with_index) unless block_given?
410
+ elementsize.times { |i| yield self[i], i }
411
+ self
412
+ end
413
+
391
414
  def inspect
392
415
  text = "["
393
416
  elementsize.times.with_index do |n, i|
@@ -0,0 +1,856 @@
1
+ module Gogyou
2
+ module Extensions
3
+ #
4
+ # .bytesize・.byteslice・.setbinary・.getbyte・.setbyte メソッドを用いて
5
+ # 整数値・実数値を埋め込む・取り出すメソッドを定義します。
6
+ #
7
+ # gogyou は ruby の String や Fiddle::Pointer、FFI::AbstractPointer
8
+ # クラスにこのモジュールを include しています。
9
+ #
10
+ module ByteArray
11
+ def swapbyte(index, bytesize)
12
+ i = index.to_i
13
+ j = i + bytesize.to_i - 1
14
+ while i < j
15
+ w = getbyte(i)
16
+ setbyte(i, getbyte(j))
17
+ setbyte(j, w)
18
+ i += 1
19
+ j -= 1
20
+ end
21
+
22
+ self
23
+ end
24
+
25
+ def storebe(index, num, bytesize)
26
+ raise RangeError unless index >= 0 &&
27
+ index < bytesize &&
28
+ index + bytesize <= bytesize() &&
29
+ index + bytesize >= 0
30
+ while bytesize > 0
31
+ bytesize -= 1
32
+ setbyte(index, 0xff & (num >> (bytesize * BITS_PER_BYTE)))
33
+ index += 1
34
+ end
35
+
36
+ self
37
+ end
38
+
39
+ def storele(index, num, bytesize)
40
+ raise RangeError unless index >= 0 &&
41
+ index < bytesize() &&
42
+ index + bytesize <= bytesize() &&
43
+ index + bytesize >= 0
44
+ while bytesize > 0
45
+ bytesize -= 1
46
+ setbyte(index, 0xff & num)
47
+ num >>= BITS_PER_BYTE
48
+ index += 1
49
+ end
50
+
51
+ self
52
+ end
53
+
54
+ def loadube(index, bytesize)
55
+ n = 0
56
+ while bytesize > 0
57
+ bytesize -= 1
58
+ n <<= BITS_PER_BYTE
59
+ n |= getbyte(index)
60
+ index += 1
61
+ end
62
+ n
63
+ end
64
+
65
+ def loadibe(index, bytesize)
66
+ loadube(index, bytesize).extendsign(bytesize * BITS_PER_BYTE)
67
+ end
68
+
69
+ def loadule(index, bytesize)
70
+ n = 0
71
+ c = 0
72
+ while bytesize > 0
73
+ bytesize -= 1
74
+ n |= getbyte(index) << (BITS_PER_BYTE * c)
75
+ index += 1
76
+ c += 1
77
+ end
78
+ n
79
+ end
80
+
81
+ def loadile(index, bytesize)
82
+ loadule(index, bytesize).extendsign(bytesize * BITS_PER_BYTE)
83
+ end
84
+
85
+ def store8(index, num)
86
+ setbyte(index.to_i, 0xff & num.to_i)
87
+ end
88
+
89
+ def loadu8(index)
90
+ getbyte(index)
91
+ end
92
+
93
+ def loadi8(index)
94
+ getbyte(index).extendsign_char
95
+ end
96
+
97
+ def store16(index, num)
98
+ setbinary(index, [num].pack("S"))
99
+ end
100
+
101
+ def loadi16(index)
102
+ byteslice(index, 2).unpack("s")[0]
103
+ end
104
+
105
+ def loadu16(index)
106
+ byteslice(index, 2).unpack("S")[0]
107
+ end
108
+
109
+ def store16swap(index, num)
110
+ setbinary(index, [num.swap16].pack("S"))
111
+ end
112
+
113
+ def loadi16swap(index)
114
+ byteslice(index, 2).unpack("s")[0].swap16s
115
+ end
116
+
117
+ def loadu16swap(index)
118
+ byteslice(index, 2).unpack("S")[0].swap16
119
+ end
120
+
121
+ def store16be(index, num)
122
+ index = index.to_i
123
+ num = 0xffff & num.to_i
124
+ setbyte(index , num >> 8)
125
+ setbyte(index + 1, num )
126
+ end
127
+
128
+ def loadu16be(index)
129
+ (getbyte(index) << 8) |
130
+ (getbyte(index + 1))
131
+ end
132
+
133
+ def loadi16be(index)
134
+ (getbyte(index).extendsign_char << 8) |
135
+ (getbyte(index + 1))
136
+ end
137
+
138
+ def store16le(index, num)
139
+ index = index.to_i
140
+ num = 0xffff & num.to_i
141
+ setbyte(index , num )
142
+ setbyte(index + 1, num >> 8)
143
+ end
144
+
145
+ def loadu16le(index)
146
+ (getbyte(index)) |
147
+ (getbyte(index + 1) << 8)
148
+ end
149
+
150
+ def loadi16le(index)
151
+ (getbyte(index)) |
152
+ (getbyte(index + 1).extendsign_char << 8)
153
+ end
154
+
155
+ def store24be(index, num)
156
+ index = index.to_i
157
+ num = 0xffffff & num.to_i
158
+ setbyte(index , num >> 16)
159
+ setbyte(index + 1, num >> 8)
160
+ setbyte(index + 2, num )
161
+ end
162
+
163
+ def loadu24be(index)
164
+ (getbyte(index) << 16) |
165
+ (getbyte(index + 1) << 8) |
166
+ (getbyte(index + 2))
167
+ end
168
+
169
+ def loadi24be(index)
170
+ (getbyte(index).extendsign_char << 16) |
171
+ (getbyte(index + 1) << 8) |
172
+ (getbyte(index + 2))
173
+ end
174
+
175
+ def store24le(index, num)
176
+ index = index.to_i
177
+ num = 0xffffff & num.to_i
178
+ setbyte(index , num )
179
+ setbyte(index + 1, num >> 8)
180
+ setbyte(index + 2, num >> 16)
181
+ end
182
+
183
+ def loadu24le(index)
184
+ (getbyte(index)) |
185
+ (getbyte(index + 1) << 8) |
186
+ (getbyte(index + 2) << 16)
187
+ end
188
+
189
+ def loadi24le(index)
190
+ (getbyte(index)) |
191
+ (getbyte(index + 1) << 8) |
192
+ (getbyte(index + 2).extendsign_char << 16)
193
+ end
194
+
195
+ def store32(index, num)
196
+ setbinary(index, [num].pack("L"))
197
+ end
198
+
199
+ def loadi32(index)
200
+ byteslice(index, 4).unpack("l")[0]
201
+ end
202
+
203
+ def loadu32(index)
204
+ byteslice(index, 4).unpack("L")[0]
205
+ end
206
+
207
+ def store32swap(index, num)
208
+ setbinary(index, [num.swap32].pack("L"))
209
+ end
210
+
211
+ def loadi32swap(index)
212
+ byteslice(index, 4).unpack("l")[0].swap32s
213
+ end
214
+
215
+ def loadu32swap(index)
216
+ byteslice(index, 4).unpack("L")[0].swap32
217
+ end
218
+
219
+ def store32be(index, num)
220
+ index = index.to_i
221
+ num = 0xffffffff & num.to_i
222
+ setbyte(index , num >> 24)
223
+ setbyte(index + 1, num >> 16)
224
+ setbyte(index + 2, num >> 8)
225
+ setbyte(index + 3, num & 0xff)
226
+ end
227
+
228
+ def loadu32be(index)
229
+ (getbyte(index) << 24) |
230
+ (getbyte(index + 1) << 16) |
231
+ (getbyte(index + 2) << 8) |
232
+ (getbyte(index + 3))
233
+ end
234
+
235
+ def loadi32be(index)
236
+ (getbyte(index).extendsign_char << 24) |
237
+ (getbyte(index + 1) << 16) |
238
+ (getbyte(index + 2) << 8) |
239
+ (getbyte(index + 3))
240
+ end
241
+
242
+ def store32le(index, num)
243
+ index = index.to_i
244
+ num = 0xffffffff & num.to_i
245
+ setbyte(index , num & 0xff)
246
+ setbyte(index + 1, num >> 8)
247
+ setbyte(index + 2, num >> 16)
248
+ setbyte(index + 3, num >> 24)
249
+ end
250
+
251
+ def loadu32le(index)
252
+ (getbyte(index)) |
253
+ (getbyte(index + 1) << 8) |
254
+ (getbyte(index + 2) << 16) |
255
+ (getbyte(index + 3) << 24)
256
+ end
257
+
258
+ def loadi32le(index)
259
+ (getbyte(index)) |
260
+ (getbyte(index + 1) << 8) |
261
+ (getbyte(index + 2) << 16) |
262
+ (getbyte(index + 3).extendsign_char << 24)
263
+ end
264
+
265
+ def store48be(index, num)
266
+ num = num.to_i
267
+ store24be(index , num >> 24)
268
+ store24be(index + 3, num )
269
+ end
270
+
271
+ def loadu48be(index)
272
+ (loadu24be(index) << 24) | loadu24be(index + 3)
273
+ end
274
+
275
+ def loadi48be(index)
276
+ (loadi24be(index) << 24) | loadu24be(index + 3)
277
+ end
278
+
279
+ def store48le(index, num)
280
+ num = num.to_i
281
+ store24le(index , num )
282
+ store24le(index + 3, num >> 24)
283
+ end
284
+
285
+ def loadu48le(index)
286
+ loadu24le(index) | (loadu24le(index + 3) << 24)
287
+ end
288
+
289
+ def loadi48le(index)
290
+ loadu24le(index) | (loadi24le(index + 3) << 24)
291
+ end
292
+
293
+ def store64(index, num)
294
+ setbinary(index, [num].pack("Q"))
295
+ end
296
+
297
+ def loadi64(index)
298
+ byteslice(index, 8).unpack("q")[0]
299
+ end
300
+
301
+ def loadu64(index)
302
+ byteslice(index, 8).unpack("Q")[0]
303
+ end
304
+
305
+ def store64swap(index, num)
306
+ setbinary(index, [num.swap64].pack("Q"))
307
+ end
308
+
309
+ def loadi64swap(index)
310
+ byteslice(index, 8).unpack("q")[0].swap64s
311
+ end
312
+
313
+ def loadu64swap(index)
314
+ byteslice(index, 8).unpack("Q")[0].swap64
315
+ end
316
+
317
+ def store64be(index, num)
318
+ num = num.to_i
319
+ store32be(index , num >> 32)
320
+ store32be(index + 4, num )
321
+ end
322
+
323
+ def loadu64be(index)
324
+ (loadu32be(index) << 32) | loadu32be(index + 4)
325
+ end
326
+
327
+ def loadi64be(index)
328
+ (loadi32be(index) << 32) | loadu32be(index + 4)
329
+ end
330
+
331
+ def store64le(index, num)
332
+ num = num.to_i
333
+ store32le(index , num )
334
+ store32le(index + 4, num >> 32)
335
+ end
336
+
337
+ def loadu64le(index)
338
+ loadu32le(index) | (loadu32le(index + 4) << 32)
339
+ end
340
+
341
+ def loadi64le(index)
342
+ loadu32le(index) | (loadi32le(index + 4) << 32)
343
+ end
344
+
345
+ def storef16(index, num)
346
+ store16(index, num.pack_binary16)
347
+ end
348
+
349
+ def loadf16(index)
350
+ loadu16(index).unpack_binary16
351
+ end
352
+
353
+ def storef16swap(index, num)
354
+ store16swap(index, num.pack_binary16)
355
+ end
356
+
357
+ def loadf16swap(index)
358
+ loadu16swap(index).unpack_binary16
359
+ end
360
+
361
+ def storef16be(index, num)
362
+ store16be(index, num.pack_binary16)
363
+ end
364
+
365
+ def loadf16be(index)
366
+ loadu16be(index).unpack_binary16
367
+ end
368
+
369
+ def storef16le(index, num)
370
+ store16le(index, num.pack_binary16)
371
+ end
372
+
373
+ def loadf16le(index)
374
+ loadu16le(index).unpack_binary16
375
+ end
376
+
377
+ def storef32(index, num)
378
+ store32(index, num.pack_binary32)
379
+ end
380
+
381
+ def loadf32(index)
382
+ loadu32(index).unpack_binary32
383
+ end
384
+
385
+ def storef32swap(index, num)
386
+ store32swap(index, num.pack_binary32)
387
+ end
388
+
389
+ def loadf32swap(index)
390
+ loadu32swap(index).unpack_binary32
391
+ end
392
+
393
+ def storef32be(index, num)
394
+ store32be(index, num.pack_binary32)
395
+ end
396
+
397
+ def loadf32be(index)
398
+ loadu32be(index).unpack_binary32
399
+ end
400
+
401
+ def storef32le(index, num)
402
+ store32le(index, num.pack_binary32)
403
+ end
404
+
405
+ def loadf32le(index)
406
+ loadu32le(index).unpack_binary32
407
+ end
408
+
409
+ def storef64(index, num)
410
+ store64(index, num.pack_binary64)
411
+ end
412
+
413
+ def loadf64(index)
414
+ loadu64(index).unpack_binary64
415
+ end
416
+
417
+ def storef64swap(index, num)
418
+ store64swap(index, num.pack_binary64)
419
+ end
420
+
421
+ def loadf64swap(index)
422
+ loadu64swap(index).unpack_binary64
423
+ end
424
+
425
+ def storef64be(index, num)
426
+ store64be(index, num.pack_binary64)
427
+ end
428
+
429
+ def loadf64be(index)
430
+ loadu64be(index).unpack_binary64
431
+ end
432
+
433
+ def storef64le(index, num)
434
+ store64le(index, num.pack_binary64)
435
+ end
436
+
437
+ def loadf64le(index)
438
+ loadu64le(index).unpack_binary64
439
+ end
440
+
441
+ def store16q8(index, num)
442
+ store16(index, num * 256)
443
+ end
444
+
445
+ def loadi16q8(index)
446
+ loadi16(index) / 256.0
447
+ end
448
+
449
+ def loadu16q8(index)
450
+ loadu16(index) / 256.0
451
+ end
452
+
453
+ def store16q8swap(index, num)
454
+ store16swap(index, num * 256)
455
+ end
456
+
457
+ def loadi16q8swap(index)
458
+ loadi16swap(index) / 256.0
459
+ end
460
+
461
+ def loadu16q8swap(index)
462
+ loadu16swap(index) / 256.0
463
+ end
464
+
465
+ def store16q8be(index, num)
466
+ store16be(index, num * 256)
467
+ end
468
+
469
+ def loadi16q8be(index)
470
+ loadi16be(index) / 256.0
471
+ end
472
+
473
+ def loadu16q8be(index)
474
+ loadu16be(index) / 256.0
475
+ end
476
+
477
+ def store16q8le(index, num)
478
+ store16le(index, num * 256)
479
+ end
480
+
481
+ def loadi16q8le(index)
482
+ loadi16le(index) / 256.0
483
+ end
484
+
485
+ def loadu16q8le(index)
486
+ loadu16le(index) / 256.0
487
+ end
488
+
489
+ def store32q6(index, num)
490
+ store32(index, num * 64)
491
+ end
492
+
493
+ def loadi32q6(index)
494
+ loadi32(index) / 64.0
495
+ end
496
+
497
+ def loadu32q6(index)
498
+ loadu32(index) / 64.0
499
+ end
500
+
501
+ def store32q6swap(index, num)
502
+ store32swap(index, num * 64)
503
+ end
504
+
505
+ def loadi32q6swap(index)
506
+ loadi32swap(index) / 64.0
507
+ end
508
+
509
+ def loadu32q6swap(index)
510
+ loadu32swap(index) / 64.0
511
+ end
512
+
513
+ def store32q6be(index, num)
514
+ store32be(index, num * 64)
515
+ end
516
+
517
+ def loadi32q6be(index)
518
+ loadi32be(index) / 64.0
519
+ end
520
+
521
+ def loadu32q6be(index)
522
+ loadu32be(index) / 64.0
523
+ end
524
+
525
+ def store32q6le(index, num)
526
+ store32le(index, num * 64)
527
+ end
528
+
529
+ def loadi32q6le(index)
530
+ loadi32le(index) / 64.0
531
+ end
532
+
533
+ def loadu32q6le(index)
534
+ loadu32le(index) / 64.0
535
+ end
536
+
537
+ def store32q8(index, num)
538
+ store32(index, num * 256)
539
+ end
540
+
541
+ def loadi32q8(index)
542
+ loadi32(index) / 256.0
543
+ end
544
+
545
+ def loadu32q8(index)
546
+ loadu32(index) / 256.0
547
+ end
548
+
549
+ def store32q8swap(index, num)
550
+ store32swap(index, num * 256)
551
+ end
552
+
553
+ def loadi32q8swap(index)
554
+ loadi32swap(index) / 256.0
555
+ end
556
+
557
+ def loadu32q8swap(index)
558
+ loadu32swap(index) / 256.0
559
+ end
560
+
561
+ def store32q8be(index, num)
562
+ store32be(index, num * 256)
563
+ end
564
+
565
+ def loadi32q8be(index)
566
+ loadi32be(index) / 256.0
567
+ end
568
+
569
+ def loadu32q8be(index)
570
+ loadu32be(index) / 256.0
571
+ end
572
+
573
+ def store32q8le(index, num)
574
+ store32le(index, num * 256)
575
+ end
576
+
577
+ def loadi32q8le(index)
578
+ loadi32le(index) / 256.0
579
+ end
580
+
581
+ def loadu32q8le(index)
582
+ loadu32le(index) / 256.0
583
+ end
584
+
585
+ def store32q12(index, num)
586
+ store32(index, num * 4096)
587
+ end
588
+
589
+ def loadi32q12(index)
590
+ loadi32(index) / 4096.0
591
+ end
592
+
593
+ def loadu32q12(index)
594
+ loadu32(index) / 4096.0
595
+ end
596
+
597
+ def store32q12swap(index, num)
598
+ store32swap(index, num * 4096)
599
+ end
600
+
601
+ def loadi32q12swap(index)
602
+ loadi32swap(index) / 4096.0
603
+ end
604
+
605
+ def loadu32q12swap(index)
606
+ loadu32swap(index) / 4096.0
607
+ end
608
+
609
+ def store32q12be(index, num)
610
+ store32be(index, num * 4096)
611
+ end
612
+
613
+ def loadi32q12be(index)
614
+ loadi32be(index) / 4096.0
615
+ end
616
+
617
+ def loadu32q12be(index)
618
+ loadu32be(index) / 4096.0
619
+ end
620
+
621
+ def store32q12le(index, num)
622
+ store32le(index, num * 4096)
623
+ end
624
+
625
+ def loadi32q12le(index)
626
+ loadi32le(index) / 4096.0
627
+ end
628
+
629
+ def loadu32q12le(index)
630
+ loadu32le(index) / 4096.0
631
+ end
632
+
633
+ def store32q16(index, num)
634
+ store32(index, num * 65536)
635
+ end
636
+
637
+ def loadi32q16(index)
638
+ loadi32(index) / 65536.0
639
+ end
640
+
641
+ def loadu32q16(index)
642
+ loadu32(index) / 65536.0
643
+ end
644
+
645
+ def store32q16swap(index, num)
646
+ store32swap(index, num * 65536)
647
+ end
648
+
649
+ def loadi32q16swap(index)
650
+ loadi32swap(index) / 65536.0
651
+ end
652
+
653
+ def loadu32q16swap(index)
654
+ loadu32swap(index) / 65536.0
655
+ end
656
+
657
+ def store32q16be(index, num)
658
+ store32be(index, num * 65536)
659
+ end
660
+
661
+ def loadi32q16be(index)
662
+ loadi32be(index) / 65536.0
663
+ end
664
+
665
+ def loadu32q16be(index)
666
+ loadu32be(index) / 65536.0
667
+ end
668
+
669
+ def store32q16le(index, num)
670
+ store32le(index, num * 65536)
671
+ end
672
+
673
+ def loadi32q16le(index)
674
+ loadi32le(index) / 65536.0
675
+ end
676
+
677
+ def loadu32q16le(index)
678
+ loadu32le(index) / 65536.0
679
+ end
680
+
681
+ def store32q24(index, num)
682
+ store32(index, num * 16777216)
683
+ end
684
+
685
+ def loadi32q24(index)
686
+ loadi32(index) / 16777216.0
687
+ end
688
+
689
+ def loadu32q24(index)
690
+ loadu32(index) / 16777216.0
691
+ end
692
+
693
+ def store32q24swap(index, num)
694
+ store32swap(index, num * 16777216)
695
+ end
696
+
697
+ def loadi32q24swap(index)
698
+ loadi32swap(index) / 16777216.0
699
+ end
700
+
701
+ def loadu32q24swap(index)
702
+ loadu32swap(index) / 16777216.0
703
+ end
704
+
705
+ def store32q24be(index, num)
706
+ store32be(index, num * 16777216)
707
+ end
708
+
709
+ def loadi32q24be(index)
710
+ loadi32be(index) / 16777216.0
711
+ end
712
+
713
+ def loadu32q24be(index)
714
+ loadu32be(index) / 16777216.0
715
+ end
716
+
717
+ def store32q24le(index, num)
718
+ store32le(index, num * 16777216)
719
+ end
720
+
721
+ def loadi32q24le(index)
722
+ loadi32le(index) / 16777216.0
723
+ end
724
+
725
+ def loadu32q24le(index)
726
+ loadu32le(index) / 16777216.0
727
+ end
728
+
729
+
730
+ def store_char(index, num)
731
+ setbinary(index, [num].pack("S!"))
732
+ end
733
+
734
+ def load_char(index)
735
+ byteslice(index, TypeSpec::SIZEOF_CHAR).unpack("c!")[0]
736
+ end
737
+
738
+ def load_uchar(index)
739
+ byteslice(index, TypeSpec::SIZEOF_CHAR).unpack("C!")[0]
740
+ end
741
+
742
+ def store_short(index, num)
743
+ setbinary(index, [num].pack("S!"))
744
+ end
745
+
746
+ def load_short(index)
747
+ byteslice(index, TypeSpec::SIZEOF_SHORT).unpack("s!")[0]
748
+ end
749
+
750
+ def load_ushort(index)
751
+ byteslice(index, TypeSpec::SIZEOF_SHORT).unpack("S!")[0]
752
+ end
753
+
754
+ def store_int(index, num)
755
+ setbinary(index, [num].pack("I!"))
756
+ end
757
+
758
+ def load_int(index)
759
+ byteslice(index, TypeSpec::SIZEOF_INT).unpack("i!")[0]
760
+ end
761
+
762
+ def load_uint(index)
763
+ byteslice(index, TypeSpec::SIZEOF_INT).unpack("I!")[0]
764
+ end
765
+
766
+ def store_long(index, num)
767
+ setbinary(index, [num].pack("L!"))
768
+ end
769
+
770
+ def load_long(index)
771
+ byteslice(index, TypeSpec::SIZEOF_LONG).unpack("l!")[0]
772
+ end
773
+
774
+ def load_ulong(index)
775
+ byteslice(index, TypeSpec::SIZEOF_LONG).unpack("L!")[0]
776
+ end
777
+
778
+ def store_longlong(index, num)
779
+ setbinary(index, [num].pack("Q"))
780
+ end
781
+
782
+ def load_longlong(index)
783
+ byteslice(index, TypeSpec::SIZEOF_LONGLONG).unpack("q")[0]
784
+ end
785
+
786
+ def load_ulonglong(index)
787
+ byteslice(index, TypeSpec::SIZEOF_LONGLONG).unpack("Q")[0]
788
+ end
789
+
790
+ def store_float(index, num)
791
+ setbinary(index, [num].pack("f"))
792
+ end
793
+
794
+ def load_float(index)
795
+ byteslice(index, TypeSpec::SIZEOF_FLOAT).unpack("f")[0]
796
+ end
797
+
798
+ def store_double(index, num)
799
+ setbinary(index, [num].pack("d"))
800
+ end
801
+
802
+ def load_double(index)
803
+ byteslice(index, TypeSpec::SIZEOF_DOUBLE).unpack("d")[0]
804
+ end
805
+
806
+
807
+ case "\0\1\2\3".unpack("I")[0]
808
+ when 0x00010203 # big endian (network byte order)
809
+ alias store storebe
810
+ alias loadi loadibe
811
+ alias loadu loadube
812
+
813
+ alias storeswap storele
814
+ alias loadiswap loadile
815
+ alias loaduswap loadule
816
+ when 0x03020100 # little endian (vax byte order)
817
+ alias store storele
818
+ alias loadi loadile
819
+ alias loadu loadule
820
+
821
+ alias storeswap storebe
822
+ alias loadiswap loadibe
823
+ alias loaduswap loadube
824
+ else
825
+ # any byte order...
826
+ end
827
+
828
+ case TypeSpec::SIZEOF_SIZE_T
829
+ when TypeSpec::SIZEOF_LONGLONG
830
+ alias store_sizet store_longlong
831
+ alias load_sizet load_ulonglong
832
+ alias load_ssizet load_longlong
833
+ when TypeSpec::SIZEOF_LONG
834
+ alias store_sizet store_long
835
+ alias load_sizet load_ulong
836
+ alias load_ssizet load_long
837
+ when TypeSpec::SIZEOF_INT
838
+ alias store_sizet store_int
839
+ alias load_sizet load_uint
840
+ alias load_ssizet load_int
841
+ else
842
+ def store_sizet(o, v)
843
+ raise NotImplementedError, "unsupported system (expected sizeof(void *) to 4 or 8, but #{TypeSpec::SIZEOF_SIZE_T})"
844
+ end
845
+
846
+ def load_ssizet(o)
847
+ raise NotImplementedError, "unsupported system (expected sizeof(void *) to 4 or 8, but #{TypeSpec::SIZEOF_SIZE_T})"
848
+ end
849
+
850
+ def load_sizet(o)
851
+ raise NotImplementedError, "unsupported system (expected sizeof(void *) to 4 or 8, but #{TypeSpec::SIZEOF_SIZE_T})"
852
+ end
853
+ end
854
+ end
855
+ end
856
+ end