Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for macos build #71

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/box2d/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

#ifdef __cplusplus
#define B2_LITERAL(T) T
#define B2_ZERO_INIT {}
#else
#define B2_LITERAL(T) (T)
#define B2_ZERO_INIT {0}
#endif

#define B2_ARRAY_COUNT(A) (int)(sizeof(A) / sizeof(A[0]))
Expand Down Expand Up @@ -177,7 +179,7 @@ typedef struct b2BodyDef
{
/// The body type: static, kinematic, or dynamic.
/// Note: if a dynamic body would have zero mass, the mass is set to one.
enum b2BodyType type;
b2BodyType type;

/// The world position of the body. Avoid creating bodies at the origin
/// since this can lead to many overlapping shapes.
Expand Down Expand Up @@ -342,7 +344,7 @@ static inline b2WorldDef b2DefaultWorldDef(void)
/// Make a body definition with default values.
static inline b2BodyDef b2DefaultBodyDef(void)
{
b2BodyDef def = {0};
b2BodyDef def = B2_ZERO_INIT;
def.type = b2_staticBody;
def.position = B2_LITERAL(b2Vec2){0.0f, 0.0f};
def.angle = 0.0f;
Expand Down
12 changes: 8 additions & 4 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ add_library(
target_include_directories(glad PUBLIC ${GLAD_DIR}/include)

# glfw for windowing and input
SET(GLFW_BUILD_DOCS OFF CACHE BOOL "GLFW Docs")
SET(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "GLFW Examples")
SET(GLFW_BUILD_TESTS OFF CACHE BOOL "GLFW Tests")
SET(GLFW_INSTALL OFF CACHE BOOL "GLFW Install")
set(GLFW_BUILD_DOCS OFF CACHE BOOL "GLFW Docs")
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "GLFW Examples")
set(GLFW_BUILD_TESTS OFF CACHE BOOL "GLFW Tests")
set(GLFW_INSTALL OFF CACHE BOOL "GLFW Install")

FetchContent_Declare(
glfw
Expand Down Expand Up @@ -94,6 +94,10 @@ set_target_properties(samples PROPERTIES
target_include_directories(samples PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${JSMN_DIR})
target_link_libraries(samples PUBLIC box2d imgui glfw glad enkiTS)

if (${CMAKE_BUILD_TYPE} STREQUAL Debug)
target_compile_definitions(samples PRIVATE _DEBUG)
endif ()

# message(STATUS "runtime = ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
# message(STATUS "binary = ${CMAKE_CURRENT_BINARY_DIR}")

Expand Down
2 changes: 1 addition & 1 deletion samples/collection/sample_dynamic_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class DynamicTree : public Sample
m_startPoint = p;
m_endPoint = p;
}
else if (mods = GLFW_MOD_SHIFT && m_rayDrag == false)
else if (mods == GLFW_MOD_SHIFT && m_rayDrag == false)
{
m_queryDrag = true;
m_startPoint = p;
Expand Down
8 changes: 4 additions & 4 deletions samples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ int main(int, char**)
// MSAA
glfwWindowHint(GLFW_SAMPLES, 4);

sprintf(buffer, "Box2D Version %d.%d.%d Smooth", b2_version.major, b2_version.minor, b2_version.revision);
snprintf(buffer, 128, "Box2D Version %d.%d.%d Smooth", b2_version.major, b2_version.minor, b2_version.revision);

if (GLFWmonitor* primaryMonitor = glfwGetPrimaryMonitor())
{
Expand Down Expand Up @@ -670,7 +670,7 @@ int main(int, char**)
if (g_draw.m_showUI)
{
const SampleEntry& entry = g_sampleEntries[s_settings.m_sampleIndex];
sprintf(buffer, "%s : %s", entry.category, entry.name);
snprintf(buffer, 128, "%s : %s", entry.category, entry.name);
s_sample->DrawTitle(buffer);
}

Expand All @@ -684,13 +684,13 @@ int main(int, char**)

// if (g_draw.m_showUI)
{
sprintf(buffer, "%.1f ms", 1000.0f * frameTime);
snprintf(buffer, 128, "%.1f ms", 1000.0f * frameTime);

ImGui::Begin("Overlay", nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoScrollbar);
ImGui::SetCursorPos(ImVec2(5.0f, g_camera.m_height - 20.0f));
ImGui::TextColored(ImColor(153, 230, 153, 255), buffer);
ImGui::TextColored(ImColor(153, 230, 153, 255), "%s", buffer);
ImGui::End();
}

Expand Down
6 changes: 3 additions & 3 deletions samples/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,14 @@ void Sample::Step(Settings& settings)

int32_t totalCount = 0;
char buffer[256] = {0};
int32_t offset = sprintf_s(buffer, 256, "colors: ");
int32_t offset = snprintf(buffer, 256, "colors: ");
for (int32_t i = 0; i < b2_graphColorCount; ++i)
{
offset += sprintf_s(buffer + offset, 256 - offset, "%d/", s.colorCounts[i]);
offset += snprintf(buffer + offset, 256 - offset, "%d/", s.colorCounts[i]);
totalCount += s.colorCounts[i];
}
totalCount += s.colorCounts[b2_graphColorCount];
sprintf_s(buffer + offset, 256 - offset, "(%d)[%d]", s.colorCounts[b2_graphColorCount], totalCount);
snprintf(buffer + offset, 256 - offset, "(%d)[%d]", s.colorCounts[b2_graphColorCount], totalCount);
g_draw.DrawString(5, m_textLine, buffer);
m_textLine += m_textIncrement;

Expand Down
2 changes: 1 addition & 1 deletion samples/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

struct Settings;

#ifdef _DEBUG
#if defined(_DEBUG)
constexpr bool g_sampleDebug = true;
#else
constexpr bool g_sampleDebug = false;
Expand Down
2 changes: 1 addition & 1 deletion samples/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void Settings::Load()
int count = tokens[i + 1].end - tokens[i + 1].start;
assert(count < 32);
const char* s = data + tokens[i + 1].start;
strncpy_s(buffer, 32, s, count);
strncpy(buffer, s, count);
char* dummy;
m_sampleIndex = strtol(buffer, &dummy, 10);
}
Expand Down
7 changes: 5 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ message(STATUS "CMake C compiler: ${CMAKE_C_COMPILER_ID}")
message(STATUS "CMake C++ compiler: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "CMake system name: ${CMAKE_SYSTEM_NAME}")

if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
if (MSVC)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# target_compile_options(box2d PRIVATE /W4 /WX)
target_compile_options(box2d PRIVATE /W4 /WX /experimental:c11atomics)
Expand All @@ -101,9 +101,12 @@ if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
message(STATUS "CMake using Clang-CL")
target_compile_options(box2d PRIVATE /W4 /WX /std:c17 /arch:AVX2)
endif()
else()
elseif(LINUX)
# FMA -mfma
target_compile_options(box2d PRIVATE -Wall -Wextra -Wpedantic -Werror -mavx)
elseif(APPLE)
# enkiTS has some missing (void) on functions
target_compile_options(box2d PRIVATE -Wall -Wextra -Wpedantic -Werror -Wno-strict-prototypes)
endif()

find_library(MATH_LIBRARY m)
Expand Down
48 changes: 24 additions & 24 deletions src/contact_solver.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "world.h"

// #include <immintrin.h>
#include <x86/avx2.h>
#include <x86/fma.h>
#include "x86/avx2.h"
#include "x86/fma.h"

// Soft constraints with constraint error substepping. Includes a bias removal stage to help remove excess energy.
// http://mmacklin.com/smallsteps.pdf
Expand Down Expand Up @@ -435,14 +435,14 @@ static b2SimdBody b2GatherBodies(const b2SolverBody* restrict bodies, int32_t* r
b2FloatW t5 = simde_mm256_unpackhi_ps(b4, b5);
b2FloatW t6 = simde_mm256_unpacklo_ps(b6, b7);
b2FloatW t7 = simde_mm256_unpackhi_ps(b6, b7);
b2FloatW tt0 = simde_mm256_shuffle_ps(t0, t2, _MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt1 = simde_mm256_shuffle_ps(t0, t2, _MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt2 = simde_mm256_shuffle_ps(t1, t3, _MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt3 = simde_mm256_shuffle_ps(t1, t3, _MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt4 = simde_mm256_shuffle_ps(t4, t6, _MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt5 = simde_mm256_shuffle_ps(t4, t6, _MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt6 = simde_mm256_shuffle_ps(t5, t7, _MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt7 = simde_mm256_shuffle_ps(t5, t7, _MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt0 = simde_mm256_shuffle_ps(t0, t2, SIMDE_MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt1 = simde_mm256_shuffle_ps(t0, t2, SIMDE_MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt2 = simde_mm256_shuffle_ps(t1, t3, SIMDE_MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt3 = simde_mm256_shuffle_ps(t1, t3, SIMDE_MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt4 = simde_mm256_shuffle_ps(t4, t6, SIMDE_MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt5 = simde_mm256_shuffle_ps(t4, t6, SIMDE_MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt6 = simde_mm256_shuffle_ps(t5, t7, SIMDE_MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt7 = simde_mm256_shuffle_ps(t5, t7, SIMDE_MM_SHUFFLE(3, 2, 3, 2));

b2SimdBody simdBody;
simdBody.v.X = simde_mm256_permute2f128_ps(tt0, tt4, 0x20);
Expand Down Expand Up @@ -470,14 +470,14 @@ static void b2ScatterBodies(b2SolverBody* restrict bodies, int32_t* restrict ind
b2FloatW t5 = simde_mm256_unpackhi_ps(simdBody->dp.Y, simdBody->da);
b2FloatW t6 = simde_mm256_unpacklo_ps(simdBody->invM, simdBody->invI);
b2FloatW t7 = simde_mm256_unpackhi_ps(simdBody->invM, simdBody->invI);
b2FloatW tt0 = simde_mm256_shuffle_ps(t0, t2, _MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt1 = simde_mm256_shuffle_ps(t0, t2, _MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt2 = simde_mm256_shuffle_ps(t1, t3, _MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt3 = simde_mm256_shuffle_ps(t1, t3, _MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt4 = simde_mm256_shuffle_ps(t4, t6, _MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt5 = simde_mm256_shuffle_ps(t4, t6, _MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt6 = simde_mm256_shuffle_ps(t5, t7, _MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt7 = simde_mm256_shuffle_ps(t5, t7, _MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt0 = simde_mm256_shuffle_ps(t0, t2, SIMDE_MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt1 = simde_mm256_shuffle_ps(t0, t2, SIMDE_MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt2 = simde_mm256_shuffle_ps(t1, t3, SIMDE_MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt3 = simde_mm256_shuffle_ps(t1, t3, SIMDE_MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt4 = simde_mm256_shuffle_ps(t4, t6, SIMDE_MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt5 = simde_mm256_shuffle_ps(t4, t6, SIMDE_MM_SHUFFLE(3, 2, 3, 2));
b2FloatW tt6 = simde_mm256_shuffle_ps(t5, t7, SIMDE_MM_SHUFFLE(1, 0, 1, 0));
b2FloatW tt7 = simde_mm256_shuffle_ps(t5, t7, SIMDE_MM_SHUFFLE(3, 2, 3, 2));

// I don't use any dummy body in the body array because this will lead to multithreaded sharing and the
// associated cache flushing.
Expand Down Expand Up @@ -771,7 +771,7 @@ void b2SolveContactsSIMD(int32_t startIndex, int32_t endIndex, b2SolverTaskConte

b2FloatW s = add(c->separation1, ds);

b2FloatW test = simde_mm256_cmp_ps(s, simde_mm256_setzero_ps(), _CMP_GT_OQ);
b2FloatW test = simde_mm256_cmp_ps(s, simde_mm256_setzero_ps(), SIMDE_CMP_GT_OQ);
b2FloatW specBias = mul(s, invDtMul);
b2FloatW softBias = simde_mm256_max_ps(mul(biasCoeff, s), minBiasVel);
b2FloatW bias = simde_mm256_blendv_ps(softBias, specBias, test);
Expand Down Expand Up @@ -811,7 +811,7 @@ void b2SolveContactsSIMD(int32_t startIndex, int32_t endIndex, b2SolverTaskConte

b2FloatW s = add(c->separation2, ds);

b2FloatW test = simde_mm256_cmp_ps(s, simde_mm256_setzero_ps(), _CMP_GT_OQ);
b2FloatW test = simde_mm256_cmp_ps(s, simde_mm256_setzero_ps(), SIMDE_CMP_GT_OQ);
b2FloatW specBias = mul(s, invDtMul);
b2FloatW softBias = simde_mm256_max_ps(mul(biasCoeff, s), minBiasVel);
b2FloatW bias = simde_mm256_blendv_ps(softBias, specBias, test);
Expand Down Expand Up @@ -932,8 +932,8 @@ void b2ApplyRestitutionSIMD(int32_t startIndex, int32_t endIndex, b2SolverTaskCo
// first point non-penetration constraint
{
// Set effective mass to zero if restitution should not be applied
b2FloatW test1 = simde_mm256_cmp_ps(add(c->relativeVelocity1, threshold), zero, _CMP_GT_OQ);
b2FloatW test2 = simde_mm256_cmp_ps(c->normalImpulse1, zero, _CMP_EQ_OQ);
b2FloatW test1 = simde_mm256_cmp_ps(add(c->relativeVelocity1, threshold), zero, SIMDE_CMP_GT_OQ);
b2FloatW test2 = simde_mm256_cmp_ps(c->normalImpulse1, zero, SIMDE_CMP_EQ_OQ);
b2FloatW test = simde_mm256_or_ps(test1, test2);
b2FloatW mass = simde_mm256_blendv_ps(c->normalMass1, zero, test);

Expand Down Expand Up @@ -966,8 +966,8 @@ void b2ApplyRestitutionSIMD(int32_t startIndex, int32_t endIndex, b2SolverTaskCo
// second point non-penetration constraint
{
// Set effective mass to zero if restitution should not be applied
b2FloatW test1 = simde_mm256_cmp_ps(add(c->relativeVelocity2, threshold), zero, _CMP_GT_OQ);
b2FloatW test2 = simde_mm256_cmp_ps(c->normalImpulse2, zero, _CMP_EQ_OQ);
b2FloatW test1 = simde_mm256_cmp_ps(add(c->relativeVelocity2, threshold), zero, SIMDE_CMP_GT_OQ);
b2FloatW test2 = simde_mm256_cmp_ps(c->normalImpulse2, zero, SIMDE_CMP_EQ_OQ);
b2FloatW test = simde_mm256_or_ps(test1, test2);
b2FloatW mass = simde_mm256_blendv_ps(c->normalMass2, zero, test);

Expand Down
6 changes: 4 additions & 2 deletions src/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "box2d/aabb.h"

#include "x86/sse2.h"

#include <limits.h>
#include <stdatomic.h>
#include <stdbool.h>
Expand Down Expand Up @@ -918,7 +920,7 @@ static void b2ExecuteMainStage(b2SolverStage* stage, b2SolverTaskContext* contex

while (atomic_load(&stage->completionCount) != blockCount)
{
_mm_pause();
simde_mm_pause();
}

atomic_store(&stage->completionCount, 0);
Expand Down Expand Up @@ -1081,7 +1083,7 @@ void b2SolverTask(int32_t startIndex, int32_t endIndex, uint32_t threadIndexDont
uint32_t syncBits = atomic_load(&context->syncBits);
while (syncBits == lastSyncBits)
{
_mm_pause();
simde_mm_pause();
syncBits = atomic_load(&context->syncBits);
}

Expand Down
6 changes: 0 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ set(BOX2D_TESTS

add_executable(test ${BOX2D_TESTS})

if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
target_compile_options(test PRIVATE /W4 /WX /std:c17)
else()
target_compile_options(test PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif()

# Special access to Box2D internals for testing
target_include_directories(test PRIVATE ${CMAKE_SOURCE_DIR}/src)

Expand Down
18 changes: 9 additions & 9 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
//}
#endif

extern int BitSetTest();
extern int MathTest();
extern int CollisionTest();
extern int DeterminismTest();
extern int DistanceTest();
extern int HelloWorld();
extern int EmptyWorld();
extern int ShapeTest();
extern int TableTest();
extern int BitSetTest(void);
extern int MathTest(void);
extern int CollisionTest(void);
extern int DeterminismTest(void);
extern int DistanceTest(void);
extern int HelloWorld(void);
extern int EmptyWorld(void);
extern int ShapeTest(void);
extern int TableTest(void);

int main(void)
{
Expand Down
2 changes: 1 addition & 1 deletion test/test_bitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#define COUNT 169

int BitSetTest()
int BitSetTest(void)
{
b2BitSet bitSet = b2CreateBitSet(COUNT);

Expand Down
4 changes: 2 additions & 2 deletions test/test_collision.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "box2d/math.h"
#include "test_macros.h"

static int AABBTest()
static int AABBTest(void)
{
b2AABB a;
a.lowerBound = (b2Vec2){-1.0f, -1.0f};
Expand All @@ -30,7 +30,7 @@ static int AABBTest()
return 0;
}

int CollisionTest()
int CollisionTest(void)
{
RUN_SUBTEST(AABBTest);

Expand Down
2 changes: 1 addition & 1 deletion test/test_determinism.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void TiltedStacks(int testIndex, int workerCount)
}

// Test multi-threaded determinism.
int DeterminismTest()
int DeterminismTest(void)
{
// Test 1 : 4 threads
TiltedStacks(0, 16);
Expand Down
10 changes: 5 additions & 5 deletions test/test_distance.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <float.h>

static int SegmentDistanceTest()
static int SegmentDistanceTest(void)
{
b2Vec2 p1 = {-1.0f, -1.0f};
b2Vec2 q1 = {-1.0f, 1.0f};
Expand All @@ -28,7 +28,7 @@ static int SegmentDistanceTest()
return 0;
}

static int ShapeDistanceTest()
static int ShapeDistanceTest(void)
{
b2Vec2 vas[] = {
(b2Vec2){-1.0f, -1.0f},
Expand Down Expand Up @@ -56,7 +56,7 @@ static int ShapeDistanceTest()
return 0;
}

static int ShapeCastTest()
static int ShapeCastTest(void)
{
b2Vec2 vas[] = {
(b2Vec2){-1.0f, -1.0f},
Expand Down Expand Up @@ -85,7 +85,7 @@ static int ShapeCastTest()
return 0;
}

static int TimeOfImpactTest()
static int TimeOfImpactTest(void)
{
b2Vec2 vas[] = {
(b2Vec2){-1.0f, -1.0f},
Expand Down Expand Up @@ -113,7 +113,7 @@ static int TimeOfImpactTest()
return 0;
}

int DistanceTest()
int DistanceTest(void)
{
RUN_SUBTEST(SegmentDistanceTest);
RUN_SUBTEST(ShapeDistanceTest);
Expand Down
Loading