Merge pull request #96 from GabrielRavier/improvePerformance2

Improve performance for portable (again)
This commit is contained in:
Clownacy 2020-04-06 13:36:59 +01:00 committed by GitHub
commit 9b116dac59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 14 deletions

19
src/Attributes.h Normal file
View file

@ -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

View file

@ -8,6 +8,7 @@
#include "../Misc.h"
#include "../../Organya.h"
#include "../../WindowsWrapper.h"
#include "../../Attributes.h"
#include "SoftwareMixer.h"

View file

@ -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 @@ 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
{

View file

@ -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;

View file

@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>
#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;