Skip to content

Commit

Permalink
6.20ms - Enabling secret -Wsign-conversion flag to find all implicit…
Browse files Browse the repository at this point in the history
… unsigned int to int conversions
  • Loading branch information
Jumbub committed Oct 4, 2021
1 parent 910b370 commit 86b3634
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 30 deletions.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC = g++

COMPILER_FLAGS = -Wall -W -pedantic -Werror -std=c++2a -lpthread -Ofast
COMPILER_FLAGS = -Wall -Wextra -Werror -Wpedantic -Wsign-conversion -std=c++2a -lpthread -Ofast
COMPILER_FLAGS_PROFILE = $(COMPILER_FLAGS) -O0 -pg
COMPILER_FLAGS_DEBUG = $(COMPILER_FLAGS) -O0 -ggdb

Expand Down
4 changes: 2 additions & 2 deletions results/benchmark.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---------------------------------------------------------------------------------------------
Benchmark Time CPU Iterations
---------------------------------------------------------------------------------------------
BM_NextBoard/min_time:2.000/process_time/real_time 6.27 ms 24.3 ms 447
BM_RenderBoard/process_time/real_time 7.17 ms 7.00 ms 88
BM_NextBoard/min_time:2.000/process_time/real_time 6.14 ms 24.0 ms 451
BM_RenderBoard/process_time/real_time 7.12 ms 6.97 ms 91
3 changes: 1 addition & 2 deletions src/board/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ using Cell = unsigned int; // Should match SDL::Uint32, otherwise the rendering
const Cell ALIVE = UINT32_MAX; // Should match SDL::SDL_UINT32_MAX, "
const Cell DEAD = 0;

using Board = std::tuple<Cell*, int, int>;
using NeighbourPositions = std::array<int, 8>;
using Board = std::tuple<Cell*, unsigned int, unsigned int>;
42 changes: 20 additions & 22 deletions src/board/next.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,38 @@
#include <tuple>
#include <vector>

int THREAD_COUNT =
auto THREAD_COUNT =
std::max(std::thread::hardware_concurrency(), (unsigned int)1);

int getThreads() { return THREAD_COUNT; }
void setThreads(int n) { THREAD_COUNT = std::max(n, 1); }
unsigned int getThreads() { return THREAD_COUNT; }
void setThreads(unsigned int n) { THREAD_COUNT = std::max(n, (unsigned int)1); }

inline const auto maxToOne(Cell n) {
return (1 - (n + 1));
}
inline const auto maxToOne(Cell n) { return (1 - (n + 1)); }

void nextBoardSection(const int startY, const int endY, const Board &board,
void nextBoardSection(const unsigned int startY, const unsigned int endY, const Board &board,
Cell *output) {
const auto &[input, width, height] = board;

int neighbours[3] = {0,0,0};
int yAboveBase = 0;
int yBelowBase = 0;
int yBase = 0;
unsigned int neighbours[3] = {UINT32_MAX, UINT32_MAX, UINT32_MAX};
unsigned int yAboveBase = 0;
unsigned int yBelowBase = 0;
unsigned int yBase = 0;

const auto endI = endY * width;
for (int i = startY * width; i < endI; i++) {
const int x = i % width;
auto currentStateBool = input[i] == ALIVE ? 1 : 0;
for (unsigned int i = startY * width; i < endI; i++) {
const unsigned int x = i % width;
auto currentStateBool = maxToOne(input[i]);

// Slide neighbours
if (x == 0) {
// Clear neighbour columns
neighbours[0] = -1;
neighbours[1] = -1;
neighbours[0] = UINT32_MAX;
neighbours[1] = UINT32_MAX;

// Compute new Y levels
const int y = i / width;
const unsigned int y = i / width;
yBase = y * width;
yBelowBase = ((y - 1 + height) % height) * width;
yBelowBase = ((y - 1) % height) * width;
yAboveBase = ((y + 1) % height) * width;
} else {
neighbours[0] = neighbours[1];
Expand All @@ -50,14 +48,14 @@ void nextBoardSection(const int startY, const int endY, const Board &board,
}

// Compute neighbours
if (neighbours[0] == -1) {
if (neighbours[0] == UINT32_MAX) {
const auto previousX = (x - 1 + width) % width;

neighbours[0] = maxToOne(input[yBelowBase + previousX]) +
maxToOne(input[yBase + previousX]) +
maxToOne(input[yAboveBase + previousX]);
}
if (neighbours[1] == -1) {
if (neighbours[1] == UINT32_MAX) {
neighbours[1] =
maxToOne(input[yBelowBase + x]) + maxToOne(input[yAboveBase + x]);
}
Expand All @@ -67,7 +65,7 @@ void nextBoardSection(const int startY, const int endY, const Board &board,
maxToOne(input[yAboveBase + nextX]);

// Compute new cell state
const int totalNeighbours = neighbours[0] + neighbours[1] + neighbours[2];
const auto totalNeighbours = neighbours[0] + neighbours[1] + neighbours[2];
if (currentStateBool && (totalNeighbours < 2 || totalNeighbours > 3))
output[i] = DEAD;
else if (!currentStateBool && totalNeighbours == 3)
Expand All @@ -89,7 +87,7 @@ Board nextBoard(const Board &board) {
auto threadLinesRemaining = height % totalThreads;

std::vector<std::thread> threads;
for (int t = 0; t < totalThreads; t++) {
for (unsigned int t = 0; t < totalThreads; t++) {
// Compute start and end indexes for threads
const auto startY = t * threadLines;
auto endY = (t + 1) * threadLines;
Expand Down
4 changes: 2 additions & 2 deletions src/board/next.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

Board nextBoard(const Board &input);

int getThreads();
void setThreads(int n);
unsigned int getThreads();
void setThreads(unsigned int n);
2 changes: 1 addition & 1 deletion src/board/sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void renderBoardSdl(const Board board, SDL_Renderer *renderer, SDL_Texture *text
const auto &[input, width, height] = board;

static_assert(sizeof(input[0]) == sizeof(Uint32));
SDL_UpdateTexture(texture, NULL, &input[0], width * sizeof(Uint32));
SDL_UpdateTexture(texture, NULL, &input[0], (long unsigned int)width * sizeof(Uint32));

SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
Expand Down

0 comments on commit 86b3634

Please sign in to comment.