|
1 | 1 | using System; |
| 2 | +using System.Runtime.CompilerServices; |
2 | 3 |
|
3 | 4 | namespace Tensorflow |
4 | 5 | { |
5 | 6 | public partial class Tensor |
6 | 7 | { |
7 | 8 | public static explicit operator bool(Tensor tensor) |
8 | 9 | { |
9 | | - EnsureScalar(tensor); |
10 | | - return tensor.Data<bool>()[0]; |
| 10 | + unsafe |
| 11 | + { |
| 12 | + EnsureScalar(tensor); |
| 13 | + return *(bool*) tensor.buffer; |
| 14 | + } |
11 | 15 | } |
12 | 16 |
|
13 | 17 | public static explicit operator sbyte(Tensor tensor) |
14 | 18 | { |
15 | | - EnsureScalar(tensor); |
16 | | - return tensor.Data<sbyte>()[0]; |
| 19 | + unsafe |
| 20 | + { |
| 21 | + EnsureScalar(tensor); |
| 22 | + return *(sbyte*) tensor.buffer; |
| 23 | + } |
17 | 24 | } |
18 | 25 |
|
19 | 26 | public static explicit operator byte(Tensor tensor) |
20 | 27 | { |
21 | | - EnsureScalar(tensor); |
22 | | - return tensor.Data<byte>()[0]; |
| 28 | + unsafe |
| 29 | + { |
| 30 | + EnsureScalar(tensor); |
| 31 | + return *(byte*) tensor.buffer; |
| 32 | + } |
23 | 33 | } |
24 | 34 |
|
25 | 35 | public static explicit operator ushort(Tensor tensor) |
26 | 36 | { |
27 | | - EnsureScalar(tensor); |
28 | | - return tensor.Data<ushort>()[0]; |
| 37 | + unsafe |
| 38 | + { |
| 39 | + EnsureScalar(tensor); |
| 40 | + return *(ushort*) tensor.buffer; |
| 41 | + } |
29 | 42 | } |
30 | 43 |
|
31 | 44 | public static explicit operator short(Tensor tensor) |
32 | 45 | { |
33 | | - EnsureScalar(tensor); |
34 | | - return tensor.Data<short>()[0]; |
| 46 | + unsafe |
| 47 | + { |
| 48 | + EnsureScalar(tensor); |
| 49 | + return *(short*) tensor.buffer; |
| 50 | + } |
35 | 51 | } |
36 | 52 |
|
37 | 53 | public static explicit operator int(Tensor tensor) |
38 | 54 | { |
39 | | - EnsureScalar(tensor); |
40 | | - return tensor.Data<int>()[0]; |
| 55 | + unsafe |
| 56 | + { |
| 57 | + EnsureScalar(tensor); |
| 58 | + return *(int*) tensor.buffer; |
| 59 | + } |
41 | 60 | } |
42 | 61 |
|
43 | 62 | public static explicit operator uint(Tensor tensor) |
44 | 63 | { |
45 | | - EnsureScalar(tensor); |
46 | | - return tensor.Data<uint>()[0]; |
| 64 | + unsafe |
| 65 | + { |
| 66 | + EnsureScalar(tensor); |
| 67 | + return *(uint*) tensor.buffer; |
| 68 | + } |
47 | 69 | } |
48 | 70 |
|
49 | 71 | public static explicit operator long(Tensor tensor) |
50 | 72 | { |
51 | | - EnsureScalar(tensor); |
52 | | - return tensor.Data<long>()[0]; |
| 73 | + unsafe |
| 74 | + { |
| 75 | + EnsureScalar(tensor); |
| 76 | + return *(long*) tensor.buffer; |
| 77 | + } |
53 | 78 | } |
54 | 79 |
|
55 | 80 | public static explicit operator ulong(Tensor tensor) |
56 | 81 | { |
57 | | - EnsureScalar(tensor); |
58 | | - return tensor.Data<ulong>()[0]; |
| 82 | + unsafe |
| 83 | + { |
| 84 | + EnsureScalar(tensor); |
| 85 | + return *(ulong*) tensor.buffer; |
| 86 | + } |
59 | 87 | } |
60 | 88 |
|
61 | 89 | public static explicit operator float(Tensor tensor) |
62 | 90 | { |
63 | | - EnsureScalar(tensor); |
64 | | - return tensor.Data<float>()[0]; |
| 91 | + unsafe |
| 92 | + { |
| 93 | + EnsureScalar(tensor); |
| 94 | + return *(float*) tensor.buffer; |
| 95 | + } |
65 | 96 | } |
66 | 97 |
|
67 | 98 | public static explicit operator double(Tensor tensor) |
68 | 99 | { |
69 | | - EnsureScalar(tensor); |
70 | | - return tensor.Data<double>()[0]; |
| 100 | + unsafe |
| 101 | + { |
| 102 | + EnsureScalar(tensor); |
| 103 | + return *(double*) tensor.buffer; |
| 104 | + } |
71 | 105 | } |
72 | 106 |
|
| 107 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
73 | 108 | private static void EnsureScalar(Tensor tensor) |
74 | 109 | { |
75 | 110 | if (tensor == null) |
|
0 commit comments