Do not indent C++ namespace contents
This is a style change. Visual Studio defaults to indenting namespace
contents, and this was adopted for a long time, but with the new Vulkan
implementation this was abandoned. However the legacy code borrowed from
the OpenGL ES implementation still used indentation so it was
inconsistent.
The justification for not indenting namespace contents is that
namespaces are merely a way to avoid name clashes with other projects
we don't control directly (and in rare cases internal subprojects when
we want to reuse the same names). Hence the vast majority of files have
a single namespace, and unlike indentation used for ease of discerning
control flow blocks, class contents, or function contents, which can
become highly nested, there is no such readability advantage to
indenting namespace contents.
This is also the Google style recommendation (though no justification or
discussion is provided):
https://google.github.io/styleguide/cppguide.html#Namespace_Formatting
One reasonable counter-argument is consistency with other blocks of
curly brackets, but considering that most namespaces span almost the
entire file, it's a substantial waste of line length.
Because there is no indentation, there's also no need to have the open
and closing brackets line up as a visual aid, like we prefer for other
uses of curly brackets. So we place the open bracket on the same line as
the namespace keyword.
A comment is added to the closing bracket to discern it from other
closing brackets. It also makes it easier to find the end of anonymous
namespaces which typically go at the top of the source file.
This change is make separately from applying clang-format because diff
tools mark all these unindented lines as changes and this makes it hard
to review the smaller style changes made by clang-format. The OpenGL ES
and Direct3D code is left untouched because it is in maintenance mode
and in case of regressions we want easy 'blame' tool usage.
Bug: b/144825072
Change-Id: Ie2925ebd697e1ffa7c4cbdc9a946531f11f4d934
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/39348
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/System/CPUID.cpp b/src/System/CPUID.cpp
index c080034..3c62828 100644
--- a/src/System/CPUID.cpp
+++ b/src/System/CPUID.cpp
@@ -27,275 +27,276 @@
#include <sys/types.h>
#endif
-namespace sw
+namespace sw {
+
+bool CPUID::MMX = detectMMX();
+bool CPUID::CMOV = detectCMOV();
+bool CPUID::SSE = detectSSE();
+bool CPUID::SSE2 = detectSSE2();
+bool CPUID::SSE3 = detectSSE3();
+bool CPUID::SSSE3 = detectSSSE3();
+bool CPUID::SSE4_1 = detectSSE4_1();
+int CPUID::cores = detectCoreCount();
+int CPUID::affinity = detectAffinity();
+
+bool CPUID::enableMMX = true;
+bool CPUID::enableCMOV = true;
+bool CPUID::enableSSE = true;
+bool CPUID::enableSSE2 = true;
+bool CPUID::enableSSE3 = true;
+bool CPUID::enableSSSE3 = true;
+bool CPUID::enableSSE4_1 = true;
+
+void CPUID::setEnableMMX(bool enable)
{
- bool CPUID::MMX = detectMMX();
- bool CPUID::CMOV = detectCMOV();
- bool CPUID::SSE = detectSSE();
- bool CPUID::SSE2 = detectSSE2();
- bool CPUID::SSE3 = detectSSE3();
- bool CPUID::SSSE3 = detectSSSE3();
- bool CPUID::SSE4_1 = detectSSE4_1();
- int CPUID::cores = detectCoreCount();
- int CPUID::affinity = detectAffinity();
+ enableMMX = enable;
- bool CPUID::enableMMX = true;
- bool CPUID::enableCMOV = true;
- bool CPUID::enableSSE = true;
- bool CPUID::enableSSE2 = true;
- bool CPUID::enableSSE3 = true;
- bool CPUID::enableSSSE3 = true;
- bool CPUID::enableSSE4_1 = true;
-
- void CPUID::setEnableMMX(bool enable)
+ if(!enableMMX)
{
- enableMMX = enable;
-
- if(!enableMMX)
- {
- enableSSE = false;
- enableSSE2 = false;
- enableSSE3 = false;
- enableSSSE3 = false;
- enableSSE4_1 = false;
- }
- }
-
- void CPUID::setEnableCMOV(bool enable)
- {
- enableCMOV = enable;
-
- if(!CMOV)
- {
- enableSSE = false;
- enableSSE2 = false;
- enableSSE3 = false;
- enableSSSE3 = false;
- enableSSE4_1 = false;
- }
- }
-
- void CPUID::setEnableSSE(bool enable)
- {
- enableSSE = enable;
-
- if(enableSSE)
- {
- enableMMX = true;
- enableCMOV = true;
- }
- else
- {
- enableSSE2 = false;
- enableSSE3 = false;
- enableSSSE3 = false;
- enableSSE4_1 = false;
- }
- }
-
- void CPUID::setEnableSSE2(bool enable)
- {
- enableSSE2 = enable;
-
- if(enableSSE2)
- {
- enableMMX = true;
- enableCMOV = true;
- enableSSE = true;
- }
- else
- {
- enableSSE3 = false;
- enableSSSE3 = false;
- enableSSE4_1 = false;
- }
- }
-
- void CPUID::setEnableSSE3(bool enable)
- {
- enableSSE3 = enable;
-
- if(enableSSE3)
- {
- enableMMX = true;
- enableCMOV = true;
- enableSSE = true;
- enableSSE2 = true;
- }
- else
- {
- enableSSSE3 = false;
- enableSSE4_1 = false;
- }
- }
-
- void CPUID::setEnableSSSE3(bool enable)
- {
- enableSSSE3 = enable;
-
- if(enableSSSE3)
- {
- enableMMX = true;
- enableCMOV = true;
- enableSSE = true;
- enableSSE2 = true;
- enableSSE3 = true;
- }
- else
- {
- enableSSE4_1 = false;
- }
- }
-
- void CPUID::setEnableSSE4_1(bool enable)
- {
- enableSSE4_1 = enable;
-
- if(enableSSE4_1)
- {
- enableMMX = true;
- enableCMOV = true;
- enableSSE = true;
- enableSSE2 = true;
- enableSSE3 = true;
- enableSSSE3 = true;
- }
- }
-
- static void cpuid(int registers[4], int info)
- {
- #if defined(__i386__) || defined(__x86_64__)
- #if defined(_WIN32)
- __cpuid(registers, info);
- #else
- __asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info));
- #endif
- #else
- registers[0] = 0;
- registers[1] = 0;
- registers[2] = 0;
- registers[3] = 0;
- #endif
- }
-
- bool CPUID::detectMMX()
- {
- int registers[4];
- cpuid(registers, 1);
- return MMX = (registers[3] & 0x00800000) != 0;
- }
-
- bool CPUID::detectCMOV()
- {
- int registers[4];
- cpuid(registers, 1);
- return CMOV = (registers[3] & 0x00008000) != 0;
- }
-
- bool CPUID::detectSSE()
- {
- int registers[4];
- cpuid(registers, 1);
- return SSE = (registers[3] & 0x02000000) != 0;
- }
-
- bool CPUID::detectSSE2()
- {
- int registers[4];
- cpuid(registers, 1);
- return SSE2 = (registers[3] & 0x04000000) != 0;
- }
-
- bool CPUID::detectSSE3()
- {
- int registers[4];
- cpuid(registers, 1);
- return SSE3 = (registers[2] & 0x00000001) != 0;
- }
-
- bool CPUID::detectSSSE3()
- {
- int registers[4];
- cpuid(registers, 1);
- return SSSE3 = (registers[2] & 0x00000200) != 0;
- }
-
- bool CPUID::detectSSE4_1()
- {
- int registers[4];
- cpuid(registers, 1);
- return SSE4_1 = (registers[2] & 0x00080000) != 0;
- }
-
- int CPUID::detectCoreCount()
- {
- int cores = 0;
-
- #if defined(_WIN32)
- DWORD_PTR processAffinityMask = 1;
- DWORD_PTR systemAffinityMask = 1;
-
- GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask);
-
- while(systemAffinityMask)
- {
- if(systemAffinityMask & 1)
- {
- cores++;
- }
-
- systemAffinityMask >>= 1;
- }
- #else
- cores = sysconf(_SC_NPROCESSORS_ONLN);
- #endif
-
- if(cores < 1) cores = 1;
- if(cores > 16) cores = 16;
-
- return cores; // FIXME: Number of physical cores
- }
-
- int CPUID::detectAffinity()
- {
- int cores = 0;
-
- #if defined(_WIN32)
- DWORD_PTR processAffinityMask = 1;
- DWORD_PTR systemAffinityMask = 1;
-
- GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask);
-
- while(processAffinityMask)
- {
- if(processAffinityMask & 1)
- {
- cores++;
- }
-
- processAffinityMask >>= 1;
- }
- #else
- return detectCoreCount(); // FIXME: Assumes no affinity limitation
- #endif
-
- if(cores < 1) cores = 1;
- if(cores > 16) cores = 16;
-
- return cores;
- }
-
- void CPUID::setFlushToZero(bool enable)
- {
- #if defined(_MSC_VER)
- _controlfp(enable ? _DN_FLUSH : _DN_SAVE, _MCW_DN);
- #else
- // Unimplemented
- #endif
- }
-
- void CPUID::setDenormalsAreZero(bool enable)
- {
- // Unimplemented
+ enableSSE = false;
+ enableSSE2 = false;
+ enableSSE3 = false;
+ enableSSSE3 = false;
+ enableSSE4_1 = false;
}
}
+
+void CPUID::setEnableCMOV(bool enable)
+{
+ enableCMOV = enable;
+
+ if(!CMOV)
+ {
+ enableSSE = false;
+ enableSSE2 = false;
+ enableSSE3 = false;
+ enableSSSE3 = false;
+ enableSSE4_1 = false;
+ }
+}
+
+void CPUID::setEnableSSE(bool enable)
+{
+ enableSSE = enable;
+
+ if(enableSSE)
+ {
+ enableMMX = true;
+ enableCMOV = true;
+ }
+ else
+ {
+ enableSSE2 = false;
+ enableSSE3 = false;
+ enableSSSE3 = false;
+ enableSSE4_1 = false;
+ }
+}
+
+void CPUID::setEnableSSE2(bool enable)
+{
+ enableSSE2 = enable;
+
+ if(enableSSE2)
+ {
+ enableMMX = true;
+ enableCMOV = true;
+ enableSSE = true;
+ }
+ else
+ {
+ enableSSE3 = false;
+ enableSSSE3 = false;
+ enableSSE4_1 = false;
+ }
+}
+
+void CPUID::setEnableSSE3(bool enable)
+{
+ enableSSE3 = enable;
+
+ if(enableSSE3)
+ {
+ enableMMX = true;
+ enableCMOV = true;
+ enableSSE = true;
+ enableSSE2 = true;
+ }
+ else
+ {
+ enableSSSE3 = false;
+ enableSSE4_1 = false;
+ }
+}
+
+void CPUID::setEnableSSSE3(bool enable)
+{
+ enableSSSE3 = enable;
+
+ if(enableSSSE3)
+ {
+ enableMMX = true;
+ enableCMOV = true;
+ enableSSE = true;
+ enableSSE2 = true;
+ enableSSE3 = true;
+ }
+ else
+ {
+ enableSSE4_1 = false;
+ }
+}
+
+void CPUID::setEnableSSE4_1(bool enable)
+{
+ enableSSE4_1 = enable;
+
+ if(enableSSE4_1)
+ {
+ enableMMX = true;
+ enableCMOV = true;
+ enableSSE = true;
+ enableSSE2 = true;
+ enableSSE3 = true;
+ enableSSSE3 = true;
+ }
+}
+
+static void cpuid(int registers[4], int info)
+{
+ #if defined(__i386__) || defined(__x86_64__)
+ #if defined(_WIN32)
+ __cpuid(registers, info);
+ #else
+ __asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info));
+ #endif
+ #else
+ registers[0] = 0;
+ registers[1] = 0;
+ registers[2] = 0;
+ registers[3] = 0;
+ #endif
+}
+
+bool CPUID::detectMMX()
+{
+ int registers[4];
+ cpuid(registers, 1);
+ return MMX = (registers[3] & 0x00800000) != 0;
+}
+
+bool CPUID::detectCMOV()
+{
+ int registers[4];
+ cpuid(registers, 1);
+ return CMOV = (registers[3] & 0x00008000) != 0;
+}
+
+bool CPUID::detectSSE()
+{
+ int registers[4];
+ cpuid(registers, 1);
+ return SSE = (registers[3] & 0x02000000) != 0;
+}
+
+bool CPUID::detectSSE2()
+{
+ int registers[4];
+ cpuid(registers, 1);
+ return SSE2 = (registers[3] & 0x04000000) != 0;
+}
+
+bool CPUID::detectSSE3()
+{
+ int registers[4];
+ cpuid(registers, 1);
+ return SSE3 = (registers[2] & 0x00000001) != 0;
+}
+
+bool CPUID::detectSSSE3()
+{
+ int registers[4];
+ cpuid(registers, 1);
+ return SSSE3 = (registers[2] & 0x00000200) != 0;
+}
+
+bool CPUID::detectSSE4_1()
+{
+ int registers[4];
+ cpuid(registers, 1);
+ return SSE4_1 = (registers[2] & 0x00080000) != 0;
+}
+
+int CPUID::detectCoreCount()
+{
+ int cores = 0;
+
+ #if defined(_WIN32)
+ DWORD_PTR processAffinityMask = 1;
+ DWORD_PTR systemAffinityMask = 1;
+
+ GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask);
+
+ while(systemAffinityMask)
+ {
+ if(systemAffinityMask & 1)
+ {
+ cores++;
+ }
+
+ systemAffinityMask >>= 1;
+ }
+ #else
+ cores = sysconf(_SC_NPROCESSORS_ONLN);
+ #endif
+
+ if(cores < 1) cores = 1;
+ if(cores > 16) cores = 16;
+
+ return cores; // FIXME: Number of physical cores
+}
+
+int CPUID::detectAffinity()
+{
+ int cores = 0;
+
+ #if defined(_WIN32)
+ DWORD_PTR processAffinityMask = 1;
+ DWORD_PTR systemAffinityMask = 1;
+
+ GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask);
+
+ while(processAffinityMask)
+ {
+ if(processAffinityMask & 1)
+ {
+ cores++;
+ }
+
+ processAffinityMask >>= 1;
+ }
+ #else
+ return detectCoreCount(); // FIXME: Assumes no affinity limitation
+ #endif
+
+ if(cores < 1) cores = 1;
+ if(cores > 16) cores = 16;
+
+ return cores;
+}
+
+void CPUID::setFlushToZero(bool enable)
+{
+ #if defined(_MSC_VER)
+ _controlfp(enable ? _DN_FLUSH : _DN_SAVE, _MCW_DN);
+ #else
+ // Unimplemented
+ #endif
+}
+
+void CPUID::setDenormalsAreZero(bool enable)
+{
+ // Unimplemented
+}
+
+} // namespace sw
diff --git a/src/System/CPUID.hpp b/src/System/CPUID.hpp
index 3c21cd7..5fbb89f 100644
--- a/src/System/CPUID.hpp
+++ b/src/System/CPUID.hpp
@@ -15,123 +15,127 @@
#ifndef sw_CPUID_hpp
#define sw_CPUID_hpp
-namespace sw
+namespace sw {
+
+#if !defined(__i386__) && defined(_M_IX86)
+ #define __i386__ 1
+#endif
+
+#if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64))
+ #define __x86_64__ 1
+#endif
+
+class CPUID
{
- #if !defined(__i386__) && defined(_M_IX86)
- #define __i386__ 1
- #endif
+public:
+ static bool supportsMMX();
+ static bool supportsCMOV();
+ static bool supportsMMX2(); // MMX instructions added by SSE: pshufw, pmulhuw, pmovmskb, pavgw/b, pextrw, pinsrw, pmaxsw/ub, etc.
+ static bool supportsSSE();
+ static bool supportsSSE2();
+ static bool supportsSSE3();
+ static bool supportsSSSE3();
+ static bool supportsSSE4_1();
+ static int coreCount();
+ static int processAffinity();
- #if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64))
- #define __x86_64__ 1
- #endif
+ static void setEnableMMX(bool enable);
+ static void setEnableCMOV(bool enable);
+ static void setEnableSSE(bool enable);
+ static void setEnableSSE2(bool enable);
+ static void setEnableSSE3(bool enable);
+ static void setEnableSSSE3(bool enable);
+ static void setEnableSSE4_1(bool enable);
- class CPUID
- {
- public:
- static bool supportsMMX();
- static bool supportsCMOV();
- static bool supportsMMX2(); // MMX instructions added by SSE: pshufw, pmulhuw, pmovmskb, pavgw/b, pextrw, pinsrw, pmaxsw/ub, etc.
- static bool supportsSSE();
- static bool supportsSSE2();
- static bool supportsSSE3();
- static bool supportsSSSE3();
- static bool supportsSSE4_1();
- static int coreCount();
- static int processAffinity();
+ static void setFlushToZero(bool enable); // Denormal results are written as zero
+ static void setDenormalsAreZero(bool enable); // Denormal inputs are read as zero
- static void setEnableMMX(bool enable);
- static void setEnableCMOV(bool enable);
- static void setEnableSSE(bool enable);
- static void setEnableSSE2(bool enable);
- static void setEnableSSE3(bool enable);
- static void setEnableSSSE3(bool enable);
- static void setEnableSSE4_1(bool enable);
+private:
+ static bool MMX;
+ static bool CMOV;
+ static bool SSE;
+ static bool SSE2;
+ static bool SSE3;
+ static bool SSSE3;
+ static bool SSE4_1;
+ static int cores;
+ static int affinity;
- static void setFlushToZero(bool enable); // Denormal results are written as zero
- static void setDenormalsAreZero(bool enable); // Denormal inputs are read as zero
+ static bool enableMMX;
+ static bool enableCMOV;
+ static bool enableSSE;
+ static bool enableSSE2;
+ static bool enableSSE3;
+ static bool enableSSSE3;
+ static bool enableSSE4_1;
- private:
- static bool MMX;
- static bool CMOV;
- static bool SSE;
- static bool SSE2;
- static bool SSE3;
- static bool SSSE3;
- static bool SSE4_1;
- static int cores;
- static int affinity;
+ static bool detectMMX();
+ static bool detectCMOV();
+ static bool detectSSE();
+ static bool detectSSE2();
+ static bool detectSSE3();
+ static bool detectSSSE3();
+ static bool detectSSE4_1();
+ static int detectCoreCount();
+ static int detectAffinity();
+};
- static bool enableMMX;
- static bool enableCMOV;
- static bool enableSSE;
- static bool enableSSE2;
- static bool enableSSE3;
- static bool enableSSSE3;
- static bool enableSSE4_1;
+} // namespace sw
- static bool detectMMX();
- static bool detectCMOV();
- static bool detectSSE();
- static bool detectSSE2();
- static bool detectSSE3();
- static bool detectSSSE3();
- static bool detectSSE4_1();
- static int detectCoreCount();
- static int detectAffinity();
- };
+/* Inline implementation */
+
+namespace sw {
+
+inline bool CPUID::supportsMMX()
+{
+ return MMX && enableMMX;
}
-namespace sw
+inline bool CPUID::supportsCMOV()
{
- inline bool CPUID::supportsMMX()
- {
- return MMX && enableMMX;
- }
-
- inline bool CPUID::supportsCMOV()
- {
- return CMOV && enableCMOV;
- }
-
- inline bool CPUID::supportsMMX2()
- {
- return supportsSSE(); // Coincides with 64-bit integer vector instructions supported by SSE
- }
-
- inline bool CPUID::supportsSSE()
- {
- return SSE && enableSSE;
- }
-
- inline bool CPUID::supportsSSE2()
- {
- return SSE2 && enableSSE2;
- }
-
- inline bool CPUID::supportsSSE3()
- {
- return SSE3 && enableSSE3;
- }
-
- inline bool CPUID::supportsSSSE3()
- {
- return SSSE3 && enableSSSE3;
- }
-
- inline bool CPUID::supportsSSE4_1()
- {
- return SSE4_1 && enableSSE4_1;
- }
-
- inline int CPUID::coreCount()
- {
- return cores;
- }
-
- inline int CPUID::processAffinity()
- {
- return affinity;
- }
+ return CMOV && enableCMOV;
}
+inline bool CPUID::supportsMMX2()
+{
+ return supportsSSE(); // Coincides with 64-bit integer vector instructions supported by SSE
+}
+
+inline bool CPUID::supportsSSE()
+{
+ return SSE && enableSSE;
+}
+
+inline bool CPUID::supportsSSE2()
+{
+ return SSE2 && enableSSE2;
+}
+
+inline bool CPUID::supportsSSE3()
+{
+ return SSE3 && enableSSE3;
+}
+
+inline bool CPUID::supportsSSSE3()
+{
+ return SSSE3 && enableSSSE3;
+}
+
+inline bool CPUID::supportsSSE4_1()
+{
+ return SSE4_1 && enableSSE4_1;
+}
+
+inline int CPUID::coreCount()
+{
+ return cores;
+}
+
+inline int CPUID::processAffinity()
+{
+ return affinity;
+}
+
+} // namespace sw
+
#endif // sw_CPUID_hpp
diff --git a/src/System/Configurator.cpp b/src/System/Configurator.cpp
index ead1d28..e544383 100644
--- a/src/System/Configurator.cpp
+++ b/src/System/Configurator.cpp
@@ -27,229 +27,230 @@
#include <unistd.h>
#endif
-namespace sw
+namespace sw {
+
+Configurator::Configurator(string iniPath)
{
- Configurator::Configurator(string iniPath)
- {
- path = iniPath;
+ path = iniPath;
- readFile();
- }
+ readFile();
+}
- Configurator::~Configurator()
- {
- }
+Configurator::~Configurator()
+{
+}
- bool Configurator::readFile()
+bool Configurator::readFile()
+{
+ #if defined(__unix__)
+ if(access(path.c_str(), R_OK) != 0)
+ {
+ return false;
+ }
+ #endif
+
+ fstream file(path.c_str(), ios::in);
+ if(file.fail()) return false;
+
+ string line;
+ string keyName;
+
+ while(getline(file, line))
{
- #if defined(__unix__)
- if(access(path.c_str(), R_OK) != 0)
+ if(line.length())
+ {
+ if(line[line.length() - 1] == '\r')
{
+ line = line.substr(0, line.length() - 1);
+ }
+
+ if(!isprint(line[0]))
+ {
+ // printf("Failing on char %d\n", line[0]);
+ file.close();
return false;
}
- #endif
- fstream file(path.c_str(), ios::in);
- if(file.fail()) return false;
+ string::size_type pLeft = line.find_first_of(";#[=");
- string line;
- string keyName;
-
- while(getline(file, line))
- {
- if(line.length())
+ if(pLeft != string::npos)
{
- if(line[line.length() - 1] == '\r')
+ switch(line[pLeft])
{
- line = line.substr(0, line.length() - 1);
- }
-
- if(!isprint(line[0]))
- {
- // printf("Failing on char %d\n", line[0]);
- file.close();
- return false;
- }
-
- string::size_type pLeft = line.find_first_of(";#[=");
-
- if(pLeft != string::npos)
- {
- switch(line[pLeft])
+ case '[':
{
- case '[':
- {
- string::size_type pRight = line.find_last_of("]");
+ string::size_type pRight = line.find_last_of("]");
- if(pRight != string::npos && pRight > pLeft)
- {
- keyName = line.substr(pLeft + 1, pRight - pLeft - 1);
- addKeyName(keyName);
- }
- }
- break;
- case '=':
+ if(pRight != string::npos && pRight > pLeft)
{
- string valueName = line.substr(0, pLeft);
- string value = line.substr(pLeft + 1);
- addValue(keyName, valueName, value);
+ keyName = line.substr(pLeft + 1, pRight - pLeft - 1);
+ addKeyName(keyName);
}
- break;
- case ';':
- case '#':
- // Ignore comments
- break;
}
+ break;
+ case '=':
+ {
+ string valueName = line.substr(0, pLeft);
+ string value = line.substr(pLeft + 1);
+ addValue(keyName, valueName, value);
+ }
+ break;
+ case ';':
+ case '#':
+ // Ignore comments
+ break;
}
}
}
-
- file.close();
-
- if(names.size())
- {
- return true;
- }
-
- return false;
}
- void Configurator::writeFile(std::string title)
+ file.close();
+
+ if(names.size())
{
- #if defined(__unix__)
- if(access(path.c_str(), W_OK) != 0)
- {
- return;
- }
- #endif
-
- fstream file(path.c_str(), ios::out);
- if(file.fail()) return;
-
- file << "; " << title << endl << endl;
-
- for(unsigned int keyID = 0; keyID < sections.size(); keyID++)
- {
- file << "[" << names[keyID] << "]" << endl;
-
- for(unsigned int valueID = 0; valueID < sections[keyID].names.size(); valueID++)
- {
- file << sections[keyID].names[valueID] << "=" << sections[keyID].values[valueID] << endl;
- }
-
- file << endl;
- }
-
- file.close();
+ return true;
}
- int Configurator::findKey(string keyName) const
- {
- for(unsigned int keyID = 0; keyID < names.size(); keyID++)
+ return false;
+}
+
+void Configurator::writeFile(std::string title)
+{
+ #if defined(__unix__)
+ if(access(path.c_str(), W_OK) != 0)
{
- if(names[keyID] == keyName)
- {
- return keyID;
- }
+ return;
+ }
+ #endif
+
+ fstream file(path.c_str(), ios::out);
+ if(file.fail()) return;
+
+ file << "; " << title << endl << endl;
+
+ for(unsigned int keyID = 0; keyID < sections.size(); keyID++)
+ {
+ file << "[" << names[keyID] << "]" << endl;
+
+ for(unsigned int valueID = 0; valueID < sections[keyID].names.size(); valueID++)
+ {
+ file << sections[keyID].names[valueID] << "=" << sections[keyID].values[valueID] << endl;
}
+ file << endl;
+ }
+
+ file.close();
+}
+
+int Configurator::findKey(string keyName) const
+{
+ for(unsigned int keyID = 0; keyID < names.size(); keyID++)
+ {
+ if(names[keyID] == keyName)
+ {
+ return keyID;
+ }
+ }
+
+ return -1;
+}
+
+int Configurator::findValue(unsigned int keyID, string valueName) const
+{
+ if(!sections.size() || keyID >= sections.size())
+ {
return -1;
}
- int Configurator::findValue(unsigned int keyID, string valueName) const
+ for(unsigned int valueID = 0; valueID < sections[keyID].names.size(); ++valueID)
{
- if(!sections.size() || keyID >= sections.size())
+ if(sections[keyID].names[valueID] == valueName)
{
- return -1;
- }
-
- for(unsigned int valueID = 0; valueID < sections[keyID].names.size(); ++valueID)
- {
- if(sections[keyID].names[valueID] == valueName)
- {
- return valueID;
- }
- }
-
- return -1;
- }
-
- unsigned int Configurator::addKeyName(string keyName)
- {
- names.resize(names.size() + 1, keyName);
- sections.resize(sections.size() + 1);
- return (unsigned int)names.size() - 1;
- }
-
- void Configurator::addValue(string const keyName, string const valueName, string const value)
- {
- int keyID = findKey(keyName);
-
- if(keyID == -1)
- {
- keyID = addKeyName(keyName);
- }
-
- int valueID = findValue(keyID, valueName);
-
- if(valueID == -1)
- {
- sections[keyID].names.resize(sections[keyID].names.size() + 1, valueName);
- sections[keyID].values.resize(sections[keyID].values.size() + 1, value);
- }
- else
- {
- sections[keyID].values[valueID] = value;
+ return valueID;
}
}
- string Configurator::getValue(string keyName, string valueName, string defaultValue) const
- {
- int keyID = findKey(keyName);
- if(keyID == -1) return defaultValue;
- int valueID = findValue((unsigned int)keyID, valueName);
- if(valueID == -1) return defaultValue;
+ return -1;
+}
- return sections[keyID].values[valueID];
+unsigned int Configurator::addKeyName(string keyName)
+{
+ names.resize(names.size() + 1, keyName);
+ sections.resize(sections.size() + 1);
+ return (unsigned int)names.size() - 1;
+}
+
+void Configurator::addValue(string const keyName, string const valueName, string const value)
+{
+ int keyID = findKey(keyName);
+
+ if(keyID == -1)
+ {
+ keyID = addKeyName(keyName);
}
- int Configurator::getInteger(string keyName, string valueName, int defaultValue) const
+ int valueID = findValue(keyID, valueName);
+
+ if(valueID == -1)
{
- char svalue[256];
-
- sprintf(svalue, "%d", defaultValue);
-
- return atoi(getValue(keyName, valueName, svalue).c_str());
+ sections[keyID].names.resize(sections[keyID].names.size() + 1, valueName);
+ sections[keyID].values.resize(sections[keyID].values.size() + 1, value);
}
-
- bool Configurator::getBoolean(string keyName, string valueName, bool defaultValue) const
+ else
{
- return getInteger(keyName, valueName, (int)defaultValue) != 0;
- }
-
- double Configurator::getFloat(string keyName, string valueName, double defaultValue) const
- {
- char svalue[256];
-
- sprintf(svalue, "%f", defaultValue);
-
- return atof(getValue(keyName, valueName, svalue).c_str());
- }
-
- unsigned int Configurator::getFormatted(string keyName, string valueName, char *format,
- void *v1, void *v2, void *v3, void *v4,
- void *v5, void *v6, void *v7, void *v8,
- void *v9, void *v10, void *v11, void *v12,
- void *v13, void *v14, void *v15, void *v16)
- {
- string value = getValue(keyName, valueName);
-
- if(!value.length()) return false;
-
- unsigned int nVals = sscanf(value.c_str(), format,
- v1, v2, v3, v4, v5, v6, v7, v8,
- v9, v10, v11, v12, v13, v14, v15, v16);
-
- return nVals;
+ sections[keyID].values[valueID] = value;
}
}
+
+string Configurator::getValue(string keyName, string valueName, string defaultValue) const
+{
+ int keyID = findKey(keyName);
+ if(keyID == -1) return defaultValue;
+ int valueID = findValue((unsigned int)keyID, valueName);
+ if(valueID == -1) return defaultValue;
+
+ return sections[keyID].values[valueID];
+}
+
+int Configurator::getInteger(string keyName, string valueName, int defaultValue) const
+{
+ char svalue[256];
+
+ sprintf(svalue, "%d", defaultValue);
+
+ return atoi(getValue(keyName, valueName, svalue).c_str());
+}
+
+bool Configurator::getBoolean(string keyName, string valueName, bool defaultValue) const
+{
+ return getInteger(keyName, valueName, (int)defaultValue) != 0;
+}
+
+double Configurator::getFloat(string keyName, string valueName, double defaultValue) const
+{
+ char svalue[256];
+
+ sprintf(svalue, "%f", defaultValue);
+
+ return atof(getValue(keyName, valueName, svalue).c_str());
+}
+
+unsigned int Configurator::getFormatted(string keyName, string valueName, char *format,
+ void *v1, void *v2, void *v3, void *v4,
+ void *v5, void *v6, void *v7, void *v8,
+ void *v9, void *v10, void *v11, void *v12,
+ void *v13, void *v14, void *v15, void *v16)
+{
+ string value = getValue(keyName, valueName);
+
+ if(!value.length()) return false;
+
+ unsigned int nVals = sscanf(value.c_str(), format,
+ v1, v2, v3, v4, v5, v6, v7, v8,
+ v9, v10, v11, v12, v13, v14, v15, v16);
+
+ return nVals;
+}
+
+} // namespace sw
diff --git a/src/System/Configurator.hpp b/src/System/Configurator.hpp
index 6fd930c..9a27a39 100644
--- a/src/System/Configurator.hpp
+++ b/src/System/Configurator.hpp
@@ -20,47 +20,48 @@
#include <stdlib.h>
-namespace sw
+namespace sw {
+
+class Configurator
{
- class Configurator
+public:
+ Configurator(std::string iniPath = "");
+
+ ~Configurator();
+
+ std::string getValue(std::string sectionName, std::string valueName, std::string defaultValue = "") const;
+ int getInteger(std::string sectionName, std::string valueName, int defaultValue = 0) const;
+ bool getBoolean(std::string sectionName, std::string valueName, bool defaultValue = false) const;
+ double getFloat(std::string sectionName, std::string valueName, double defaultValue = 0.0) const;
+ unsigned int getFormatted(std::string sectionName, std::string valueName, char *format,
+ void *v1 = 0, void *v2 = 0, void *v3 = 0, void *v4 = 0,
+ void *v5 = 0, void *v6 = 0, void *v7 = 0, void *v8 = 0,
+ void *v9 = 0, void *v10 = 0, void *v11 = 0, void *v12 = 0,
+ void *v13 = 0, void *v14 = 0, void *v15 = 0, void *v16 = 0);
+
+ void addValue(std::string sectionName, std::string valueName, std::string value);
+
+ void writeFile(std::string title = "Configuration File");
+
+private:
+ bool readFile();
+
+ unsigned int addKeyName(std::string sectionName);
+ int findKey(std::string sectionName) const;
+ int findValue(unsigned int sectionID, std::string valueName) const;
+
+ std::string path;
+
+ struct Section
{
- public:
- Configurator(std::string iniPath = "");
-
- ~Configurator();
-
- std::string getValue(std::string sectionName, std::string valueName, std::string defaultValue = "") const;
- int getInteger(std::string sectionName, std::string valueName, int defaultValue = 0) const;
- bool getBoolean(std::string sectionName, std::string valueName, bool defaultValue = false) const;
- double getFloat(std::string sectionName, std::string valueName, double defaultValue = 0.0) const;
- unsigned int getFormatted(std::string sectionName, std::string valueName, char *format,
- void *v1 = 0, void *v2 = 0, void *v3 = 0, void *v4 = 0,
- void *v5 = 0, void *v6 = 0, void *v7 = 0, void *v8 = 0,
- void *v9 = 0, void *v10 = 0, void *v11 = 0, void *v12 = 0,
- void *v13 = 0, void *v14 = 0, void *v15 = 0, void *v16 = 0);
-
- void addValue(std::string sectionName, std::string valueName, std::string value);
-
- void writeFile(std::string title = "Configuration File");
-
- private:
- bool readFile();
-
- unsigned int addKeyName(std::string sectionName);
- int findKey(std::string sectionName) const;
- int findValue(unsigned int sectionID, std::string valueName) const;
-
- std::string path;
-
- struct Section
- {
- std::vector<std::string> names;
- std::vector<std::string> values;
- };
-
- std::vector<Section> sections;
std::vector<std::string> names;
+ std::vector<std::string> values;
};
-}
+
+ std::vector<Section> sections;
+ std::vector<std::string> names;
+};
+
+} // namespace sw
#endif // sw_Configurator_hpp
diff --git a/src/System/Debug.cpp b/src/System/Debug.cpp
index acf469e..dd66d56 100644
--- a/src/System/Debug.cpp
+++ b/src/System/Debug.cpp
@@ -17,8 +17,8 @@
#include <stdio.h>
#include <stdarg.h>
-namespace sw
-{
+namespace sw {
+
void trace(const char *format, ...)
{
if(false)
@@ -36,4 +36,5 @@
}
}
}
-}
+
+} // namespace sw
diff --git a/src/System/Debug.hpp b/src/System/Debug.hpp
index 0c862d4..d26623c 100644
--- a/src/System/Debug.hpp
+++ b/src/System/Debug.hpp
@@ -25,10 +25,11 @@
#undef min
#undef max
-namespace sw
-{
+namespace sw {
+
void trace(const char *format, ...);
inline void trace() {}
+
}
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
diff --git a/src/System/Half.cpp b/src/System/Half.cpp
index cde8190..4f8f7cd 100644
--- a/src/System/Half.cpp
+++ b/src/System/Half.cpp
@@ -14,89 +14,90 @@
#include "Half.hpp"
-namespace sw
+namespace sw {
+
+half::half(float fp32)
{
- half::half(float fp32)
+ unsigned int fp32i = *(unsigned int*)&fp32;
+ unsigned int sign = (fp32i & 0x80000000) >> 16;
+ unsigned int abs = fp32i & 0x7FFFFFFF;
+
+ if(abs > 0x47FFEFFF) // Infinity
{
- unsigned int fp32i = *(unsigned int*)&fp32;
- unsigned int sign = (fp32i & 0x80000000) >> 16;
- unsigned int abs = fp32i & 0x7FFFFFFF;
+ fp16i = sign | 0x7FFF;
+ }
+ else if(abs < 0x38800000) // Denormal
+ {
+ unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
+ int e = 113 - (abs >> 23);
- if(abs > 0x47FFEFFF) // Infinity
+ if(e < 24)
{
- fp16i = sign | 0x7FFF;
- }
- else if(abs < 0x38800000) // Denormal
- {
- unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
- int e = 113 - (abs >> 23);
-
- if(e < 24)
- {
- abs = mantissa >> e;
- }
- else
- {
- abs = 0;
- }
-
- fp16i = sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
+ abs = mantissa >> e;
}
else
{
- fp16i = sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
- }
- }
-
- half::operator float() const
- {
- unsigned int fp32i;
-
- int s = (fp16i >> 15) & 0x00000001;
- int e = (fp16i >> 10) & 0x0000001F;
- int m = fp16i & 0x000003FF;
-
- if(e == 0)
- {
- if(m == 0)
- {
- fp32i = s << 31;
-
- return (float&)fp32i;
- }
- else
- {
- while(!(m & 0x00000400))
- {
- m <<= 1;
- e -= 1;
- }
-
- e += 1;
- m &= ~0x00000400;
- }
+ abs = 0;
}
- e = e + (127 - 15);
- m = m << 13;
-
- fp32i = (s << 31) | (e << 23) | m;
-
- return (float&)fp32i;
+ fp16i = sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
}
-
- half &half::operator=(half h)
+ else
{
- fp16i = h.fp16i;
-
- return *this;
- }
-
-
- half &half::operator=(float f)
- {
- *this = half(f);
-
- return *this;
+ fp16i = sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
}
}
+
+half::operator float() const
+{
+ unsigned int fp32i;
+
+ int s = (fp16i >> 15) & 0x00000001;
+ int e = (fp16i >> 10) & 0x0000001F;
+ int m = fp16i & 0x000003FF;
+
+ if(e == 0)
+ {
+ if(m == 0)
+ {
+ fp32i = s << 31;
+
+ return (float&)fp32i;
+ }
+ else
+ {
+ while(!(m & 0x00000400))
+ {
+ m <<= 1;
+ e -= 1;
+ }
+
+ e += 1;
+ m &= ~0x00000400;
+ }
+ }
+
+ e = e + (127 - 15);
+ m = m << 13;
+
+ fp32i = (s << 31) | (e << 23) | m;
+
+ return (float&)fp32i;
+}
+
+half &half::operator=(half h)
+{
+ fp16i = h.fp16i;
+
+ return *this;
+}
+
+
+half &half::operator=(float f)
+{
+ *this = half(f);
+
+ return *this;
+}
+
+} // namespace sw
diff --git a/src/System/Half.hpp b/src/System/Half.hpp
index e77224d..2831ca2 100644
--- a/src/System/Half.hpp
+++ b/src/System/Half.hpp
@@ -20,293 +20,294 @@
#include <algorithm>
#include <cmath>
-namespace sw
+namespace sw {
+
+class half
{
- class half
+public:
+ half() = default;
+ explicit half(float f);
+
+ operator float() const;
+
+ half &operator=(half h);
+ half &operator=(float f);
+
+private:
+ unsigned short fp16i;
+};
+
+inline half shortAsHalf(short s)
+{
+ union
{
- public:
- half() = default;
- explicit half(float f);
+ half h;
+ short s;
+ } hs;
- operator float() const;
+ hs.s = s;
- half &operator=(half h);
- half &operator=(float f);
+ return hs.h;
+}
- private:
- unsigned short fp16i;
- };
+class RGB9E5
+{
+ unsigned int R : 9;
+ unsigned int G : 9;
+ unsigned int B : 9;
+ unsigned int E : 5;
- inline half shortAsHalf(short s)
+public:
+ RGB9E5(float rgb[3]) : RGB9E5(rgb[0], rgb[1], rgb[2])
{
- union
- {
- half h;
- short s;
- } hs;
-
- hs.s = s;
-
- return hs.h;
}
- class RGB9E5
+ RGB9E5(float r, float g, float b)
{
- unsigned int R : 9;
- unsigned int G : 9;
- unsigned int B : 9;
- unsigned int E : 5;
+ // Vulkan 1.1.117 section 15.2.1 RGB to Shared Exponent Conversion
- public:
- RGB9E5(float rgb[3]) : RGB9E5(rgb[0], rgb[1], rgb[2])
- {
- }
+ // B is the exponent bias (15)
+ constexpr int g_sharedexp_bias = 15;
- RGB9E5(float r, float g, float b)
- {
- // Vulkan 1.1.117 section 15.2.1 RGB to Shared Exponent Conversion
+ // N is the number of mantissa bits per component (9)
+ constexpr int g_sharedexp_mantissabits = 9;
- // B is the exponent bias (15)
- constexpr int g_sharedexp_bias = 15;
+ // Emax is the maximum allowed biased exponent value (31)
+ constexpr int g_sharedexp_maxexponent = 31;
- // N is the number of mantissa bits per component (9)
- constexpr int g_sharedexp_mantissabits = 9;
+ constexpr float g_sharedexp_max =
+ ((static_cast<float>(1 << g_sharedexp_mantissabits) - 1) /
+ static_cast<float>(1 << g_sharedexp_mantissabits)) *
+ static_cast<float>(1 << (g_sharedexp_maxexponent - g_sharedexp_bias));
- // Emax is the maximum allowed biased exponent value (31)
- constexpr int g_sharedexp_maxexponent = 31;
+ // Clamp components to valid range. NaN becomes 0.
+ const float red_c = std::min(!(r > 0) ? 0 : r, g_sharedexp_max);
+ const float green_c = std::min(!(g > 0) ? 0 : g, g_sharedexp_max);
+ const float blue_c = std::min(!(b > 0) ? 0 : b, g_sharedexp_max);
- constexpr float g_sharedexp_max =
- ((static_cast<float>(1 << g_sharedexp_mantissabits) - 1) /
- static_cast<float>(1 << g_sharedexp_mantissabits)) *
- static_cast<float>(1 << (g_sharedexp_maxexponent - g_sharedexp_bias));
+ // We're reducing the mantissa to 9 bits, so we must round up if the next
+ // bit is 1. In other words add 0.5 to the new mantissa's position and
+ // allow overflow into the exponent so we can scale correctly.
+ constexpr int half = 1 << (23 - g_sharedexp_mantissabits);
+ const float red_r = bit_cast<float>(bit_cast<int>(red_c) + half);
+ const float green_r = bit_cast<float>(bit_cast<int>(green_c) + half);
+ const float blue_r = bit_cast<float>(bit_cast<int>(blue_c) + half);
- // Clamp components to valid range. NaN becomes 0.
- const float red_c = std::min(!(r > 0) ? 0 : r, g_sharedexp_max);
- const float green_c = std::min(!(g > 0) ? 0 : g, g_sharedexp_max);
- const float blue_c = std::min(!(b > 0) ? 0 : b, g_sharedexp_max);
+ // The largest component determines the shared exponent. It can't be lower
+ // than 0 (after bias subtraction) so also limit to the mimimum representable.
+ constexpr float min_s = 0.5f / (1 << g_sharedexp_bias);
+ float max_s = std::max(std::max(red_r, green_r), std::max(blue_r, min_s));
- // We're reducing the mantissa to 9 bits, so we must round up if the next
- // bit is 1. In other words add 0.5 to the new mantissa's position and
- // allow overflow into the exponent so we can scale correctly.
- constexpr int half = 1 << (23 - g_sharedexp_mantissabits);
- const float red_r = bit_cast<float>(bit_cast<int>(red_c) + half);
- const float green_r = bit_cast<float>(bit_cast<int>(green_c) + half);
- const float blue_r = bit_cast<float>(bit_cast<int>(blue_c) + half);
+ // Obtain the reciprocal of the shared exponent by inverting the bits,
+ // and scale by the new mantissa's size. Note that the IEEE-754 single-precision
+ // format has an implicit leading 1, but this shared component format does not.
+ float scale = bit_cast<float>((bit_cast<int>(max_s) & 0x7F800000) ^ 0x7F800000) * (1 << (g_sharedexp_mantissabits - 2));
- // The largest component determines the shared exponent. It can't be lower
- // than 0 (after bias subtraction) so also limit to the mimimum representable.
- constexpr float min_s = 0.5f / (1 << g_sharedexp_bias);
- float max_s = std::max(std::max(red_r, green_r), std::max(blue_r, min_s));
+ R = static_cast<unsigned int>(round(red_c * scale));
+ G = static_cast<unsigned int>(round(green_c * scale));
+ B = static_cast<unsigned int>(round(blue_c * scale));
+ E = (bit_cast<unsigned int>(max_s) >> 23) - 127 + 15 + 1;
+ }
- // Obtain the reciprocal of the shared exponent by inverting the bits,
- // and scale by the new mantissa's size. Note that the IEEE-754 single-precision
- // format has an implicit leading 1, but this shared component format does not.
- float scale = bit_cast<float>((bit_cast<int>(max_s) & 0x7F800000) ^ 0x7F800000) * (1 << (g_sharedexp_mantissabits - 2));
-
- R = static_cast<unsigned int>(round(red_c * scale));
- G = static_cast<unsigned int>(round(green_c * scale));
- B = static_cast<unsigned int>(round(blue_c * scale));
- E = (bit_cast<unsigned int>(max_s) >> 23) - 127 + 15 + 1;
- }
-
- operator unsigned int() const
- {
- return *reinterpret_cast<const unsigned int*>(this);
- }
-
- void toRGB16F(half rgb[3]) const
- {
- constexpr int offset = 24; // Exponent bias (15) + number of mantissa bits per component (9) = 24
-
- const float factor = (1u << E) * (1.0f / (1 << offset));
- rgb[0] = half(R * factor);
- rgb[1] = half(G * factor);
- rgb[2] = half(B * factor);
- }
- };
-
- class R11G11B10F
+ operator unsigned int() const
{
- unsigned int R : 11;
- unsigned int G : 11;
- unsigned int B : 10;
+ return *reinterpret_cast<const unsigned int*>(this);
+ }
- static inline half float11ToFloat16(unsigned short fp11)
+ void toRGB16F(half rgb[3]) const
+ {
+ constexpr int offset = 24; // Exponent bias (15) + number of mantissa bits per component (9) = 24
+
+ const float factor = (1u << E) * (1.0f / (1 << offset));
+ rgb[0] = half(R * factor);
+ rgb[1] = half(G * factor);
+ rgb[2] = half(B * factor);
+ }
+};
+
+class R11G11B10F
+{
+ unsigned int R : 11;
+ unsigned int G : 11;
+ unsigned int B : 10;
+
+ static inline half float11ToFloat16(unsigned short fp11)
+ {
+ return shortAsHalf(fp11 << 4); // Sign bit 0
+ }
+
+ static inline half float10ToFloat16(unsigned short fp10)
+ {
+ return shortAsHalf(fp10 << 5); // Sign bit 0
+ }
+
+ inline unsigned short float32ToFloat11(float fp32)
+ {
+ const unsigned int float32MantissaMask = 0x7FFFFF;
+ const unsigned int float32ExponentMask = 0x7F800000;
+ const unsigned int float32SignMask = 0x80000000;
+ const unsigned int float32ValueMask = ~float32SignMask;
+ const unsigned int float32ExponentFirstBit = 23;
+ const unsigned int float32ExponentBias = 127;
+
+ const unsigned short float11Max = 0x7BF;
+ const unsigned short float11MantissaMask = 0x3F;
+ const unsigned short float11ExponentMask = 0x7C0;
+ const unsigned short float11BitMask = 0x7FF;
+ const unsigned int float11ExponentBias = 14;
+
+ const unsigned int float32Maxfloat11 = 0x477E0000;
+ const unsigned int float32Minfloat11 = 0x38800000;
+
+ const unsigned int float32Bits = *reinterpret_cast<unsigned int*>(&fp32);
+ const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
+
+ unsigned int float32Val = float32Bits & float32ValueMask;
+
+ if((float32Val & float32ExponentMask) == float32ExponentMask)
{
- return shortAsHalf(fp11 << 4); // Sign bit 0
- }
-
- static inline half float10ToFloat16(unsigned short fp10)
- {
- return shortAsHalf(fp10 << 5); // Sign bit 0
- }
-
- inline unsigned short float32ToFloat11(float fp32)
- {
- const unsigned int float32MantissaMask = 0x7FFFFF;
- const unsigned int float32ExponentMask = 0x7F800000;
- const unsigned int float32SignMask = 0x80000000;
- const unsigned int float32ValueMask = ~float32SignMask;
- const unsigned int float32ExponentFirstBit = 23;
- const unsigned int float32ExponentBias = 127;
-
- const unsigned short float11Max = 0x7BF;
- const unsigned short float11MantissaMask = 0x3F;
- const unsigned short float11ExponentMask = 0x7C0;
- const unsigned short float11BitMask = 0x7FF;
- const unsigned int float11ExponentBias = 14;
-
- const unsigned int float32Maxfloat11 = 0x477E0000;
- const unsigned int float32Minfloat11 = 0x38800000;
-
- const unsigned int float32Bits = *reinterpret_cast<unsigned int*>(&fp32);
- const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
-
- unsigned int float32Val = float32Bits & float32ValueMask;
-
- if((float32Val & float32ExponentMask) == float32ExponentMask)
+ // INF or NAN
+ if((float32Val & float32MantissaMask) != 0)
{
- // INF or NAN
- if((float32Val & float32MantissaMask) != 0)
- {
- return float11ExponentMask |
- (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) &
- float11MantissaMask);
- }
- else if(float32Sign)
- {
- // -INF is clamped to 0 since float11 is positive only
- return 0;
- }
- else
- {
- return float11ExponentMask;
- }
+ return float11ExponentMask |
+ (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) &
+ float11MantissaMask);
}
else if(float32Sign)
{
- // float11 is positive only, so clamp to zero
+ // -INF is clamped to 0 since float11 is positive only
return 0;
}
- else if(float32Val > float32Maxfloat11)
- {
- // The number is too large to be represented as a float11, set to max
- return float11Max;
- }
else
{
- if(float32Val < float32Minfloat11)
- {
- // The number is too small to be represented as a normalized float11
- // Convert it to a denormalized value.
- const unsigned int shift = (float32ExponentBias - float11ExponentBias) -
- (float32Val >> float32ExponentFirstBit);
- float32Val =
- ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
- }
- else
- {
- // Rebias the exponent to represent the value as a normalized float11
- float32Val += 0xC8000000;
- }
-
- return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask;
+ return float11ExponentMask;
}
}
-
- inline unsigned short float32ToFloat10(float fp32)
+ else if(float32Sign)
{
- const unsigned int float32MantissaMask = 0x7FFFFF;
- const unsigned int float32ExponentMask = 0x7F800000;
- const unsigned int float32SignMask = 0x80000000;
- const unsigned int float32ValueMask = ~float32SignMask;
- const unsigned int float32ExponentFirstBit = 23;
- const unsigned int float32ExponentBias = 127;
-
- const unsigned short float10Max = 0x3DF;
- const unsigned short float10MantissaMask = 0x1F;
- const unsigned short float10ExponentMask = 0x3E0;
- const unsigned short float10BitMask = 0x3FF;
- const unsigned int float10ExponentBias = 14;
-
- const unsigned int float32Maxfloat10 = 0x477C0000;
- const unsigned int float32Minfloat10 = 0x38800000;
-
- const unsigned int float32Bits = *reinterpret_cast<unsigned int*>(&fp32);
- const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
-
- unsigned int float32Val = float32Bits & float32ValueMask;
-
- if((float32Val & float32ExponentMask) == float32ExponentMask)
+ // float11 is positive only, so clamp to zero
+ return 0;
+ }
+ else if(float32Val > float32Maxfloat11)
+ {
+ // The number is too large to be represented as a float11, set to max
+ return float11Max;
+ }
+ else
+ {
+ if(float32Val < float32Minfloat11)
{
- // INF or NAN
- if((float32Val & float32MantissaMask) != 0)
- {
- return float10ExponentMask |
- (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) &
- float10MantissaMask);
- }
- else if(float32Sign)
- {
- // -INF is clamped to 0 since float11 is positive only
- return 0;
- }
- else
- {
- return float10ExponentMask;
- }
+ // The number is too small to be represented as a normalized float11
+ // Convert it to a denormalized value.
+ const unsigned int shift = (float32ExponentBias - float11ExponentBias) -
+ (float32Val >> float32ExponentFirstBit);
+ float32Val =
+ ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
+ }
+ else
+ {
+ // Rebias the exponent to represent the value as a normalized float11
+ float32Val += 0xC8000000;
+ }
+
+ return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask;
+ }
+ }
+
+ inline unsigned short float32ToFloat10(float fp32)
+ {
+ const unsigned int float32MantissaMask = 0x7FFFFF;
+ const unsigned int float32ExponentMask = 0x7F800000;
+ const unsigned int float32SignMask = 0x80000000;
+ const unsigned int float32ValueMask = ~float32SignMask;
+ const unsigned int float32ExponentFirstBit = 23;
+ const unsigned int float32ExponentBias = 127;
+
+ const unsigned short float10Max = 0x3DF;
+ const unsigned short float10MantissaMask = 0x1F;
+ const unsigned short float10ExponentMask = 0x3E0;
+ const unsigned short float10BitMask = 0x3FF;
+ const unsigned int float10ExponentBias = 14;
+
+ const unsigned int float32Maxfloat10 = 0x477C0000;
+ const unsigned int float32Minfloat10 = 0x38800000;
+
+ const unsigned int float32Bits = *reinterpret_cast<unsigned int*>(&fp32);
+ const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
+
+ unsigned int float32Val = float32Bits & float32ValueMask;
+
+ if((float32Val & float32ExponentMask) == float32ExponentMask)
+ {
+ // INF or NAN
+ if((float32Val & float32MantissaMask) != 0)
+ {
+ return float10ExponentMask |
+ (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) &
+ float10MantissaMask);
}
else if(float32Sign)
{
- // float10 is positive only, so clamp to zero
+ // -INF is clamped to 0 since float11 is positive only
return 0;
}
- else if(float32Val > float32Maxfloat10)
- {
- // The number is too large to be represented as a float11, set to max
- return float10Max;
- }
else
{
- if(float32Val < float32Minfloat10)
- {
- // The number is too small to be represented as a normalized float11
- // Convert it to a denormalized value.
- const unsigned int shift = (float32ExponentBias - float10ExponentBias) -
- (float32Val >> float32ExponentFirstBit);
- float32Val =
- ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
- }
- else
- {
- // Rebias the exponent to represent the value as a normalized float11
- float32Val += 0xC8000000;
- }
-
- return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask;
+ return float10ExponentMask;
}
}
-
- public:
- R11G11B10F(float rgb[3])
+ else if(float32Sign)
{
- R = float32ToFloat11(rgb[0]);
- G = float32ToFloat11(rgb[1]);
- B = float32ToFloat10(rgb[2]);
+ // float10 is positive only, so clamp to zero
+ return 0;
}
-
- operator unsigned int() const
+ else if(float32Val > float32Maxfloat10)
{
- return *reinterpret_cast<const unsigned int*>(this);
+ // The number is too large to be represented as a float11, set to max
+ return float10Max;
}
-
- void toRGB16F(half rgb[3]) const
+ else
{
- rgb[0] = float11ToFloat16(R);
- rgb[1] = float11ToFloat16(G);
- rgb[2] = float10ToFloat16(B);
+ if(float32Val < float32Minfloat10)
+ {
+ // The number is too small to be represented as a normalized float11
+ // Convert it to a denormalized value.
+ const unsigned int shift = (float32ExponentBias - float10ExponentBias) -
+ (float32Val >> float32ExponentFirstBit);
+ float32Val =
+ ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
+ }
+ else
+ {
+ // Rebias the exponent to represent the value as a normalized float11
+ float32Val += 0xC8000000;
+ }
+
+ return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask;
}
- };
-}
+ }
+
+public:
+ R11G11B10F(float rgb[3])
+ {
+ R = float32ToFloat11(rgb[0]);
+ G = float32ToFloat11(rgb[1]);
+ B = float32ToFloat10(rgb[2]);
+ }
+
+ operator unsigned int() const
+ {
+ return *reinterpret_cast<const unsigned int*>(this);
+ }
+
+ void toRGB16F(half rgb[3]) const
+ {
+ rgb[0] = float11ToFloat16(R);
+ rgb[1] = float11ToFloat16(G);
+ rgb[2] = float10ToFloat16(B);
+ }
+};
+
+} // namespace sw
#endif // sw_Half_hpp
diff --git a/src/System/Math.cpp b/src/System/Math.cpp
index 290d4ab..36d1df2 100644
--- a/src/System/Math.cpp
+++ b/src/System/Math.cpp
@@ -14,36 +14,37 @@
#include "Math.hpp"
-namespace sw
+namespace sw {
+
+inline uint64_t FNV_1a(uint64_t hash, unsigned char data)
{
- inline uint64_t FNV_1a(uint64_t hash, unsigned char data)
- {
- return (hash ^ data) * 1099511628211;
- }
-
- uint64_t FNV_1a(const unsigned char *data, int size)
- {
- int64_t hash = 0xCBF29CE484222325;
-
- for(int i = 0; i < size; i++)
- {
- hash = FNV_1a(hash, data[i]);
- }
-
- return hash;
- }
-
- unsigned char sRGB8toLinear8(unsigned char value)
- {
- static unsigned char sRGBtoLinearTable[256] = { 255 };
- if(sRGBtoLinearTable[0] == 255)
- {
- for(int i = 0; i < 256; i++)
- {
- sRGBtoLinearTable[i] = static_cast<unsigned char>(sw::sRGBtoLinear(static_cast<float>(i) / 255.0f) * 255.0f + 0.5f);
- }
- }
-
- return sRGBtoLinearTable[value];
- }
+ return (hash ^ data) * 1099511628211;
}
+
+uint64_t FNV_1a(const unsigned char *data, int size)
+{
+ int64_t hash = 0xCBF29CE484222325;
+
+ for(int i = 0; i < size; i++)
+ {
+ hash = FNV_1a(hash, data[i]);
+ }
+
+ return hash;
+}
+
+unsigned char sRGB8toLinear8(unsigned char value)
+{
+ static unsigned char sRGBtoLinearTable[256] = { 255 };
+ if(sRGBtoLinearTable[0] == 255)
+ {
+ for(int i = 0; i < 256; i++)
+ {
+ sRGBtoLinearTable[i] = static_cast<unsigned char>(sw::sRGBtoLinear(static_cast<float>(i) / 255.0f) * 255.0f + 0.5f);
+ }
+ }
+
+ return sRGBtoLinearTable[value];
+}
+
+} // namespace sw
diff --git a/src/System/Math.hpp b/src/System/Math.hpp
index efef5fd..f45f933 100644
--- a/src/System/Math.hpp
+++ b/src/System/Math.hpp
@@ -24,359 +24,360 @@
#include <intrin.h>
#endif
-namespace sw
+namespace sw {
+
+using std::abs;
+
+#undef min
+#undef max
+
+template<class T>
+inline T constexpr max(T a, T b)
{
- using std::abs;
+ return a > b ? a : b;
+}
- #undef min
- #undef max
+template<class T>
+inline constexpr T min(T a, T b)
+{
+ return a < b ? a : b;
+}
- template<class T>
- inline T constexpr max(T a, T b)
+template<class T>
+inline constexpr T max(T a, T b, T c)
+{
+ return max(max(a, b), c);
+}
+
+template<class T>
+inline constexpr T min(T a, T b, T c)
+{
+ return min(min(a, b), c);
+}
+
+template<class T>
+inline constexpr T max(T a, T b, T c, T d)
+{
+ return max(max(a, b), max(c, d));
+}
+
+template<class T>
+inline constexpr T min(T a, T b, T c, T d)
+{
+ return min(min(a, b), min(c, d));
+}
+
+template <typename destType, typename sourceType>
+destType bit_cast(const sourceType &source)
+{
+ union
{
- return a > b ? a : b;
+ sourceType s;
+ destType d;
+ } sd;
+ sd.s = source;
+ return sd.d;
+}
+
+inline int iround(float x)
+{
+ return (int)floor(x + 0.5f);
+// return _mm_cvtss_si32(_mm_load_ss(&x)); // FIXME: Demands SSE support
+}
+
+inline int ifloor(float x)
+{
+ return (int)floor(x);
+}
+
+inline int ceilFix4(int x)
+{
+ return (x + 0xF) & 0xFFFFFFF0;
+}
+
+inline int ceilInt4(int x)
+{
+ return (x + 0xF) >> 4;
+}
+
+#define BITS(x) ( \
+!!((x) & 0x80000000) + \
+!!((x) & 0xC0000000) + \
+!!((x) & 0xE0000000) + \
+!!((x) & 0xF0000000) + \
+!!((x) & 0xF8000000) + \
+!!((x) & 0xFC000000) + \
+!!((x) & 0xFE000000) + \
+!!((x) & 0xFF000000) + \
+!!((x) & 0xFF800000) + \
+!!((x) & 0xFFC00000) + \
+!!((x) & 0xFFE00000) + \
+!!((x) & 0xFFF00000) + \
+!!((x) & 0xFFF80000) + \
+!!((x) & 0xFFFC0000) + \
+!!((x) & 0xFFFE0000) + \
+!!((x) & 0xFFFF0000) + \
+!!((x) & 0xFFFF8000) + \
+!!((x) & 0xFFFFC000) + \
+!!((x) & 0xFFFFE000) + \
+!!((x) & 0xFFFFF000) + \
+!!((x) & 0xFFFFF800) + \
+!!((x) & 0xFFFFFC00) + \
+!!((x) & 0xFFFFFE00) + \
+!!((x) & 0xFFFFFF00) + \
+!!((x) & 0xFFFFFF80) + \
+!!((x) & 0xFFFFFFC0) + \
+!!((x) & 0xFFFFFFE0) + \
+!!((x) & 0xFFFFFFF0) + \
+!!((x) & 0xFFFFFFF8) + \
+!!((x) & 0xFFFFFFFC) + \
+!!((x) & 0xFFFFFFFE) + \
+!!((x) & 0xFFFFFFFF))
+
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+
+inline unsigned long log2i(int x)
+{
+ #if defined(_MSC_VER)
+ unsigned long y;
+ _BitScanReverse(&y, x);
+ return y;
+ #else
+ return 31 - __builtin_clz(x);
+ #endif
+}
+
+inline bool isPow2(int x)
+{
+ return (x & -x) == x;
+}
+
+template<class T>
+inline T clamp(T x, T a, T b)
+{
+ ASSERT(a <= b);
+ if(x < a) x = a;
+ if(x > b) x = b;
+
+ return x;
+}
+
+inline float clamp01(float x)
+{
+ return clamp(x, 0.0f, 1.0f);
+}
+
+// Bit-cast of a floating-point value into a two's complement integer representation.
+// This makes floating-point values comparable as integers.
+inline int32_t float_as_twos_complement(float f)
+{
+ // IEEE-754 floating-point numbers are sorted by magnitude in the same way as integers,
+ // except negative values are like one's complement integers. Convert them to two's complement.
+ int32_t i = bit_cast<int32_t>(f);
+ return (i < 0) ? (0x7FFFFFFFu - i) : i;
+}
+
+// 'Safe' clamping operation which always returns a value between min and max (inclusive).
+inline float clamp_s(float x, float min, float max)
+{
+ // NaN values can't be compared directly
+ if(float_as_twos_complement(x) < float_as_twos_complement(min)) x = min;
+ if(float_as_twos_complement(x) > float_as_twos_complement(max)) x = max;
+
+ return x;
+}
+
+inline int ceilPow2(int x)
+{
+ int i = 1;
+
+ while(i < x)
+ {
+ i <<= 1;
}
- template<class T>
- inline constexpr T min(T a, T b)
+ return i;
+}
+
+inline int floorDiv(int a, int b)
+{
+ return a / b + ((a % b) >> 31);
+}
+
+inline int floorMod(int a, int b)
+{
+ int r = a % b;
+ return r + ((r >> 31) & b);
+}
+
+inline int ceilDiv(int a, int b)
+{
+ return a / b - (-(a % b) >> 31);
+}
+
+inline int ceilMod(int a, int b)
+{
+ int r = a % b;
+ return r - ((-r >> 31) & b);
+}
+
+template<const int n>
+inline unsigned int unorm(float x)
+{
+ static const unsigned int max = 0xFFFFFFFF >> (32 - n);
+ static const float maxf = static_cast<float>(max);
+
+ if(x >= 1.0f)
{
- return a < b ? a : b;
+ return max;
}
-
- template<class T>
- inline constexpr T max(T a, T b, T c)
+ else if(x <= 0.0f)
{
- return max(max(a, b), c);
+ return 0;
}
-
- template<class T>
- inline constexpr T min(T a, T b, T c)
+ else
{
- return min(min(a, b), c);
+ return static_cast<unsigned int>(maxf * x + 0.5f);
}
+}
- template<class T>
- inline constexpr T max(T a, T b, T c, T d)
+template<const int n>
+inline int snorm(float x)
+{
+ static const unsigned int min = 0x80000000 >> (32 - n);
+ static const unsigned int max = 0xFFFFFFFF >> (32 - n + 1);
+ static const float maxf = static_cast<float>(max);
+ static const unsigned int range = 0xFFFFFFFF >> (32 - n);
+
+ if(x >= 0.0f)
{
- return max(max(a, b), max(c, d));
- }
-
- template<class T>
- inline constexpr T min(T a, T b, T c, T d)
- {
- return min(min(a, b), min(c, d));
- }
-
- template <typename destType, typename sourceType>
- destType bit_cast(const sourceType &source)
- {
- union
- {
- sourceType s;
- destType d;
- } sd;
- sd.s = source;
- return sd.d;
- }
-
- inline int iround(float x)
- {
- return (int)floor(x + 0.5f);
- // return _mm_cvtss_si32(_mm_load_ss(&x)); // FIXME: Demands SSE support
- }
-
- inline int ifloor(float x)
- {
- return (int)floor(x);
- }
-
- inline int ceilFix4(int x)
- {
- return (x + 0xF) & 0xFFFFFFF0;
- }
-
- inline int ceilInt4(int x)
- {
- return (x + 0xF) >> 4;
- }
-
- #define BITS(x) ( \
- !!((x) & 0x80000000) + \
- !!((x) & 0xC0000000) + \
- !!((x) & 0xE0000000) + \
- !!((x) & 0xF0000000) + \
- !!((x) & 0xF8000000) + \
- !!((x) & 0xFC000000) + \
- !!((x) & 0xFE000000) + \
- !!((x) & 0xFF000000) + \
- !!((x) & 0xFF800000) + \
- !!((x) & 0xFFC00000) + \
- !!((x) & 0xFFE00000) + \
- !!((x) & 0xFFF00000) + \
- !!((x) & 0xFFF80000) + \
- !!((x) & 0xFFFC0000) + \
- !!((x) & 0xFFFE0000) + \
- !!((x) & 0xFFFF0000) + \
- !!((x) & 0xFFFF8000) + \
- !!((x) & 0xFFFFC000) + \
- !!((x) & 0xFFFFE000) + \
- !!((x) & 0xFFFFF000) + \
- !!((x) & 0xFFFFF800) + \
- !!((x) & 0xFFFFFC00) + \
- !!((x) & 0xFFFFFE00) + \
- !!((x) & 0xFFFFFF00) + \
- !!((x) & 0xFFFFFF80) + \
- !!((x) & 0xFFFFFFC0) + \
- !!((x) & 0xFFFFFFE0) + \
- !!((x) & 0xFFFFFFF0) + \
- !!((x) & 0xFFFFFFF8) + \
- !!((x) & 0xFFFFFFFC) + \
- !!((x) & 0xFFFFFFFE) + \
- !!((x) & 0xFFFFFFFF))
-
- #define MAX(x, y) ((x) > (y) ? (x) : (y))
- #define MIN(x, y) ((x) < (y) ? (x) : (y))
-
- inline unsigned long log2i(int x)
- {
- #if defined(_MSC_VER)
- unsigned long y;
- _BitScanReverse(&y, x);
- return y;
- #else
- return 31 - __builtin_clz(x);
- #endif
- }
-
- inline bool isPow2(int x)
- {
- return (x & -x) == x;
- }
-
- template<class T>
- inline T clamp(T x, T a, T b)
- {
- ASSERT(a <= b);
- if(x < a) x = a;
- if(x > b) x = b;
-
- return x;
- }
-
- inline float clamp01(float x)
- {
- return clamp(x, 0.0f, 1.0f);
- }
-
- // Bit-cast of a floating-point value into a two's complement integer representation.
- // This makes floating-point values comparable as integers.
- inline int32_t float_as_twos_complement(float f)
- {
- // IEEE-754 floating-point numbers are sorted by magnitude in the same way as integers,
- // except negative values are like one's complement integers. Convert them to two's complement.
- int32_t i = bit_cast<int32_t>(f);
- return (i < 0) ? (0x7FFFFFFFu - i) : i;
- }
-
- // 'Safe' clamping operation which always returns a value between min and max (inclusive).
- inline float clamp_s(float x, float min, float max)
- {
- // NaN values can't be compared directly
- if(float_as_twos_complement(x) < float_as_twos_complement(min)) x = min;
- if(float_as_twos_complement(x) > float_as_twos_complement(max)) x = max;
-
- return x;
- }
-
- inline int ceilPow2(int x)
- {
- int i = 1;
-
- while(i < x)
- {
- i <<= 1;
- }
-
- return i;
- }
-
- inline int floorDiv(int a, int b)
- {
- return a / b + ((a % b) >> 31);
- }
-
- inline int floorMod(int a, int b)
- {
- int r = a % b;
- return r + ((r >> 31) & b);
- }
-
- inline int ceilDiv(int a, int b)
- {
- return a / b - (-(a % b) >> 31);
- }
-
- inline int ceilMod(int a, int b)
- {
- int r = a % b;
- return r - ((-r >> 31) & b);
- }
-
- template<const int n>
- inline unsigned int unorm(float x)
- {
- static const unsigned int max = 0xFFFFFFFF >> (32 - n);
- static const float maxf = static_cast<float>(max);
-
if(x >= 1.0f)
{
return max;
}
- else if(x <= 0.0f)
+ else
{
- return 0;
+ return static_cast<int>(maxf * x + 0.5f);
+ }
+ }
+ else
+ {
+ if(x <= -1.0f)
+ {
+ return min;
}
else
{
- return static_cast<unsigned int>(maxf * x + 0.5f);
+ return static_cast<int>(maxf * x - 0.5f) & range;
}
}
+}
- template<const int n>
- inline int snorm(float x)
+template<const int n>
+inline unsigned int ucast(float x)
+{
+ static const unsigned int max = 0xFFFFFFFF >> (32 - n);
+ static const float maxf = static_cast<float>(max);
+
+ if(x >= maxf)
{
- static const unsigned int min = 0x80000000 >> (32 - n);
- static const unsigned int max = 0xFFFFFFFF >> (32 - n + 1);
- static const float maxf = static_cast<float>(max);
- static const unsigned int range = 0xFFFFFFFF >> (32 - n);
-
- if(x >= 0.0f)
- {
- if(x >= 1.0f)
- {
- return max;
- }
- else
- {
- return static_cast<int>(maxf * x + 0.5f);
- }
- }
- else
- {
- if(x <= -1.0f)
- {
- return min;
- }
- else
- {
- return static_cast<int>(maxf * x - 0.5f) & range;
- }
- }
+ return max;
}
-
- template<const int n>
- inline unsigned int ucast(float x)
+ else if(x <= 0.0f)
{
- static const unsigned int max = 0xFFFFFFFF >> (32 - n);
- static const float maxf = static_cast<float>(max);
+ return 0;
+ }
+ else
+ {
+ return static_cast<unsigned int>(x + 0.5f);
+ }
+}
+template<const int n>
+inline int scast(float x)
+{
+ static const unsigned int min = 0x80000000 >> (32 - n);
+ static const unsigned int max = 0xFFFFFFFF >> (32 - n + 1);
+ static const float maxf = static_cast<float>(max);
+ static const float minf = static_cast<float>(min);
+ static const unsigned int range = 0xFFFFFFFF >> (32 - n);
+
+ if(x > 0.0f)
+ {
if(x >= maxf)
{
return max;
}
- else if(x <= 0.0f)
+ else
{
- return 0;
+ return static_cast<int>(x + 0.5f);
+ }
+ }
+ else
+ {
+ if(x <= -minf)
+ {
+ return min;
}
else
{
- return static_cast<unsigned int>(x + 0.5f);
+ return static_cast<int>(x - 0.5f) & range;
}
}
-
- template<const int n>
- inline int scast(float x)
- {
- static const unsigned int min = 0x80000000 >> (32 - n);
- static const unsigned int max = 0xFFFFFFFF >> (32 - n + 1);
- static const float maxf = static_cast<float>(max);
- static const float minf = static_cast<float>(min);
- static const unsigned int range = 0xFFFFFFFF >> (32 - n);
-
- if(x > 0.0f)
- {
- if(x >= maxf)
- {
- return max;
- }
- else
- {
- return static_cast<int>(x + 0.5f);
- }
- }
- else
- {
- if(x <= -minf)
- {
- return min;
- }
- else
- {
- return static_cast<int>(x - 0.5f) & range;
- }
- }
- }
-
- inline float sRGBtoLinear(float c)
- {
- if(c <= 0.04045f)
- {
- return c * 0.07739938f; // 1.0f / 12.92f;
- }
- else
- {
- return powf((c + 0.055f) * 0.9478673f, 2.4f); // 1.0f / 1.055f
- }
- }
-
- inline float linearToSRGB(float c)
- {
- if(c <= 0.0031308f)
- {
- return c * 12.92f;
- }
- else
- {
- return 1.055f * powf(c, 0.4166667f) - 0.055f; // 1.0f / 2.4f
- }
- }
-
- unsigned char sRGB8toLinear8(unsigned char value);
-
- uint64_t FNV_1a(const unsigned char *data, int size); // Fowler-Noll-Vo hash function
-
- // Round up to the next multiple of alignment
- template<typename T>
- inline T align(T value, unsigned int alignment)
- {
- return ((value + alignment - 1) / alignment) * alignment;
- }
-
- template<unsigned int alignment, typename T>
- inline T align(T value)
- {
- return ((value + alignment - 1) / alignment) * alignment;
- }
-
- inline int clampToSignedInt(unsigned int x)
- {
- return static_cast<int>(min(x, 0x7FFFFFFFu));
- }
-
- // Convert floating value v to fixed point with p digits after the decimal point
- constexpr int toFixedPoint(float v, int p) {
- return static_cast<int>(v * (1 << p));
- }
}
+inline float sRGBtoLinear(float c)
+{
+ if(c <= 0.04045f)
+ {
+ return c * 0.07739938f; // 1.0f / 12.92f;
+ }
+ else
+ {
+ return powf((c + 0.055f) * 0.9478673f, 2.4f); // 1.0f / 1.055f
+ }
+}
+
+inline float linearToSRGB(float c)
+{
+ if(c <= 0.0031308f)
+ {
+ return c * 12.92f;
+ }
+ else
+ {
+ return 1.055f * powf(c, 0.4166667f) - 0.055f; // 1.0f / 2.4f
+ }
+}
+
+unsigned char sRGB8toLinear8(unsigned char value);
+
+uint64_t FNV_1a(const unsigned char *data, int size); // Fowler-Noll-Vo hash function
+
+// Round up to the next multiple of alignment
+template<typename T>
+inline T align(T value, unsigned int alignment)
+{
+ return ((value + alignment - 1) / alignment) * alignment;
+}
+
+template<unsigned int alignment, typename T>
+inline T align(T value)
+{
+ return ((value + alignment - 1) / alignment) * alignment;
+}
+
+inline int clampToSignedInt(unsigned int x)
+{
+ return static_cast<int>(min(x, 0x7FFFFFFFu));
+}
+
+// Convert floating value v to fixed point with p digits after the decimal point
+constexpr int toFixedPoint(float v, int p) {
+ return static_cast<int>(v * (1 << p));
+}
+
+} // namespace sw
+
#endif // sw_Math_hpp
diff --git a/src/System/Memory.cpp b/src/System/Memory.cpp
index e045254..e637a55 100644
--- a/src/System/Memory.cpp
+++ b/src/System/Memory.cpp
@@ -40,10 +40,10 @@
#define __x86__
#endif
-namespace sw
-{
-namespace
-{
+namespace sw {
+
+namespace {
+
struct Allocation
{
// size_t bytes;
@@ -86,6 +86,7 @@
return aligned;
#endif
}
+
} // anonymous namespace
size_t memoryPageSize()
@@ -160,4 +161,5 @@
}
#endif
}
-}
+
+} // namespace sw
diff --git a/src/System/Memory.hpp b/src/System/Memory.hpp
index 0e8d188..d1e7871 100644
--- a/src/System/Memory.hpp
+++ b/src/System/Memory.hpp
@@ -18,8 +18,8 @@
#include <stddef.h>
#include <stdint.h>
-namespace sw
-{
+namespace sw {
+
size_t memoryPageSize();
void *allocate(size_t bytes, size_t alignment = 16);
@@ -27,6 +27,7 @@
void clear(uint16_t *memory, uint16_t element, size_t count);
void clear(uint32_t *memory, uint32_t element, size_t count);
-}
+
+} // namespace sw
#endif // Memory_hpp
diff --git a/src/System/Socket.cpp b/src/System/Socket.cpp
index b098031..1989a94 100644
--- a/src/System/Socket.cpp
+++ b/src/System/Socket.cpp
@@ -23,88 +23,89 @@
#include <sys/select.h>
#endif
-namespace sw
+namespace sw {
+
+Socket::Socket(SOCKET socket) : socket(socket)
{
- Socket::Socket(SOCKET socket) : socket(socket)
+}
+
+Socket::Socket(const char *address, const char *port)
+{
+ #if defined(_WIN32)
+ socket = INVALID_SOCKET;
+ #else
+ socket = -1;
+ #endif
+
+ addrinfo hints = {};
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+ hints.ai_flags = AI_PASSIVE;
+
+ addrinfo *info = 0;
+ getaddrinfo(address, port, &hints, &info);
+
+ if(info)
{
- }
-
- Socket::Socket(const char *address, const char *port)
- {
- #if defined(_WIN32)
- socket = INVALID_SOCKET;
- #else
- socket = -1;
- #endif
-
- addrinfo hints = {};
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
-
- addrinfo *info = 0;
- getaddrinfo(address, port, &hints, &info);
-
- if(info)
- {
- socket = ::socket(info->ai_family, info->ai_socktype, info->ai_protocol);
- bind(socket, info->ai_addr, (int)info->ai_addrlen);
- }
- }
-
- Socket::~Socket()
- {
- #if defined(_WIN32)
- closesocket(socket);
- #else
- close(socket);
- #endif
- }
-
- void Socket::listen(int backlog)
- {
- ::listen(socket, backlog);
- }
-
- bool Socket::select(int us)
- {
- fd_set sockets;
- FD_ZERO(&sockets);
- FD_SET(socket, &sockets);
-
- timeval timeout = {us / 1000000, us % 1000000};
-
- return ::select(FD_SETSIZE, &sockets, 0, 0, &timeout) >= 1;
- }
-
- Socket *Socket::accept()
- {
- return new Socket(::accept(socket, 0, 0));
- }
-
- int Socket::receive(char *buffer, int length)
- {
- return recv(socket, buffer, length, 0);
- }
-
- void Socket::send(const char *buffer, int length)
- {
- ::send(socket, buffer, length, 0);
- }
-
- void Socket::startup()
- {
- #if defined(_WIN32)
- WSADATA winsockData;
- WSAStartup(MAKEWORD(2, 2), &winsockData);
- #endif
- }
-
- void Socket::cleanup()
- {
- #if defined(_WIN32)
- WSACleanup();
- #endif
+ socket = ::socket(info->ai_family, info->ai_socktype, info->ai_protocol);
+ bind(socket, info->ai_addr, (int)info->ai_addrlen);
}
}
+
+Socket::~Socket()
+{
+ #if defined(_WIN32)
+ closesocket(socket);
+ #else
+ close(socket);
+ #endif
+}
+
+void Socket::listen(int backlog)
+{
+ ::listen(socket, backlog);
+}
+
+bool Socket::select(int us)
+{
+ fd_set sockets;
+ FD_ZERO(&sockets);
+ FD_SET(socket, &sockets);
+
+ timeval timeout = {us / 1000000, us % 1000000};
+
+ return ::select(FD_SETSIZE, &sockets, 0, 0, &timeout) >= 1;
+}
+
+Socket *Socket::accept()
+{
+ return new Socket(::accept(socket, 0, 0));
+}
+
+int Socket::receive(char *buffer, int length)
+{
+ return recv(socket, buffer, length, 0);
+}
+
+void Socket::send(const char *buffer, int length)
+{
+ ::send(socket, buffer, length, 0);
+}
+
+void Socket::startup()
+{
+ #if defined(_WIN32)
+ WSADATA winsockData;
+ WSAStartup(MAKEWORD(2, 2), &winsockData);
+ #endif
+}
+
+void Socket::cleanup()
+{
+ #if defined(_WIN32)
+ WSACleanup();
+ #endif
+}
+
+} // namespace sw
diff --git a/src/System/Socket.hpp b/src/System/Socket.hpp
index b6b9abd..efb5062 100644
--- a/src/System/Socket.hpp
+++ b/src/System/Socket.hpp
@@ -22,28 +22,29 @@
typedef int SOCKET;
#endif
-namespace sw
+namespace sw {
+
+class Socket
{
- class Socket
- {
- public:
- Socket(SOCKET socket);
- Socket(const char *address, const char *port);
- ~Socket();
+public:
+ Socket(SOCKET socket);
+ Socket(const char *address, const char *port);
+ ~Socket();
- void listen(int backlog = 1);
- bool select(int us);
- Socket *accept();
-
- int receive(char *buffer, int length);
- void send(const char *buffer, int length);
+ void listen(int backlog = 1);
+ bool select(int us);
+ Socket *accept();
- static void startup();
- static void cleanup();
+ int receive(char *buffer, int length);
+ void send(const char *buffer, int length);
- private:
- SOCKET socket;
- };
+ static void startup();
+ static void cleanup();
+
+private:
+ SOCKET socket;
+};
+
}
#endif // sw_Socket_hpp
diff --git a/src/System/Synchronization.hpp b/src/System/Synchronization.hpp
index 5af65e2..1a8c585 100644
--- a/src/System/Synchronization.hpp
+++ b/src/System/Synchronization.hpp
@@ -28,8 +28,7 @@
#include <mutex>
#include <queue>
-namespace sw
-{
+namespace sw {
// TaskEvents is an interface for notifying when tasks begin and end.
// Tasks can be nested and/or overlapping.
@@ -191,6 +190,6 @@
return queue.size();
}
-} // namespace sw
+} // namespace sw
#endif // sw_Synchronization_hpp
diff --git a/src/System/Timer.cpp b/src/System/Timer.cpp
index db0ba4a..7bd7ba5 100644
--- a/src/System/Timer.cpp
+++ b/src/System/Timer.cpp
@@ -35,65 +35,66 @@
#endif
#endif
-namespace sw
+namespace sw {
+
+Timer::Timer()
{
- Timer::Timer()
- {
- }
-
- Timer::~Timer()
- {
- }
-
- double Timer::seconds()
- {
- #if defined(_WIN32)
- return (double)counter() / (double)frequency();
- #else
- timeval t;
- gettimeofday(&t, 0);
- return (double)t.tv_sec + (double)t.tv_usec * 1.0e-6;
- #endif
- }
-
- int64_t Timer::ticks()
- {
- #if defined(_WIN32)
- #if defined(_M_ARM64)
- return _ReadStatusReg(ARM64_PMCCNTR_EL0);
- #else
- return __rdtsc();
- #endif
- #elif defined(__i386__) || defined(__x86_64__)
- int64_t tsc;
- __asm volatile("rdtsc": "=A" (tsc));
- return tsc;
- #else
- return 0;
- #endif
- }
-
- int64_t Timer::counter()
- {
- #if defined(_WIN32)
- int64_t counter;
- QueryPerformanceCounter((LARGE_INTEGER*)&counter);
- return counter;
- #else
- timeval t;
- gettimeofday(&t, 0);
- return t.tv_sec * 1000000 + t.tv_usec;
- #endif
- }
-
- int64_t Timer::frequency()
- {
- #if defined(_WIN32)
- int64_t frequency;
- QueryPerformanceFrequency((LARGE_INTEGER*)&frequency);
- return frequency;
- #else
- return 1000000; // gettimeofday uses microsecond resolution
- #endif
- }
}
+
+Timer::~Timer()
+{
+}
+
+double Timer::seconds()
+{
+ #if defined(_WIN32)
+ return (double)counter() / (double)frequency();
+ #else
+ timeval t;
+ gettimeofday(&t, 0);
+ return (double)t.tv_sec + (double)t.tv_usec * 1.0e-6;
+ #endif
+}
+
+int64_t Timer::ticks()
+{
+ #if defined(_WIN32)
+ #if defined(_M_ARM64)
+ return _ReadStatusReg(ARM64_PMCCNTR_EL0);
+ #else
+ return __rdtsc();
+ #endif
+ #elif defined(__i386__) || defined(__x86_64__)
+ int64_t tsc;
+ __asm volatile("rdtsc": "=A" (tsc));
+ return tsc;
+ #else
+ return 0;
+ #endif
+}
+
+int64_t Timer::counter()
+{
+ #if defined(_WIN32)
+ int64_t counter;
+ QueryPerformanceCounter((LARGE_INTEGER*)&counter);
+ return counter;
+ #else
+ timeval t;
+ gettimeofday(&t, 0);
+ return t.tv_sec * 1000000 + t.tv_usec;
+ #endif
+}
+
+int64_t Timer::frequency()
+{
+ #if defined(_WIN32)
+ int64_t frequency;
+ QueryPerformanceFrequency((LARGE_INTEGER*)&frequency);
+ return frequency;
+ #else
+ return 1000000; // gettimeofday uses microsecond resolution
+ #endif
+}
+
+} // namespace sw
diff --git a/src/System/Timer.hpp b/src/System/Timer.hpp
index 977c877..a2f687b 100644
--- a/src/System/Timer.hpp
+++ b/src/System/Timer.hpp
@@ -17,21 +17,22 @@
#include "Types.hpp"
-namespace sw
+namespace sw {
+
+class Timer
{
- class Timer
- {
- public:
- Timer();
+public:
+ Timer();
- ~Timer();
+ ~Timer();
- static double seconds();
- static int64_t ticks();
+ static double seconds();
+ static int64_t ticks();
- static int64_t counter();
- static int64_t frequency();
- };
-}
+ static int64_t counter();
+ static int64_t frequency();
+};
+
+} // namespace sw
#endif // sw_Timer_hpp
diff --git a/src/System/Types.hpp b/src/System/Types.hpp
index 70c084d..734efb0 100644
--- a/src/System/Types.hpp
+++ b/src/System/Types.hpp
@@ -42,102 +42,103 @@
#define ALIGN(bytes, type) type __attribute__((aligned(bytes)))
#endif
-namespace sw
+namespace sw {
+
+typedef ALIGN(1, uint8_t) byte;
+typedef ALIGN(2, uint16_t) word;
+typedef ALIGN(4, uint32_t) dword;
+typedef ALIGN(8, uint64_t) qword;
+typedef ALIGN(16, uint64_t) qword2[2];
+typedef ALIGN(4, uint8_t) byte4[4];
+typedef ALIGN(8, uint8_t) byte8[8];
+typedef ALIGN(16, uint8_t) byte16[16];
+typedef ALIGN(8, uint16_t) word4[4];
+typedef ALIGN(8, uint32_t) dword2[2];
+typedef ALIGN(16, uint32_t) dword4[4];
+typedef ALIGN(16, uint64_t) xword[2];
+
+typedef ALIGN(1, int8_t) sbyte;
+typedef ALIGN(4, int8_t) sbyte4[4];
+typedef ALIGN(8, int8_t) sbyte8[8];
+typedef ALIGN(16, int8_t) sbyte16[16];
+typedef ALIGN(8, short) short4[4];
+typedef ALIGN(8, unsigned short) ushort4[4];
+typedef ALIGN(16, short) short8[8];
+typedef ALIGN(16, unsigned short) ushort8[8];
+typedef ALIGN(8, int) int2[2];
+typedef ALIGN(8, unsigned int) uint2[2];
+typedef ALIGN(16, unsigned int) uint4[4];
+
+typedef ALIGN(8, float) float2[2];
+
+ALIGN(16, struct int4
{
- typedef ALIGN(1, uint8_t) byte;
- typedef ALIGN(2, uint16_t) word;
- typedef ALIGN(4, uint32_t) dword;
- typedef ALIGN(8, uint64_t) qword;
- typedef ALIGN(16, uint64_t) qword2[2];
- typedef ALIGN(4, uint8_t) byte4[4];
- typedef ALIGN(8, uint8_t) byte8[8];
- typedef ALIGN(16, uint8_t) byte16[16];
- typedef ALIGN(8, uint16_t) word4[4];
- typedef ALIGN(8, uint32_t) dword2[2];
- typedef ALIGN(16, uint32_t) dword4[4];
- typedef ALIGN(16, uint64_t) xword[2];
+ int x;
+ int y;
+ int z;
+ int w;
- typedef ALIGN(1, int8_t) sbyte;
- typedef ALIGN(4, int8_t) sbyte4[4];
- typedef ALIGN(8, int8_t) sbyte8[8];
- typedef ALIGN(16, int8_t) sbyte16[16];
- typedef ALIGN(8, short) short4[4];
- typedef ALIGN(8, unsigned short) ushort4[4];
- typedef ALIGN(16, short) short8[8];
- typedef ALIGN(16, unsigned short) ushort8[8];
- typedef ALIGN(8, int) int2[2];
- typedef ALIGN(8, unsigned int) uint2[2];
- typedef ALIGN(16, unsigned int) uint4[4];
-
- typedef ALIGN(8, float) float2[2];
-
- ALIGN(16, struct int4
+ int &operator[](int i)
{
- int x;
- int y;
- int z;
- int w;
-
- int &operator[](int i)
- {
- return (&x)[i];
- }
-
- const int &operator[](int i) const
- {
- return (&x)[i];
- }
-
- bool operator!=(const int4 &rhs)
- {
- return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w;
- }
-
- bool operator==(const int4 &rhs)
- {
- return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
- }
- });
-
- ALIGN(16, struct float4
- {
- float x;
- float y;
- float z;
- float w;
-
- float &operator[](int i)
- {
- return (&x)[i];
- }
-
- const float &operator[](int i) const
- {
- return (&x)[i];
- }
-
- bool operator!=(const float4 &rhs)
- {
- return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w;
- }
-
- bool operator==(const float4 &rhs)
- {
- return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
- }
- });
-
- inline constexpr float4 vector(float x, float y, float z, float w)
- {
- return { x, y, z, w };
+ return (&x)[i];
}
- inline constexpr float4 replicate(float f)
+ const int &operator[](int i) const
{
- return vector(f, f, f, f);
+ return (&x)[i];
}
- #define OFFSET(s,m) (int)(size_t)&reinterpret_cast<const volatile char&>((((s*)0)->m))
+ bool operator!=(const int4 &rhs)
+ {
+ return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w;
+ }
+
+ bool operator==(const int4 &rhs)
+ {
+ return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
+ }
+});
+
+ALIGN(16, struct float4
+{
+ float x;
+ float y;
+ float z;
+ float w;
+
+ float &operator[](int i)
+ {
+ return (&x)[i];
+ }
+
+ const float &operator[](int i) const
+ {
+ return (&x)[i];
+ }
+
+ bool operator!=(const float4 &rhs)
+ {
+ return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w;
+ }
+
+ bool operator==(const float4 &rhs)
+ {
+ return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
+ }
+});
+
+inline constexpr float4 vector(float x, float y, float z, float w)
+{
+ return { x, y, z, w };
}
+inline constexpr float4 replicate(float f)
+{
+ return vector(f, f, f, f);
+}
+
+#define OFFSET(s,m) (int)(size_t)&reinterpret_cast<const volatile char&>((((s*)0)->m))
+
+} // namespace sw
+
#endif // sw_Types_hpp