diff --git a/src/Attributes.h b/src/Attributes.h new file mode 100644 index 00000000..ac0b6b3c --- /dev/null +++ b/src/Attributes.h @@ -0,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 diff --git a/src/Backends/Audio/SDL2.cpp b/src/Backends/Audio/SDL2.cpp index 2f4b9745..f1054c21 100644 --- 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" diff --git a/src/Backends/Audio/SoftwareMixer.cpp b/src/Backends/Audio/SoftwareMixer.cpp index 0b3c87b3..4260eae4 100644 --- a/src/Backends/Audio/SoftwareMixer.cpp +++ b/src/Backends/Audio/SoftwareMixer.cpp @@ -1,21 +1,16 @@ #include "SoftwareMixer.h" -#include +#include #include #include #include #include +#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 @@ void Mixer_SetSoundPan(Mixer_Sound *sound, long pan) } // 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 @@ ATTR_HOT void Mixer_MixSounds(float *stream, unsigned int frames_total) 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 @@ ATTR_HOT void Mixer_MixSounds(float *stream, unsigned int frames_total) { if (sound->looping) { - sound->position = fmod(sound->position, (double)sound->frames); + sound->position = std::fmod(sound->position, (double)sound->frames); } else { diff --git a/src/Backends/Rendering/Software.cpp b/src/Backends/Rendering/Software.cpp index 0ad6c985..d534955a 100644 --- 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 RenderBackend_UnlockSurface(RenderBackend_Surface *surface, unsigned int wi (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 @@ void RenderBackend_Blit(RenderBackend_Surface *source_surface, const RECT *rect, 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_Blit(RenderBackend_Surface *source_surface, const RECT *rect, } } -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; diff --git a/src/PixTone.cpp b/src/PixTone.cpp index a2d06126..fd48a938 100644 --- a/src/PixTone.cpp +++ b/src/PixTone.cpp @@ -4,6 +4,7 @@ #include #include +#include "Attributes.h" #include "WindowsWrapper.h" #include "Random.h" @@ -65,7 +66,7 @@ void MakeWaveTables(void) //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;