Skip to content

Commit

Permalink
Linux fixes for 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik McClure committed Jun 11, 2014
1 parent 84d36b8 commit f411c9b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 26 deletions.
17 changes: 16 additions & 1 deletion include/bss-util/bss_compiler.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright ©2013 Black Sphere Studios
// Copyright ©2014 Black Sphere Studios
// For conditions of distribution and use, see copyright notice in "bss_util.h"

#ifndef __BSS_COMPILER_H__
Expand Down Expand Up @@ -45,6 +45,8 @@
#define BSS_FORCEINLINE inline
#define BSS_RESTRICT __restrict__
#define BSS_ALIGNED(sn, n) sn
#define MSC_FASTCALL BSS_FASTCALL
#define GCC_FASTCALL

# elif defined __GNUC__ // GCC
#define BSS_COMPILER_GCC
Expand All @@ -60,6 +62,7 @@
#define BSS_RESTRICT __restrict__
#define BSS_ALIGN(n) __attribute__((aligned(n)))
#define BSS_ALIGNED(sn, n) sn BSS_ALIGN(n)
#define BSS_VARIADIC_TEMPLATES
#ifndef __int8
#define __int8 char
#endif
Expand All @@ -73,6 +76,8 @@
#define __int64 long long
#endif
//typedef __int128 __int128; // GCC doesn't have __int64/32/16/8, but it does have __int128 for whatever reason.
#define MSC_FASTCALL
#define GCC_FASTCALL BSS_FASTCALL

#elif defined _MSC_VER // VC++
#define BSS_COMPILER_MSC
Expand All @@ -89,6 +94,16 @@
#define BSS_ALIGN(n) __declspec(align(n))
#define BSS_ALIGNED(sn, n) BSS_ALIGN(n) sn
#define BSS_VERIFY_HEAP _ASSERTE(_CrtCheckMemory());
#define FUNCPTRCC(m,CC) CC m
#define MSC_FASTCALL BSS_FASTCALL
#define GCC_FASTCALL
#if (_MANAGED == 1) || (_M_CEE == 1)
#define MSC_MANAGED
#endif

#if _MSC_VER >= 1800
#define BSS_VARIADIC_TEMPLATES
#endif
#endif

#if defined(__MINGW32__) || defined(__MINGW64__)
Expand Down
21 changes: 18 additions & 3 deletions include/bss-util/bss_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <float.h>
#ifdef BSS_COMPILER_GCC
#include <stdlib.h> // For abs(int) on GCC
#include <fpu_control.h> // for CPU control on GCC
#endif

