summaryrefslogtreecommitdiffstats
path: root/source/core
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2018-10-25 11:24:16 -0400
committerGitHub <noreply@github.com>2018-10-25 11:24:16 -0400
commit4f0415e338862ffec50c2d47eddea958255b504e (patch)
tree084c8e25552e328ed14eafcf0431493f58f973e8 /source/core
parent2700a89f8c80620f1d523563cc80ec0da39e9761 (diff)
Feature/premake linux (#689)
* Premake work in progress for linux. * Added dump function. * Remove examples on linux Small warning fix. * * Don't build render-test on linux * Removed work around virtual destructor warning, and just used virtual dtor for simplicity * Git ignore obj directories * Fix premake working on windows. * * Fix sprintf_s functions * Make generates arg parsing more robust * Added FloatIntUnion to avoid type punning/strong aliasing issues, and repeated union definitions. * Work around problems building on linux with getClass claiming a strict aliasing issue. * Fix for targetBlock appearing potentiall used unintialized to gcc. * Linux slang link options -fPIC to make dll. * Add -fPIC to build options on linux. * Add -ldl for linux on slang. * Fixes to try and get premake working with .so on linux. * Make core compile with -fPIC * Try to fix linux linking with --no-as-needed before -ldl * Add rpath back. * Remove render-gl from linux build. * Re-add location for linux. * Don't include <malloc.h> except on windows. * Remove unused line to fix warning on osx. * Remove ambiguity on OSX for operator <<. * Fixing ambiguity with operator overloading and Int types for OSX. * Fix ambiguity around UInt and operator * Fix ambiguity of UInt conversion for OSX. * Added UnambiguousInt and UnambiguousUInt to make it easier to work around OSX integer coercion for UInt/Int types.
Diffstat (limited to 'source/core')
-rw-r--r--source/core/allocator.h5
-rw-r--r--source/core/common.h7
-rw-r--r--source/core/secure-crt.h20
-rw-r--r--source/core/slang-cpu-defines.h13
-rw-r--r--source/core/slang-math.h50
-rw-r--r--source/core/stream.cpp8
-rw-r--r--source/core/type-traits.h4
7 files changed, 65 insertions, 42 deletions
diff --git a/source/core/allocator.h b/source/core/allocator.h
index 46550a054..3306d3780 100644
--- a/source/core/allocator.h
+++ b/source/core/allocator.h
@@ -2,6 +2,9 @@
#define CORE_LIB_ALLOCATOR_H
#include <stdlib.h>
+#ifdef _MSC_VER
+# include <malloc.h>
+#endif
namespace Slang
{
@@ -9,6 +12,8 @@ namespace Slang
{
#ifdef _MSC_VER
return _aligned_malloc(size, alignment);
+#elif defined(__CYGWIN__)
+ return aligned_alloc(alignment, size);
#else
void * rs = 0;
int succ = posix_memalign(&rs, alignment, size);
diff --git a/source/core/common.h b/source/core/common.h
index 17161ab14..5aa7b7737 100644
--- a/source/core/common.h
+++ b/source/core/common.h
@@ -38,9 +38,12 @@ namespace Slang
}
#ifdef _MSC_VER
-#define SLANG_RETURN_NEVER __declspec(noreturn)
+# define SLANG_RETURN_NEVER __declspec(noreturn)
+//#elif SLANG_CLANG
+//# define SLANG_RETURN_NEVER [[noreturn]]
#else
-#define SLANG_RETURN_NEVER /* empty */
+# define SLANG_RETURN_NEVER [[noreturn]]
+//# define SLANG_RETURN_NEVER /* empty */
#endif
#ifdef _MSC_VER
diff --git a/source/core/secure-crt.h b/source/core/secure-crt.h
index a76fb2679..52a0d4870 100644
--- a/source/core/secure-crt.h
+++ b/source/core/secure-crt.h
@@ -5,6 +5,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <strings.h>
+
#include <wchar.h>
inline void memcpy_s(void *dest, size_t numberOfElements, const void * src, size_t count)
@@ -30,16 +32,26 @@ inline size_t wcsnlen_s(const wchar_t * str, size_t /*numberofElements*/)
return wcslen(str);
}
-inline size_t strnlen_s(const char * str, size_t numberofElements)
+inline size_t strnlen_s(const char * str, size_t numberOfElements)
{
- return strnlen(str, numberofElements);
+#if defined( __CYGWIN__ )
+ const char* cur = str;
+ if (str)
+ {
+ const char*const end = str + numberOfElements;
+ while (*cur && cur < end) cur++;
+ }
+ return size_t(cur - str);
+#else
+ return strnlen(str, numberOfElements);
+#endif
}
inline int sprintf_s(char * buffer, size_t sizeOfBuffer, const char * format, ...)
{
va_list argptr;
va_start(argptr, format);
- int rs = snprintf(buffer, sizeOfBuffer, format, argptr);
+ int rs = vsnprintf(buffer, sizeOfBuffer, format, argptr);
va_end(argptr);
return rs;
}
@@ -48,7 +60,7 @@ inline int swprintf_s(wchar_t * buffer, size_t sizeOfBuffer, const wchar_t * for
{
va_list argptr;
va_start(argptr, format);
- int rs = swprintf(buffer, sizeOfBuffer, format, argptr);
+ int rs = vswprintf(buffer, sizeOfBuffer, format, argptr);
va_end(argptr);
return rs;
}
diff --git a/source/core/slang-cpu-defines.h b/source/core/slang-cpu-defines.h
index dc76008b9..5ad1ecf88 100644
--- a/source/core/slang-cpu-defines.h
+++ b/source/core/slang-cpu-defines.h
@@ -54,6 +54,19 @@
#define SLANG_PTR_IS_64 (SLANG_PROCESSOR_ARM_64 | SLANG_PROCESSOR_X86_64 | SLANG_PROCESSOR_POWER_PC_64)
#define SLANG_PTR_IS_32 (SLANG_PTR_IS_64 ^ 1)
+// TODO: This isn't great. The problem is UInt maps to size_t, and on some targets (like OSX)
+// size_t is distinct from any other integral type. So that creates an ambiguity
+// Really we want to modify the StringBuilder and elsewhere to handle the case when it is known it can't unambiguously coerce
+namespace Slang {
+#ifdef SLANG_PTR_IS_64
+typedef UInt64 UnambigousUInt;
+typedef Int64 UnambiguousInt;
+#else
+typedef UInt32 UnambigousUInt;
+typedef Int32 UnambiguousInt;
+#endif
+} // namespace Slang
+
// Processor features
#if SLANG_PROCESSOR_FAMILY_X86
# define SLANG_LITTLE_ENDIAN 1
diff --git a/source/core/slang-math.h b/source/core/slang-math.h
index 6d6b3e7a1..a245e2d2c 100644
--- a/source/core/slang-math.h
+++ b/source/core/slang-math.h
@@ -8,6 +8,17 @@ namespace Slang
class Math
{
public:
+ // Use to fix type punning issues with strict aliasing
+ union FloatIntUnion
+ {
+ float fvalue;
+ int ivalue;
+
+ inline static FloatIntUnion makeFromInt(int i) { FloatIntUnion cast; cast.ivalue = i; return cast; }
+ inline static FloatIntUnion makeFromFloat(float f) { FloatIntUnion cast; cast.fvalue = f; return cast; }
+ };
+
+
static const float Pi;
template<typename T>
static T Min(const T& v1, const T&v2)
@@ -110,30 +121,19 @@ namespace Slang
}
*/
};
- inline int FloatAsInt(float val)
+ inline int FloatAsInt(float val)
{
- union InterCast
- {
- float fvalue;
- int ivalue;
- } cast;
- cast.fvalue = val;
- return cast.ivalue;
+ return Math::FloatIntUnion::makeFromFloat(val).ivalue;
}
- inline float IntAsFloat(int val)
+ inline float IntAsFloat(int val)
{
- union InterCast
- {
- float fvalue;
- int ivalue;
- } cast;
- cast.ivalue = val;
- return cast.fvalue;
+ return Math::FloatIntUnion::makeFromInt(val).fvalue;
}
inline unsigned short FloatToHalf(float val)
{
- int x = *(int*)&val;
+ const auto x = FloatAsInt(val);
+
unsigned short bits = (x >> 16) & 0x8000;
unsigned short m = (x >> 12) & 0x07ff;
unsigned int e = (x >> 23) & 0xff;
@@ -158,19 +158,9 @@ namespace Slang
inline float HalfToFloat(unsigned short input)
{
- union InterCast
- {
- float fvalue;
- int ivalue;
- InterCast() = default;
- InterCast(int ival)
- {
- ivalue = ival;
- }
- };
- static const InterCast magic = InterCast((127 + (127 - 15)) << 23);
- static const InterCast was_infnan = InterCast((127 + 16) << 23);
- InterCast o;
+ static const auto magic = Math::FloatIntUnion::makeFromInt((127 + (127 - 15)) << 23);
+ static const auto was_infnan = Math::FloatIntUnion::makeFromInt((127 + 16) << 23);
+ Math::FloatIntUnion o;
o.ivalue = (input & 0x7fff) << 13; // exponent/mantissa bits
o.fvalue *= magic.fvalue; // exponent adjust
if (o.fvalue >= was_infnan.fvalue) // make sure Inf/NaN survive
diff --git a/source/core/stream.cpp b/source/core/stream.cpp
index b705b13a1..949ce718c 100644
--- a/source/core/stream.cpp
+++ b/source/core/stream.cpp
@@ -91,9 +91,9 @@ namespace Slang
default:
break;
}
- int shFlag;
#ifdef _WIN32
- switch (share)
+ int shFlag = _SH_DENYRW;
+ switch (share)
{
case Slang::FileShare::None:
shFlag = _SH_DENYRW;
@@ -130,13 +130,13 @@ namespace Slang
}
Int64 FileStream::GetPosition()
{
-#ifdef _WIN32
+#if defined(_WIN32) || defined(__CYGWIN__)
fpos_t pos;
fgetpos(handle, &pos);
return pos;
#elif defined(__APPLE__)
return ftell(handle);
-#else
+#else
fpos64_t pos;
fgetpos64(handle, &pos);
return *(Int64*)(&pos);
diff --git a/source/core/type-traits.h b/source/core/type-traits.h
index 5a05538d3..804b4d3fe 100644
--- a/source/core/type-traits.h
+++ b/source/core/type-traits.h
@@ -37,8 +37,8 @@ namespace Slang
template <typename B, typename D>
struct IsConvertible
{
- static TraitResultYes Use(B) {};
- static TraitResultNo Use(...) {};
+ static TraitResultYes Use(B) { return TraitResultYes(); };
+ static TraitResultNo Use(...) { return TraitResultNo(); };
enum { Value = sizeof(Use(*(D*)(nullptr))) == sizeof(TraitResultYes) };
};
}