From b36385e333b792b4be40eb1cf0ca81bb0b9de4c9 Mon Sep 17 00:00:00 2001 From: rlar Date: Tue, 22 Mar 2016 13:03:59 +0100 Subject: [PATCH] improve `SWAP' macro --- src/include/ngspice/macros.h | 7 ++++++- src/maths/sparse/spdefs.h | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/include/ngspice/macros.h b/src/include/ngspice/macros.h index 47d2886f6..4d2fafd0a 100644 --- a/src/include/ngspice/macros.h +++ b/src/include/ngspice/macros.h @@ -36,7 +36,12 @@ #define ABS(a) ((a) < 0.0 ? -(a) : (a)) #define SGN(a) ((a) < 0.0 ? -(1.0) : (1.0)) #define SIGN(a,b) ( b >= 0 ? (a >= 0 ? a : - a) : (a >= 0 ? - a : a)) -#define SWAP(type, a, b) {type swapx; swapx = a; a = b; b = swapx;} +#define SWAP(type, a, b) \ + do { \ + type SWAP_macro_local = a; \ + a = b; \ + b = SWAP_macro_local; \ + } while(0) #define ABORT() \ diff --git a/src/maths/sparse/spdefs.h b/src/maths/sparse/spdefs.h index 0eaf0b0a1..7efe6a2a8 100644 --- a/src/maths/sparse/spdefs.h +++ b/src/maths/sparse/spdefs.h @@ -78,7 +78,12 @@ #define SQR(a) ((a)*(a)) /* Macro procedure that swaps two entities. */ -#define SWAP(type, a, b) {type swapx; swapx = a; a = b; b = swapx;} +#define SWAP(type, a, b) \ + do { \ + type SWAP_macro_local = a; \ + a = b; \ + b = SWAP_macro_local; \ + } while(0)