namespace bss_util {
Expand Down Expand Up @@ -313,24 +314,38 @@ namespace bss_util {
return _mm_cvttsd_si32(_mm_load_sd(&f));
}

#ifndef MSC_MANAGED
BSS_FORCEINLINE static void fSetRounding(bool nearest)
{
uint a;
unsigned int a;
#ifdef BSS_COMPILER_MSC
_controlfp_s(&a, nearest?_RC_NEAR:_RC_CHOP, _MCW_RC);
#else
_FPU_GETCW(a);
a = (a&(~(_FPU_RC_NEAREST|_FPU_RC_DOWN|_FPU_RC_UP|_FPU_RC_ZERO)))|(nearest?_FPU_RC_NEAREST:_FPU_RC_ZERO);
_FPU_SETCW(a);
#endif
}
BSS_FORCEINLINE static void fSetDenormal(bool on)
{
uint a;
unsigned int a;
#ifdef BSS_COMPILER_MSC
_controlfp_s(&a, on?_DN_SAVE:_DN_FLUSH, _MCW_DN);
#else
_FPU_GETCW(a); // Linux doesn't know what the denormal flags are, so we just hardcode the values in from windows' flags.
a = (a&(~0x03000000))|(on?0:0x01000000);
_FPU_SETCW(a);
#endif
}
#endif

// Returns true if FPU is in single precision mode and false otherwise (false for both double and extended precision)
BSS_FORCEINLINE static bool FPUsingle()
{
#if defined(BSS_CPU_x86) || defined(BSS_CPU_x86_64) || defined(BSS_CPU_IA_64)
unsigned int i;
#ifdef BSS_COMPILER_GCC
__asm__ __volatile__ ("fnstcw %0" : "=m" (i));
_FPU_GETCW(i);
#elif defined(BSS_COMPILER_MSC)
i=_mm_getcsr();
#endif
Expand Down
32 changes: 19 additions & 13 deletions include/bss-util/cKhash.h → include/bss-util/cHash.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright ©2014 Black Sphere Studios
// For conditions of distribution and use, see copyright notice in "bss_util.h"

#ifndef __C_KHASH_H__BSS__
#define __C_KHASH_H__BSS__
#ifndef __C_HASH_H__BSS__
#define __C_HASH_H__BSS__

#include "khash.h"
#include "bss_defines.h"
Expand Down Expand Up @@ -306,13 +306,13 @@ namespace bss_util {
KHTYPE* _h;
};

inline khint_t KH_INT64_HASHFUNC(__int64 key) { return (khint32_t)((key)>>33^(key)^(key)<<11); }
template<class T>
inline static khint_t KH_INT64_HASHFUNC(__int64 key) { return (khint32_t)((key)>>33^(key)^(key)<<11); }
template<class T>
BSS_FORCEINLINE khint_t KH_INT_HASHFUNC(T key) { return (khint32_t)key; }
inline khint_t KH_STR_HASHFUNC(const char * s) { khint_t h = *s; if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s; return h; }
inline khint_t KH_STRINS_HASHFUNC(const char *s) { khint_t h = ((*s)>64&&(*s)<91)?(*s)+32:*s; if (h) for (++s ; *s; ++s) h = (h << 5) - h + (((*s)>64&&(*s)<91)?(*s)+32:*s); return h; }
inline khint_t KH_STRW_HASHFUNC(const wchar_t * s) { khint_t h = *s; if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s; return h; }
inline khint_t KH_STRWINS_HASHFUNC(const wchar_t *s) { khint_t h = towlower(*s); if (h) for (++s ; *s; ++s) h = (h << 5) - h + towlower(*s); return h; }
inline static khint_t KH_STR_HASHFUNC(const char * s) { khint_t h = *s; if(h) for(++s; *s; ++s) h = (h << 5) - h + *s; return h; }
inline static khint_t KH_STRINS_HASHFUNC(const char *s) { khint_t h = ((*s)>64&&(*s)<91)?(*s)+32:*s; if(h) for(++s; *s; ++s) h = (h << 5) - h + (((*s)>64&&(*s)<91)?(*s)+32:*s); return h; }
inline static khint_t KH_STRW_HASHFUNC(const wchar_t * s) { khint_t h = *s; if(h) for(++s; *s; ++s) h = (h << 5) - h + *s; return h; }
inline static khint_t KH_STRWINS_HASHFUNC(const wchar_t *s) { khint_t h = towlower(*s); if(h) for(++s; *s; ++s) h = (h << 5) - h + towlower(*s); return h; }
template<class T>
BSS_FORCEINLINE khint_t KH_POINTER_HASHFUNC(T p) {
#ifdef BSS_64BIT
Expand All @@ -324,25 +324,31 @@ namespace bss_util {
template<typename T, int I> struct KH_AUTO_HELPER { };
template<typename T> struct KH_AUTO_HELPER<T, 1> { BSS_FORCEINLINE static khint_t hash(T k) { return KH_POINTER_HASHFUNC<T>(k); } };
template<typename T> struct KH_AUTO_HELPER<T, 2> { BSS_FORCEINLINE static khint_t hash(T k) { return KH_INT_HASHFUNC<T>((__int32)k); } };
template<typename T> struct KH_AUTO_HELPER<T, 4> { BSS_FORCEINLINE static khint_t hash(T k) { return KH_INT64_HASHFUNC<T>((__int64)k); } };
template<typename T> struct KH_AUTO_HELPER<T, 4> { BSS_FORCEINLINE static khint_t hash(T k) { return KH_INT64_HASHFUNC((__int64)k); } };

#ifdef BSS_COMPILER_GCC // GCC decides that "force inline" doesn't really mean "force inline" so we have to put static here to protect it from it's own idiocy.
#define FIX_PISS_POOR_PROGRAMMING_IN_GCC static
#else
#define FIX_PISS_POOR_PROGRAMMING_IN_GCC
#endif

template<typename T>
BSS_FORCEINLINE khint_t KH_AUTO_HASHFUNC(T k) { return KH_AUTO_HELPER<T,std::is_pointer<T>::value + std::is_integral<T>::value*2*(1+(sizeof(T)==8))>::hash(k); }
FIX_PISS_POOR_PROGRAMMING_IN_GCC BSS_FORCEINLINE khint_t KH_AUTO_HASHFUNC(T k) { return KH_AUTO_HELPER<T, std::is_pointer<T>::value + std::is_integral<T>::value*2*(1+(sizeof(T)==8))>::hash(k); }
template<> BSS_FORCEINLINE khint_t KH_AUTO_HASHFUNC<const char*>(const char* k) { return KH_STR_HASHFUNC(k); }
template<> BSS_FORCEINLINE khint_t KH_AUTO_HASHFUNC<char*>(char* k) { return KH_STR_HASHFUNC(k); }
template<> BSS_FORCEINLINE khint_t KH_AUTO_HASHFUNC<const wchar_t*>(const wchar_t* k) { return KH_STRW_HASHFUNC(k); }
template<> BSS_FORCEINLINE khint_t KH_AUTO_HASHFUNC<wchar_t*>(wchar_t* k) { return KH_STRW_HASHFUNC(k); }
template<> BSS_FORCEINLINE khint_t KH_AUTO_HASHFUNC<double>(double k) { return KH_INT64_HASHFUNC(*(__int64*)&k); }
template<> BSS_FORCEINLINE khint_t KH_AUTO_HASHFUNC<float>(float k) { return *(khint32_t*)&k; }
template<typename T> BSS_FORCEINLINE khint_t KH_AUTOINS_HASHFUNC(T k) { return KH_STRINS_HASHFUNC(k); }
template<typename T> FIX_PISS_POOR_PROGRAMMING_IN_GCC BSS_FORCEINLINE khint_t KH_AUTOINS_HASHFUNC(T k) { return KH_STRINS_HASHFUNC(k); }
template<> BSS_FORCEINLINE khint_t KH_AUTOINS_HASHFUNC<const wchar_t*>(const wchar_t* k) { return KH_STRWINS_HASHFUNC(k); }
template<> BSS_FORCEINLINE khint_t KH_AUTOINS_HASHFUNC<wchar_t*>(wchar_t* k) { return KH_STRWINS_HASHFUNC(k); }

template<typename T>
BSS_FORCEINLINE bool KH_AUTO_EQUALFUNC(T a, T b) { return a==b; }
FIX_PISS_POOR_PROGRAMMING_IN_GCC BSS_FORCEINLINE bool KH_AUTO_EQUALFUNC(T a, T b) { return a==b; }
template<> BSS_FORCEINLINE bool KH_AUTO_EQUALFUNC<const char*>(const char* a, const char* b) { return strcmp(a, b)==0; }
template<> BSS_FORCEINLINE bool KH_AUTO_EQUALFUNC<const wchar_t*>(const wchar_t* a, const wchar_t* b) { return wcscmp(a, b)==0; }
template<typename T> BSS_FORCEINLINE bool KH_AUTOINS_EQUALFUNC(T a, T b) { return STRICMP(a, b)==0; }
template<typename T> FIX_PISS_POOR_PROGRAMMING_IN_GCC BSS_FORCEINLINE bool KH_AUTOINS_EQUALFUNC(T a, T b) { return STRICMP(a, b)==0; }
template<> BSS_FORCEINLINE bool KH_AUTOINS_EQUALFUNC<const wchar_t*>(const wchar_t* a, const wchar_t* b) { return WCSICMP(a, b)==0; }
template<> BSS_FORCEINLINE bool KH_AUTOINS_EQUALFUNC<wchar_t*>(wchar_t* a, wchar_t* b) { return WCSICMP(a, b)==0; }

Expand Down
15 changes: 8 additions & 7 deletions include/bss-util/cRefCounter.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright ©2013 Black Sphere Studios
// Copyright ©2014 Black Sphere Studios
// For conditions of distribution and use, see copyright notice in "bss_util.h"

#ifndef __C_REFCOUNTER_H__BSS__ //These are used in case this header file is used by two different projects dependent on each other, resulting in duplicates which cannot be differentiated by #pragma once
Expand All @@ -11,21 +11,22 @@ namespace bss_util {
class BSS_COMPILER_DLLEXPORT cRefCounter
{
public:
// Increments and returns the reference counter
BSS_FORCEINLINE unsigned int Grab() { return ++_refs; } const
// Decrements the reference counter and calls delete this; if it is equal to or less then 0
BSS_FORCEINLINE void Drop() { if(--_refs <= 0) DestroyThis(); }

protected:
// Constructor - Reference is set to 0 because you may or may not have a persistent reference to this, or something else will try to grab it or whatever
inline cRefCounter() { _refs = 0; }
inline cRefCounter(const cRefCounter& copy) { _refs = 0; }
// Destructor - Does nothing, but because it is virtual, ensures that all superclasses get destroyed as well
virtual ~cRefCounter() { }
// Increments and returns the reference counter
BSS_FORCEINLINE unsigned int Grab() { return ++_refs; } const
// Decrements the reference counter and calls delete this; if it is equal to or less then 0
BSS_FORCEINLINE void Drop() { if(--_refs <= 0) DestroyThis(); }
// Destroys this object - made a seperate virtual function so it is overridable to ensure it is deleted in the proper DLL
virtual void DestroyThis() { delete this; }

protected:
int _refs; //holds the number of references held for this object
};
}

#endif
#endif
2 changes: 1 addition & 1 deletion include/cAudioResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef __C_AUDIO_RESOURCE_H__TOAL__
#define __C_AUDIO_RESOURCE_H__TOAL__

#include "bss-util/cKhash.h"
#include "bss-util/cHash.h"
#include "bss-util/cRefCounter.h"
#include "bss-util/cStr.h"
#include "bss-util/bss_alloc_fixed.h"
Expand Down
2 changes: 1 addition & 1 deletion tinyoal.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ INCLUDE_DIRS := include
LIBRARY_DIRS :=
LIBRARIES := rt

CPPFLAGS += -fPIC -D BSS_UTIL_EXPORTS -Wall -Wno-attributes -Wno-unknown-pragmas -Wno-reorder -Wno-missing-braces -std=gnu++0x
CPPFLAGS += -fPIC -D BSS_UTIL_EXPORTS -Wall -Wno-attributes -Wno-unknown-pragmas -Wno-reorder -Wno-missing-braces -Wno-unused-function -std=gnu++0x
LDFLAGS += -shared

include base.mk
Expand Down

0 comments on commit f411c9b

Please sign in to comment.