Browse Source

Tensor.Explicit.cs: perf-ops

tags/v0.12
Eli Belash 6 years ago
parent
commit
4ba71e24db
1 changed files with 57 additions and 22 deletions
  1. +57
    -22
      src/TensorFlowNET.Core/Tensors/Tensor.Explicit.cs

+ 57
- 22
src/TensorFlowNET.Core/Tensors/Tensor.Explicit.cs View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Runtime.CompilerServices;


namespace Tensorflow namespace Tensorflow
{ {
@@ -6,70 +7,104 @@ namespace Tensorflow
{ {
public static explicit operator bool(Tensor tensor) public static explicit operator bool(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<bool>()[0];
unsafe
{
EnsureScalar(tensor);
return *(bool*) tensor.buffer;
}
} }


public static explicit operator sbyte(Tensor tensor) public static explicit operator sbyte(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<sbyte>()[0];
unsafe
{
EnsureScalar(tensor);
return *(sbyte*) tensor.buffer;
}
} }


public static explicit operator byte(Tensor tensor) public static explicit operator byte(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<byte>()[0];
unsafe
{
EnsureScalar(tensor);
return *(byte*) tensor.buffer;
}
} }


public static explicit operator ushort(Tensor tensor) public static explicit operator ushort(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<ushort>()[0];
unsafe
{
EnsureScalar(tensor);
return *(ushort*) tensor.buffer;
}
} }


public static explicit operator short(Tensor tensor) public static explicit operator short(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<short>()[0];
unsafe
{
EnsureScalar(tensor);
return *(short*) tensor.buffer;
}
} }


public static explicit operator int(Tensor tensor) public static explicit operator int(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<int>()[0];
unsafe
{
EnsureScalar(tensor);
return *(int*) tensor.buffer;
}
} }


public static explicit operator uint(Tensor tensor) public static explicit operator uint(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<uint>()[0];
unsafe
{
EnsureScalar(tensor);
return *(uint*) tensor.buffer;
}
} }


public static explicit operator long(Tensor tensor) public static explicit operator long(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<long>()[0];
unsafe
{
EnsureScalar(tensor);
return *(long*) tensor.buffer;
}
} }


public static explicit operator ulong(Tensor tensor) public static explicit operator ulong(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<ulong>()[0];
unsafe
{
EnsureScalar(tensor);
return *(ulong*) tensor.buffer;
}
} }


public static explicit operator float(Tensor tensor) public static explicit operator float(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<float>()[0];
unsafe
{
EnsureScalar(tensor);
return *(float*) tensor.buffer;
}
} }


public static explicit operator double(Tensor tensor) public static explicit operator double(Tensor tensor)
{ {
EnsureScalar(tensor);
return tensor.Data<double>()[0];
unsafe
{
EnsureScalar(tensor);
return *(double*) tensor.buffer;
}
} }


[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void EnsureScalar(Tensor tensor) private static void EnsureScalar(Tensor tensor)
{ {
if (tensor == null) if (tensor == null)


Loading…
Cancel
Save