shithub: cstory

Download patch

ref: 9b116dac59fefe504f3f8dbbfe87fd053f20ee70
parent: 0fe8eacb1cc05716c269f59a5197fb6490be9cdb
parent: 81e3259535acda40d00505fd908b9dbed3b66ef4
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon Apr 6 09:36:59 EDT 2020

Merge pull request #96 from GabrielRavier/improvePerformance2

Improve performance for portable (again)

--- /dev/null
+++ b/src/Attributes.h
@@ -1,0 +1,19 @@
+#pragma once
+
+#ifdef __GNUC__
+
+#define ATTRIBUTE_HOT __attribute__((hot))
+#define ATTRIBUTE_OPTIMIZE(optString) __attribute__((optimize(optString)))
+#define LIKELY(condition) __builtin_expect((condition), 1)
+#define UNLIKELY(condition) __builtin_expect((condition), 0)
+#define PREFETCH(address, isWrite, locality) __builtin_prefetch((address), (isWrite), (locality))
+
+#else
+
+#define ATTRIBUTE_HOT
+#define ATTRIBUTE_OPTIMIZE(optString)
+#define LIKELY(condition) condition
+#define UNLIKELY(condition) condition
+#define PREFETCH(address, isWrite, locality)
+
+#endif
--- a/src/Backends/Audio/SDL2.cpp
+++ b/src/Backends/Audio/SDL2.cpp
@@ -8,6 +8,7 @@
 #include "../Misc.h"
 #include "../../Organya.h"
 #include "../../WindowsWrapper.h"
+#include "../../Attributes.h"
 
 #include "SoftwareMixer.h"
 
--- a/src/Backends/Audio/SoftwareMixer.cpp
+++ b/src/Backends/Audio/SoftwareMixer.cpp
@@ -1,21 +1,16 @@
 #include "SoftwareMixer.h"
 
-#include <math.h>
+#include <cmath>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "../../Attributes.h"
 
 #define MIN(a, b) ((a) < (b) ? (a) : (b))
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 #define CLAMP(x, y, z) MIN(MAX((x), (y)), (z))
 
-#ifdef __GNUC__
-#define ATTR_HOT __attribute__((hot))
-#else
-#define ATTR_HOT
-#endif
-
 struct Mixer_Sound
 {
 	unsigned char *samples;
@@ -144,7 +139,7 @@
 }
 
 // Most CPU-intensive function in the game (2/3rd CPU time consumption in my experience), so marked with attrHot so the compiler considers it a hot spot (as it is) when optimizing
-ATTR_HOT void Mixer_MixSounds(float *stream, unsigned int frames_total)
+ATTRIBUTE_HOT void Mixer_MixSounds(float *stream, unsigned int frames_total)
 {
 	for (Mixer_Sound *sound = sound_list_head; sound != NULL; sound = sound->next)
 	{
@@ -159,7 +154,7 @@
 				const float sample2 = (sound->samples[(size_t)sound->position + 1] - 128.0f) / 128.0f;
 
 				// Perform linear interpolation
-				const float interpolated_sample = sample1 + ((sample2 - sample1) * fmod(sound->position, 1.0));
+				const float interpolated_sample = sample1 + (sample2 - sample1) * (sound->position - std::trunc(sound->position));
 
 				*steam_pointer++ += interpolated_sample * sound->volume_l;
 				*steam_pointer++ += interpolated_sample * sound->volume_r;
@@ -170,7 +165,7 @@
 				{
 					if (sound->looping)
 					{
-						sound->position = fmod(sound->position, (double)sound->frames);
+						sound->position = std::fmod(sound->position, (double)sound->frames);
 					}
 					else
 					{
--- a/src/Backends/Rendering/Software.cpp
+++ b/src/Backends/Rendering/Software.cpp
@@ -7,6 +7,7 @@
 #include "SDL.h"
 
 #include "../../WindowsWrapper.h"
+#include "../../Attributes.h"
 
 #include "../Misc.h"
 #include "../SDL2/Window.h"
@@ -151,7 +152,7 @@
 	(void)height;
 }
 
-void RenderBackend_Blit(RenderBackend_Surface *source_surface, const RECT *rect, RenderBackend_Surface *destination_surface, long x, long y, BOOL colour_key)
+ATTRIBUTE_HOT void RenderBackend_Blit(RenderBackend_Surface *source_surface, const RECT *rect, RenderBackend_Surface *destination_surface, long x, long y, BOOL colour_key)
 {
 	if (source_surface == NULL || destination_surface == NULL)
 		return;
@@ -208,7 +209,7 @@
 
 			for (long i = 0; i < rect_clamped.right - rect_clamped.left; ++i)
 			{
-				if (source_pointer[0] == 0 && source_pointer[1] == 0 && source_pointer[2] == 0)	// Assumes the colour key will always be #000000 (black)
+				if (UNLIKELY(source_pointer[0] == 0 && source_pointer[1] == 0 && source_pointer[2] == 0))	// Assumes the colour key will always be #000000 (black)
 				{
 					source_pointer += 3;
 					destination_pointer += 3;
@@ -234,7 +235,7 @@
 	}
 }
 
-void RenderBackend_ColourFill(RenderBackend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue)
+ATTRIBUTE_HOT void RenderBackend_ColourFill(RenderBackend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue)
 {
 	if (surface == NULL)
 		return;
--- a/src/PixTone.cpp
+++ b/src/PixTone.cpp
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "Attributes.h"
 #include "WindowsWrapper.h"
 
 #include "Random.h"
@@ -65,7 +66,7 @@
 
 //BOOL wave_tables_made;
 
-BOOL MakePixelWaveData(const PIXTONEPARAMETER *ptp, unsigned char *pData)
+ATTRIBUTE_HOT BOOL MakePixelWaveData(const PIXTONEPARAMETER *ptp, unsigned char *pData)
 {
 	int i;
 	int a, b, c, d;