Permalink
Browse files

replace long with ptrdiff_t for memory size/offset etc

  • Loading branch information...
1 parent eb397ad commit d643d5582e688faec4d0ad70fad9fb6ad215e3ce @BTNC BTNC committed Oct 3, 2016
View
@@ -28,8 +28,8 @@ if ok then
-- Allocator
ffi.cdef[[
typedef struct THAllocator {
- void* (*malloc)(void*, long);
- void* (*realloc)(void*, void*, long);
+ void* (*malloc)(void*, ptrdiff_t);
+ void* (*realloc)(void*, void*, ptrdiff_t);
void (*free)(void*, void*);
} THAllocator;
]]
@@ -41,7 +41,7 @@ typedef struct THAllocator {
typedef struct THRealStorage
{
real *data;
- long size;
+ ptrdiff_t size;
int refcount;
char flag;
THAllocator *allocator;
@@ -78,7 +78,7 @@ typedef struct THRealTensor
int nDimension;
THRealStorage *storage;
- long storageOffset;
+ ptrdiff_t storageOffset;
int refcount;
char flag;
View
@@ -39,7 +39,7 @@ IMPLEMENT_TORCH_FILE_FUNC(synchronize)
static int torch_File_seek(lua_State *L)
{
THFile *self = luaT_checkudata(L, 1, "torch.File");
- long position = luaL_checklong(L, 2)-1;
+ ptrdiff_t position = luaL_checkinteger(L, 2)-1;
// >= 0 because it has 1 already subtracted
THArgCheck(position >= 0, 2, "position has to be greater than 0!");
THFile_seek(self, (size_t)position);
@@ -73,8 +73,8 @@ IMPLEMENT_TORCH_FILE_FUNC(close)
{ \
if(lua_isnumber(L, 2)) \
{ \
- long size = lua_tonumber(L, 2); \
- long nread; \
+ ptrdiff_t size = lua_tonumber(L, 2); \
+ ptrdiff_t nread; \
\
TH##TYPEC##Storage *storage = TH##TYPEC##Storage_newWithSize(size); \
luaT_pushudata(L, storage, "torch." #TYPEC "Storage"); \
@@ -134,7 +134,7 @@ static int torch_File_readString(lua_State *L)
THFile *self = luaT_checkudata(L, 1, "torch.File");
const char *format = luaL_checkstring(L, 2);
char *str;
- long size;
+ ptrdiff_t size;
size = THFile_readStringRaw(self, format, &str);
lua_pushlstring(L, str, size);
@@ -151,7 +151,7 @@ static int torch_File_writeString(lua_State *L)
luaL_checktype(L, 2, LUA_TSTRING);
str = lua_tolstring(L, 2, &size);
- lua_pushnumber(L, THFile_writeStringRaw(self, str, (long)size));
+ lua_pushnumber(L, THFile_writeStringRaw(self, str, size));
return 1;
}
View
@@ -4,6 +4,58 @@ require 'torchcwrap'
local interface = wrap.CInterface.new()
local method = wrap.CInterface.new()
+local argtypes = wrap.CInterface.argtypes
+
+argtypes['ptrdiff_t'] = {
+
+ helpname = function(arg)
+ return 'ptrdiff_t'
+ end,
+
+ declare = function(arg)
+ -- if it is a number we initialize here
+ local default = tonumber(tostring(arg.default)) or 0
+ return string.format("%s arg%d = %g;", 'ptrdiff_t', arg.i, default)
+ end,
+
+ check = function(arg, idx)
+ return string.format("lua_isnumber(L, %d)", idx)
+ end,
+
+ read = function(arg, idx)
+ return string.format("arg%d = (%s)lua_tonumber(L, %d);", arg.i, 'ptrdiff_t', idx)
+ end,
+
+ init = function(arg)
+ -- otherwise do it here
+ if arg.default then
+ local default = tostring(arg.default)
+ if not tonumber(default) then
+ return string.format("arg%d = %s;", arg.i, default)
+ end
+ end
+ end,
+
+ carg = function(arg)
+ return string.format('arg%d', arg.i)
+ end,
+
+ creturn = function(arg)
+ return string.format('arg%d', arg.i)
+ end,
+
+ precall = function(arg)
+ if arg.returned then
+ return string.format('lua_pushnumber(L, (lua_Number)arg%d);', arg.i)
+ end
+ end,
+
+ postcall = function(arg)
+ if arg.creturned then
+ return string.format('lua_pushnumber(L, (lua_Number)arg%d);', arg.i)
+ end
+ end
+}
interface:print([[
#include "TH.h"
@@ -533,7 +585,7 @@ for _,Tensor in ipairs({"ByteTensor", "CharTensor",
wrap("numel",
cname("numel"),
{{name=Tensor},
- {name="long", creturned=true}})
+ {name="ptrdiff_t", creturned=true}})
for _,name in ipairs({"cumsum", "cumprod"}) do
wrap(name,
View
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
+#include <stddef.h>
#include "luaT.h"
#include "TH.h"
View
@@ -20,15 +20,15 @@ static int torch_Storage_(new)(lua_State *L)
int isShared = 0;
if(luaT_optboolean(L, index + 1, 0))
isShared = TH_ALLOCATOR_MAPPED_SHARED;
- long size = luaL_optlong(L, index + 2, 0);
+ ptrdiff_t size = luaL_optinteger(L, index + 2, 0);
if (isShared && luaT_optboolean(L, index + 3, 0))
isShared = TH_ALLOCATOR_MAPPED_SHAREDMEM;
storage = THStorage_(newWithMapping)(fileName, size, isShared);
}
else if(lua_type(L, index) == LUA_TTABLE)
{
- long size = lua_objlen(L, index);
- long i;
+ ptrdiff_t size = lua_objlen(L, index);
+ ptrdiff_t i;
if (allocator)
storage = THStorage_(newWithAllocator)(size, allocator, NULL);
else
@@ -52,11 +52,11 @@ static int torch_Storage_(new)(lua_State *L)
THStorage *src = luaT_checkudata(L, index, torch_Storage);
real *ptr = src->data;
- long offset = luaL_optlong(L, index + 1, 1) - 1;
+ ptrdiff_t offset = luaL_optinteger(L, index + 1, 1) - 1;
if (offset < 0 || offset >= src->size) {
luaL_error(L, "offset out of bounds");
}
- long size = luaL_optlong(L, index + 2, src->size - offset);
+ ptrdiff_t size = luaL_optinteger(L, index + 2, src->size - offset);
if (size < 1 || size > (src->size - offset)) {
luaL_error(L, "size out of bounds");
}
@@ -67,8 +67,8 @@ static int torch_Storage_(new)(lua_State *L)
}
else if(lua_type(L, index + 1) == LUA_TNUMBER)
{
- long size = luaL_optlong(L, index, 0);
- real *ptr = (real *)luaL_optlong(L, index + 1, 0);
+ ptrdiff_t size = luaL_optinteger(L, index, 0);
+ real *ptr = (real *)luaL_optinteger(L, index + 1, 0);
if (allocator)
storage = THStorage_(newWithDataAndAllocator)(ptr, size, allocator, NULL);
else
@@ -77,7 +77,7 @@ static int torch_Storage_(new)(lua_State *L)
}
else
{
- long size = luaL_optlong(L, index, 0);
+ ptrdiff_t size = luaL_optinteger(L, index, 0);
if (allocator)
storage = THStorage_(newWithAllocator)(size, allocator, NULL);
else
@@ -104,7 +104,7 @@ static int torch_Storage_(free)(lua_State *L)
static int torch_Storage_(resize)(lua_State *L)
{
THStorage *storage = luaT_checkudata(L, 1, torch_Storage);
- long size = luaL_checklong(L, 2);
+ ptrdiff_t size = luaL_checkinteger(L, 2);
/* int keepContent = luaT_optboolean(L, 3, 0); */
THStorage_(resize)(storage, size);/*, keepContent); */
lua_settop(L, 1);
@@ -148,14 +148,14 @@ static int torch_Storage_(fill)(lua_State *L)
static int torch_Storage_(elementSize)(lua_State *L)
{
- luaT_pushlong(L, THStorage_(elementSize)());
+ luaT_pushinteger(L, THStorage_(elementSize)());
return 1;
}
static int torch_Storage_(__len__)(lua_State *L)
{
THStorage *storage = luaT_checkudata(L, 1, torch_Storage);
- luaT_pushlong(L, storage->size);
+ luaT_pushinteger(L, storage->size);
return 1;
}
@@ -164,7 +164,7 @@ static int torch_Storage_(__newindex__)(lua_State *L)
if(lua_isnumber(L, 2))
{
THStorage *storage = luaT_checkudata(L, 1, torch_Storage);
- long index = luaL_checklong(L, 2) - 1;
+ ptrdiff_t index = luaL_checkinteger(L, 2) - 1;
real number = luaG_(checkreal)(L, 3);
THStorage_(set)(storage, index, number);
lua_pushboolean(L, 1);
@@ -180,7 +180,7 @@ static int torch_Storage_(__index__)(lua_State *L)
if(lua_isnumber(L, 2))
{
THStorage *storage = luaT_checkudata(L, 1, torch_Storage);
- long index = luaL_checklong(L, 2) - 1;
+ ptrdiff_t index = luaL_checkinteger(L, 2) - 1;
luaG_(pushreal)(L, THStorage_(get)(storage, index));
lua_pushboolean(L, 1);
return 2;
@@ -214,7 +214,7 @@ static int torch_Storage_(string)(lua_State *L)
static int torch_Storage_(totable)(lua_State *L)
{
THStorage *storage = luaT_checkudata(L, 1, torch_Storage);
- long i;
+ ptrdiff_t i;
lua_newtable(L);
for(i = 0; i < storage->size; i++)
@@ -237,6 +237,9 @@ static int torch_Storage_(write)(lua_State *L)
THStorage *storage = luaT_checkudata(L, 1, torch_Storage);
THFile *file = luaT_checkudata(L, 2, "torch.File");
+#ifdef DEBUG
+ THAssert(storage->size < LONG_MAX);
+#endif
THFile_writeLongScalar(file, storage->size);
THFile_writeRealRaw(file, storage->data, storage->size);
@@ -247,7 +250,7 @@ static int torch_Storage_(read)(lua_State *L)
{
THStorage *storage = luaT_checkudata(L, 1, torch_Storage);
THFile *file = luaT_checkudata(L, 2, "torch.File");
- long size = THFile_readLongScalar(file);
+ ptrdiff_t size = THFile_readLongScalar(file);
THStorage_(resize)(storage, size);
THFile_readRealRaw(file, storage->data, storage->size);
View
@@ -5,7 +5,7 @@
#include "luaG.h"
static void torch_Tensor_(c_readTensorStorageSizeStride)(lua_State *L, int index, int allowNone, int allowTensor, int allowStorage, int allowStride,
- THStorage **storage_, long *storageOffset_, THLongStorage **size_, THLongStorage **stride_);
+ THStorage **storage_, ptrdiff_t *storageOffset_, THLongStorage **size_, THLongStorage **stride_);
static void torch_Tensor_(c_readSizeStride)(lua_State *L, int index, int allowStride, THLongStorage **size_, THLongStorage **stride_);
@@ -29,7 +29,7 @@ static int torch_Tensor_(size)(lua_State *L)
static int torch_Tensor_(elementSize)(lua_State *L)
{
- luaT_pushlong(L, THStorage_(elementSize)());
+ luaT_pushinteger(L, THStorage_(elementSize)());
return 1;
}
@@ -55,7 +55,7 @@ static int torch_Tensor_(stride)(lua_State *L)
static int torch_Tensor_(nDimension)(lua_State *L)
{
THTensor *tensor = luaT_checkudata(L, 1, torch_Tensor);
- luaT_pushlong(L, tensor->nDimension);
+ luaT_pushinteger(L, tensor->nDimension);
return 1;
}
@@ -76,21 +76,21 @@ static int torch_Tensor_(storage)(lua_State *L)
static int torch_Tensor_(storageOffset)(lua_State *L)
{
THTensor *tensor = luaT_checkudata(L, 1, torch_Tensor);
- luaT_pushlong(L, tensor->storageOffset+1);
+ luaT_pushinteger(L, tensor->storageOffset+1);
return 1;
}
static int torch_Tensor_(new)(lua_State *L)
{
THTensor *tensor;
- long storageOffset;
+ ptrdiff_t storageOffset;
THLongStorage *size, *stride;
if(lua_type(L, 1) == LUA_TTABLE)
{
- long i, j;
+ ptrdiff_t i, j;
THLongStorage *counter;
- long si = 0;
+ ptrdiff_t si = 0;
int dimension = 0;
int is_finished = 0;
@@ -214,7 +214,7 @@ static int torch_Tensor_(set)(lua_State *L)
{
THTensor *self = luaT_checkudata(L, 1, torch_Tensor);
THStorage *storage;
- long storageOffset;
+ ptrdiff_t storageOffset;
THLongStorage *size, *stride;
torch_Tensor_(c_readTensorStorageSizeStride)(L, 2, 1, 1, 1, 1,
@@ -651,7 +651,7 @@ static int torch_Tensor_(isSetTo)(lua_State *L)
static int torch_Tensor_(nElement)(lua_State *L)
{
THTensor *tensor = luaT_checkudata(L, 1, torch_Tensor);
- luaT_pushlong(L, THTensor_(nElement)(tensor));
+ luaT_pushinteger(L, THTensor_(nElement)(tensor));
return 1;
}
@@ -752,7 +752,7 @@ static int torch_Tensor_(__newindex__)(lua_State *L)
}
else if((idx = luaT_toudata(L, 2, "torch.LongStorage")))
{
- long index = THTensor_(storageOffset)(tensor);
+ ptrdiff_t index = THTensor_(storageOffset)(tensor);
real value = luaG_(checkreal)(L,3);
int dim;
@@ -904,7 +904,7 @@ static int torch_Tensor_(__index__)(lua_State *L)
}
else if((idx = luaT_toudata(L, 2, "torch.LongStorage")))
{
- long index = THTensor_(storageOffset)(tensor);
+ ptrdiff_t index = THTensor_(storageOffset)(tensor);
int dim;
THArgCheck(idx->size == tensor->nDimension, 2, "invalid size");
@@ -1071,7 +1071,7 @@ static void torch_Tensor_(c_readSizeStride)(lua_State *L, int index, int allowSt
}
static void torch_Tensor_(c_readTensorStorageSizeStride)(lua_State *L, int index, int allowNone, int allowTensor, int allowStorage, int allowStride,
- THStorage **storage_, long *storageOffset_, THLongStorage **size_, THLongStorage **stride_)
+ THStorage **storage_, ptrdiff_t *storageOffset_, THLongStorage **size_, THLongStorage **stride_)
{
THTensor *src = NULL;
THStorage *storage = NULL;
@@ -1105,7 +1105,7 @@ static void torch_Tensor_(c_readTensorStorageSizeStride)(lua_State *L, int index
}
else
{
- *storageOffset_ = luaL_checklong(L, index+1)-1;
+ *storageOffset_ = luaL_checkinteger(L, index+1)-1;
torch_Tensor_(c_readSizeStride)(L, index+2, allowStride, size_, stride_);
}
return;
@@ -111,7 +111,7 @@ ENDIF(C_SSE4_1_FOUND OR C_SSE4_2_FOUND)
IF(C_AVX_FOUND)
SET(CMAKE_C_FLAGS "-DUSE_AVX ${CMAKE_C_FLAGS}")
IF(MSVC)
- SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve5x5_avx.c PROPERTIES COMPILE_FLAGS "/Ox /fp:fast /arch:AVX /std:c99")
+ SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve5x5_avx.c PROPERTIES COMPILE_FLAGS "/Ox /fp:fast /arch:AVX /std:c99")
ELSE(MSVC)
SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve5x5_avx.c PROPERTIES COMPILE_FLAGS "-O3 -ffast-math -mavx -std=c99")
ENDIF(MSVC)
Oops, something went wrong.

0 comments on commit d643d55

Please sign in to comment.