Re: [hatari-devel] Removing SDL use for byte order checks / conversions

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


Dear Sirs:

An unused M_PI define in src/falcon/dsp_core.c is the only M_PI in
Hatari that is not my fault.

I have come to the conclusion that the entirety of computer science
has had decades to find a way of representing pi and has failed.
  Thus, the correct way of defining pi on a computer in C is as follows:
#define pi 3.14159265358979323846264338327950 /* use more digits if needed */
Note that pi is lower case because pi is fundamentally more important than
C convention.

Here are three patches to purge M_PI

On Mon, Oct 3, 2022 at 9:25 AM Christian Zietz <czietz@xxxxxxx> wrote:
David Savinkoff schrieb:

> I think MSFT is trying to be a trouble maker here, so use the force.

It's always easy and convenient to blame Microsoft, I know. But -- as
Thomas already wrote -- M_PI is *not* part of math.h in ANSI C. You will
find that glibc's math.h does not define it unconditionally either:
https://github.com/bminor/glibc/blob/b7bd94068e55d5fa8681e2ab4802d31852ea404e/math/math.h#L1144
.... it's just that __USE_MISC or __USE_XOPEN is set somewhere else.

> I would conditionally define M_PI most accurately in a future-proof way.

.... and, therefore, this suggestion might be good solution.

Regards
Christian
--
Christian Zietz  -  CHZ-Soft  -  czietz@xxxxxxx
WWW: https://www.chzsoft.de/
New GnuPG-Key-ID: 0x8708B34C827B159E



diff -ur a/src/sound.c b/src/sound.c
--- a/src/sound.c	2022-09-30 14:19:06.000000000 -0700
+++ b/src/sound.c	2022-10-04 13:39:26.323978687 -0700
@@ -410,7 +410,7 @@
 
 /**
  * Get coefficients for different Fs (C10 is in ST only):
- * Wc = 2*M_PI*4895.1;
+ * Wc = 2*pi*4895.1;
  * Fs = 44100;
  * warp = Wc/tanf((Wc/2)/Fs);
  * b = Wc/(warp+Wc);
diff -ur a/src/falcon/dsp_core.c b/src/falcon/dsp_core.c
--- a/src/falcon/dsp_core.c	2022-09-25 12:14:13.000000000 -0700
+++ b/src/falcon/dsp_core.c	2022-10-04 14:13:22.571914992 -0700
@@ -37,9 +37,7 @@
 dsp_core_t dsp_core;
 
 /*--- Defines ---*/
-#ifndef M_PI
-#define M_PI	3.141592653589793238462643383279502
-#endif
+#define pi	3.1415926535897932384626433832795028842
 
 /*--- Functions prototypes ---*/
 static void dsp_core_dsp2host(void);
diff -ur a/src/dmaSnd.c b/src/dmaSnd.c
--- a/src/dmaSnd.c	2022-10-01 12:31:17.000000000 -0700
+++ b/src/dmaSnd.c	2022-10-04 14:14:02.795913734 -0700
@@ -95,7 +95,6 @@
 
 const char DmaSnd_fileid[] = "Hatari dmaSnd.c";
 
-#include <SDL_stdinc.h>		/* Required for M_PI */
 #include "main.h"
 #include "audio.h"
 #include "configuration.h"
@@ -1355,6 +1354,7 @@
 	lmc1992.coef[4] = lmc1992.treb_table[set_treb].b1 * lmc1992.bass_table[set_bass].b1;
 }
 
+#define pi 3.1415926535897932384626433832795028842
 
 /**
  * Compute the first order bass shelf
@@ -1366,9 +1366,9 @@
 
 	/* g, fc, Fs must be positive real numbers > 0.0 */
 	if (g < 1.0)
-		bass.a1 = a1 = (tanf(M_PI*fc/Fs) - g  ) / (tanf(M_PI*fc/Fs) + g  );
+		bass.a1 = a1 = (tanf(pi*fc/Fs) - g  ) / (tanf(pi*fc/Fs) + g  );
 	else
-		bass.a1 = a1 = (tanf(M_PI*fc/Fs) - 1.0) / (tanf(M_PI*fc/Fs) + 1.0);
+		bass.a1 = a1 = (tanf(pi*fc/Fs) - 1.0) / (tanf(pi*fc/Fs) + 1.0);
 
 	bass.b0 = (1.0 + a1) * (g - 1.0) / 2.0 + 1.0;
 	bass.b1 = (1.0 + a1) * (g - 1.0) / 2.0 + a1;
@@ -1387,9 +1387,9 @@
 
 	/* g, fc, Fs must be positive real numbers > 0.0 */
 	if (g < 1.0)
-		treb.a1 = a1 = (g*tanf(M_PI*fc/Fs) - 1.0) / (g*tanf(M_PI*fc/Fs) + 1.0);
+		treb.a1 = a1 = (g*tanf(pi*fc/Fs) - 1.0) / (g*tanf(pi*fc/Fs) + 1.0);
 	else
-		treb.a1 = a1 =   (tanf(M_PI*fc/Fs) - 1.0) /   (tanf(M_PI*fc/Fs) + 1.0);
+		treb.a1 = a1 =   (tanf(pi*fc/Fs) - 1.0) /   (tanf(pi*fc/Fs) + 1.0);
 
 	treb.b0 = 1.0 + (1.0 - a1) * (g - 1.0) / 2.0;
 	treb.b1 = a1  + (a1 - 1.0) * (g - 1.0) / 2.0;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/