narray_ffi 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/narray_ffi.rb +60 -0
- data/narray_ffi.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7999260e1d1b305db5f0c534e2f5f1e6323fc3ba
|
4
|
+
data.tar.gz: 6e439735e3139f0df821b53201c9fe4158d2ecce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf8a6c49ba47dda532a414e2b14d94dfe16c5c4a4fb311fb14839496a9b4fbe3f02b3e687420a32f8f4abc9c1e55f307b4dd3d5c5acdae195e14e51e11f6b25f
|
7
|
+
data.tar.gz: 7c0974b9fe5933755a955acb059b7da3e3f3a0cfcf21a9019d8146e5abf7fccef624084a70bc60659ff31e818ad4436b0c367f62d4ec9d500797824dc215cb43
|
data/lib/narray_ffi.rb
CHANGED
@@ -1,3 +1,63 @@
|
|
1
1
|
require "narray"
|
2
2
|
require "ffi"
|
3
3
|
require "narray_ffi_c.so"
|
4
|
+
|
5
|
+
class ANArray < NArray
|
6
|
+
|
7
|
+
FFITYPECODES = {
|
8
|
+
NArray::BYTE => :char,
|
9
|
+
NArray::SINT => :short,
|
10
|
+
NArray::INT => :int,
|
11
|
+
NArray::SFLOAT => :float,
|
12
|
+
NArray::FLOAT => :double,
|
13
|
+
NArray::SCOMPLEX => :float,
|
14
|
+
NArray::COMPLEX => :double
|
15
|
+
}
|
16
|
+
|
17
|
+
def self.new(typecode, alignment, *size)
|
18
|
+
raise "Wrong type code" if not FFITYPECODES[typecode]
|
19
|
+
raise "Invalid alignment" unless alignment > 0 and ( alignment & (alignment - 1) == 0 )
|
20
|
+
total = size[0]
|
21
|
+
size[1..-1].each { |sz| total = total * sz }
|
22
|
+
total = 2*total if typecode == NArray::COMPLEX or typecode == NArray::SCOMPLEX
|
23
|
+
mem = FFI::MemoryPointer::new( FFITYPECODES[typecode], total + alignment - 1 )
|
24
|
+
address = mem.address
|
25
|
+
offset = address & (alignment - 1)
|
26
|
+
offset = alignment - offset unless offset == 0
|
27
|
+
mem = mem.slice(offset, total*mem.type_size)
|
28
|
+
return NArray.to_na(mem, typecode, *size)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.byte(alignment, *size)
|
32
|
+
return self.new(NArray::BYTE, alignment, *size)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.sint(alignment, *size)
|
36
|
+
return self.new(NArray::SINT, alignment, *size)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.int(alignment, *size)
|
40
|
+
return self.new(NArray::INT, alignment, *size)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.sfloat(alignment, *size)
|
44
|
+
return self.new(NArray::SFLOAT, alignment, *size)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.float(alignment, *size)
|
48
|
+
return self.new(NArray::FLOAT, alignment, *size)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.scomplex(alignment, *size)
|
52
|
+
return self.new(NArray::SCOMPLEX, alignment, *size)
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.complex(alignment, *size)
|
56
|
+
return self.new(NArray::COMPLEX, alignment, *size)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.object(alignment, *size)
|
60
|
+
return self.new(NArray::OBJECT, alignment, *size)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/narray_ffi.gemspec
CHANGED