Merge Freddie's bit_operations branch into 3.15

This commit is contained in:
Andrew Johnson
2020-04-14 15:28:22 -05:00
7 changed files with 88 additions and 48 deletions
+38 -23
View File
@@ -25,6 +25,7 @@
#include "postfix.h"
#include "postfixPvt.h"
static double calcRandom(void);
static int cond_search(const char **ppinst, int match);
@@ -48,7 +49,6 @@ epicsShareFunc long
double *ptop; /* stack pointer */
double top; /* value from top of stack */
epicsInt32 itop; /* integer from top of stack */
epicsUInt32 utop; /* unsigned integer from top of stack */
int op;
int nargs;
@@ -287,45 +287,60 @@ epicsShareFunc long
*ptop = ! *ptop;
break;
/* For bitwise operations on values with bit 31 set, double values
* must first be cast to unsigned to correctly set that bit; the
* double value must be negative in that case. The result must be
* cast to a signed integer before converting to the double result.
/* Be VERY careful converting double to int in case bit 31 is set!
* Out-of-range errors give very different results on different sytems.
* Convert negative doubles to signed and positive doubles to unsigned
* first to avoid overflows if bit 32 is set.
* The result is always signed, values with bit 31 set are negative
* to avoid problems when writing the value to signed integer fields
* like longout.VAL or ao.RVAL. However unsigned fields may give
* problems on some architectures. (Fewer than giving problems with
* signed integer. Maybe the conversion functions should handle
* overflows better.)
*/
#define d2i(x) ((x)<0?(epicsInt32)(x):(epicsInt32)(epicsUInt32)(x))
#define d2ui(x) ((x)<0?(epicsUInt32)(epicsInt32)(x):(epicsUInt32)(x))
case BIT_OR:
utop = *ptop--;
*ptop = (epicsInt32) ((epicsUInt32) *ptop | utop);
top = *ptop--;
*ptop = (double)(d2i(*ptop) | d2i(top));
break;
case BIT_AND:
utop = *ptop--;
*ptop = (epicsInt32) ((epicsUInt32) *ptop & utop);
top = *ptop--;
*ptop = (double)(d2i(*ptop) & d2i(top));
break;
case BIT_EXCL_OR:
utop = *ptop--;
*ptop = (epicsInt32) ((epicsUInt32) *ptop ^ utop);
top = *ptop--;
*ptop = (double)(d2i(*ptop) ^ d2i(top));
break;
case BIT_NOT:
utop = *ptop;
*ptop = (epicsInt32) ~utop;
*ptop = (double)~d2i(*ptop);
break;
/* The shift operators use signed integers, so a right-shift will
* extend the sign bit into the left-hand end of the value. The
* double-casting through unsigned here is important, see above.
/* In C the shift operators decide on an arithmetic or logical shift
* based on whether the integer is signed or unsigned.
* With signed integers, a right-shift is arithmetic and will
* extend the sign bit into the left-hand end of the value. When used
* with unsigned values a logical shift is performed. The
* double-casting through signed/unsigned here is important, see above.
*/
case RIGHT_SHIFT:
utop = *ptop--;
*ptop = ((epicsInt32) (epicsUInt32) *ptop) >> (utop & 31);
case RIGHT_SHIFT_ARITH:
top = *ptop--;
*ptop = (double)(d2i(*ptop) >> (d2i(top) & 31));
break;
case LEFT_SHIFT:
utop = *ptop--;
*ptop = ((epicsInt32) (epicsUInt32) *ptop) << (utop & 31);
case LEFT_SHIFT_ARITH:
top = *ptop--;
*ptop = (double)(d2i(*ptop) << (d2i(top) & 31));
break;
case RIGHT_SHIFT_LOGIC:
top = *ptop--;
*ptop = (double)(d2ui(*ptop) >> (d2ui(top) & 31u));
break;
case NOT_EQ:
@@ -382,11 +397,11 @@ epicsShareFunc long
*presult = *ptop;
return 0;
}
#if defined(_WIN32) && defined(_M_X64) && !defined(_MINGW)
# pragma optimize("", on)
#endif
epicsShareFunc long
calcArgUsage(const char *pinst, unsigned long *pinputs, unsigned long *pstores)
{
+6 -4
View File
@@ -148,13 +148,14 @@ static const ELEMENT operators[] = {
{":=", 0, 0, -1, STORE_OPERATOR, STORE_A},
{";", 0, 0, 0, EXPR_TERMINATOR,NOT_GENERATED},
{"<", 3, 3, -1, BINARY_OPERATOR,LESS_THAN},
{"<<", 2, 2, -1, BINARY_OPERATOR,LEFT_SHIFT},
{"<<", 2, 2, -1, BINARY_OPERATOR,LEFT_SHIFT_ARITH},
{"<=", 3, 3, -1, BINARY_OPERATOR,LESS_OR_EQ},
{"=", 3, 3, -1, BINARY_OPERATOR,EQUAL},
{"==", 3, 3, -1, BINARY_OPERATOR,EQUAL},
{">", 3, 3, -1, BINARY_OPERATOR,GR_THAN},
{">=", 3, 3, -1, BINARY_OPERATOR,GR_OR_EQ},
{">>", 2, 2, -1, BINARY_OPERATOR,RIGHT_SHIFT},
{">>", 2, 2, -1, BINARY_OPERATOR,RIGHT_SHIFT_ARITH},
{">>>", 2, 2, -1, BINARY_OPERATOR,RIGHT_SHIFT_LOGIC},
{"?", 0, 0, -1, CONDITIONAL, COND_IF},
{"AND", 2, 2, -1, BINARY_OPERATOR,BIT_AND},
{"OR", 1, 1, -1, BINARY_OPERATOR,BIT_OR},
@@ -579,8 +580,9 @@ epicsShareFunc void
"BIT_AND",
"BIT_EXCL_OR",
"BIT_NOT",
"RIGHT_SHIFT",
"LEFT_SHIFT",
"RIGHT_SHIFT_ARITH",
"LEFT_SHIFT_ARITH",
"RIGHT_SHIFT_LOGIC",
/* Relationals */
"NOT_EQ",
"LESS_THAN",
+3 -2
View File
@@ -84,8 +84,9 @@ typedef enum {
BIT_AND,
BIT_EXCL_OR,
BIT_NOT,
RIGHT_SHIFT,
LEFT_SHIFT,
RIGHT_SHIFT_ARITH,
LEFT_SHIFT_ARITH,
RIGHT_SHIFT_LOGIC,
/* Relationals */
NOT_EQ,
LESS_THAN,
+22 -6
View File
@@ -104,7 +104,7 @@ void testUInt32Calc(const char *expr, epicsUInt32 expected) {
testDiag("calcPerform: error evaluating '%s'", expr);
}
uresult = (epicsUInt32) result;
uresult = (result < 0.0 ? (epicsUInt32)(epicsInt32)result : (epicsUInt32)result);
pass = (uresult == expected);
if (!testOk(pass, "%s", expr)) {
testDiag("Expected result is 0x%x (%u), actually got 0x%x (%u)",
@@ -297,7 +297,7 @@ MAIN(epicsCalcTest)
const double a=1.0, b=2.0, c=3.0, d=4.0, e=5.0, f=6.0,
g=7.0, h=8.0, i=9.0, j=10.0, k=11.0, l=12.0;
testPlan(613);
testPlan(630);
/* LITERAL_OPERAND elements */
testExpr(0);
@@ -612,14 +612,14 @@ MAIN(epicsCalcTest)
testExpr(0.0 + NaN);
testExpr(Inf + 0.0);
testExpr(Inf + Inf);
#if defined(_WIN64) && defined(_MSC_VER)
#if defined(_WIN32) && defined(_MSC_VER)
testCalc("Inf + -Inf", NaN);
#else
testExpr(Inf + -Inf);
#endif
testExpr(Inf + NaN);
testExpr(-Inf + 0.0);
#if defined(_WIN64) && defined(_MSC_VER)
#if defined(_WIN32) && defined(_MSC_VER)
testCalc("-Inf + Inf", NaN);
#else
testExpr(-Inf + Inf);
@@ -688,7 +688,7 @@ MAIN(epicsCalcTest)
testExpr(NaN < NaN);
testExpr(1 << 2);
testExpr(1 << 3 << 2)
testExpr(1 << 3 << 2);
testExpr(0 <= 1);
testExpr(0 <= 0);
@@ -776,7 +776,9 @@ MAIN(epicsCalcTest)
testExpr(NaN >= NaN);
testExpr(8 >> 1);
testCalc("8 >>> 1", 8u >> 1u);
testExpr(64 >> 2 >> 1);
testCalc("64 >>> 2 >>> 1", 64u >> 2u >> 1u);
testExpr(7 AND 4);
@@ -875,11 +877,14 @@ MAIN(epicsCalcTest)
testExpr(3 << 2 & 10); // 2 2
testCalc("18 & 6 << 2", (18 & 6) << 2); // 2 2
testExpr(36 >> 2 & 10); // 2 2
testCalc("36 >>> 2 & 10", 36u >> 2u & 10u); // 2 2
testCalc("18 & 20 >> 2", (18 & 20) >> 2); // 2 2
testCalc("18 & 20 >>> 2", (18u & 20u) >> 2u); // 2 2
testExpr(3 & 4 == 4); // 2 3
testExpr(3 AND 4 == 4); // 2 3
testCalc("1 << 2 != 4", 1 << (2 != 4)); // 2 3
testCalc("16 >> 2 != 4", 16 >> (2 != 4)); // 2 3
testCalc("16 >>> 2 != 4", 16u >> (2u != 4u)); // 2 3
testExpr(3 AND -2); // 2 8
testExpr(0 < 1 ? 2 : 3); // 3 0
testExpr(1 <= 0 ? 2 : 3); // 3 0
@@ -951,7 +956,11 @@ MAIN(epicsCalcTest)
testUInt32Calc("~0xaaaaaaaa", 0x55555555u);
testUInt32Calc("~~0xaaaaaaaa", 0xaaaaaaaau);
testUInt32Calc("0xaaaaaaaa >> 8", 0xffaaaaaau);
testUInt32Calc("0x55555555 >> 8", 0x00555555u);
testUInt32Calc("0xaaaaaaaa >>> 8", 0x00aaaaaau);
testUInt32Calc("0x55555555 >>> 8", 0x00555555u);
testUInt32Calc("0xaaaaaaaa << 8", 0xaaaaaa00u);
testUInt32Calc("0x55555555 << 8", 0x55555500u);
// using integer literals assigned to variables
testUInt32Calc("a:=0xaaaaaaaa; b:=0xffff0000; a AND b", 0xaaaa0000u);
testUInt32Calc("a:=0xaaaaaaaa; b:=0xffff0000; a OR b", 0xffffaaaau);
@@ -959,7 +968,11 @@ MAIN(epicsCalcTest)
testUInt32Calc("a:=0xaaaaaaaa; ~a", 0x55555555u);
testUInt32Calc("a:=0xaaaaaaaa; ~~a", 0xaaaaaaaau);
testUInt32Calc("a:=0xaaaaaaaa; a >> 8", 0xffaaaaaau);
testUInt32Calc("a:=0xaaaaaaaa; a >>> 8", 0x00aaaaaau);
testUInt32Calc("a:=0xaaaaaaaa; a << 8", 0xaaaaaa00u);
testUInt32Calc("a:=0x55555555; a >> 8", 0x00555555u);
testUInt32Calc("a:=0x55555555; a >>> 8", 0x00555555u);
testUInt32Calc("a:=0x55555555; a << 8", 0x55555500u);
// Test proper conversion of double values (+ 0.1 enforces double literal)
// when used as inputs to the bitwise operations.
@@ -979,9 +992,13 @@ MAIN(epicsCalcTest)
testUInt32Calc("~ -1431655766.1", 0x55555555u);
testUInt32Calc("~ 2863311530.1", 0x55555555u);
testUInt32Calc("-1431655766.1 >> 0", 0xaaaaaaaau);
testUInt32Calc("-1431655766.1 >>> 0", 0xaaaaaaaau);
testUInt32Calc("2863311530.1 >> 0", 0xaaaaaaaau);
testUInt32Calc("2863311530.1 >>> 0", 0xaaaaaaaau);
testUInt32Calc("-1431655766.1 >> 0.1", 0xaaaaaaaau);
testUInt32Calc("-1431655766.1 >>> 0.1", 0xaaaaaaaau);
testUInt32Calc("2863311530.1 >> 0.1", 0xaaaaaaaau);
testUInt32Calc("2863311530.1 >>> 0.1", 0xaaaaaaaau);
testUInt32Calc("-1431655766.1 << 0", 0xaaaaaaaau);
testUInt32Calc("2863311530.1 << 0", 0xaaaaaaaau);
testUInt32Calc("-1431655766.1 << 0.1", 0xaaaaaaaau);
@@ -989,4 +1006,3 @@ MAIN(epicsCalcTest)
return testDone();
}
+9 -9
View File
@@ -32,23 +32,23 @@ MAIN(epicsMathTest)
testOk1(epicsINF > 0.0);
testOk1(epicsINF - epicsINF != 0.0);
#if defined(_WIN64) && defined(_MSC_VER)
testTodoBegin("Known failure on windows-x64");
#if defined(_WIN32) && defined(_MSC_VER)
testTodoBegin("Known failure on windows-x64 and win32-x86");
#endif
testOk1(epicsINF + -epicsINF != 0.0);
testOk1(-epicsINF + epicsINF != 0.0);
#if defined(_WIN64) && defined(_MSC_VER)
#if defined(_WIN32) && defined(_MSC_VER)
testTodoEnd();
#endif
testOk1(isnan(epicsINF - epicsINF));
#if defined(_WIN64) && defined(_MSC_VER)
testTodoBegin("Known failure on windows-x64");
#if defined(_WIN32) && defined(_MSC_VER)
testTodoBegin("Known failure on windows-x64 and win32-x86");
#endif
testOk1(isnan(epicsINF + -epicsINF));
testOk1(isnan(-epicsINF + epicsINF));
#if defined(_WIN64) && defined(_MSC_VER)
#if defined(_WIN32) && defined(_MSC_VER)
testTodoEnd();
#endif
@@ -62,12 +62,12 @@ MAIN(epicsMathTest)
testOk1(!(epicsNAN > epicsNAN));
testOk1(isnan(epicsNAN - epicsNAN));
#if defined(_WIN64) && defined(_MSC_VER)
testTodoBegin("Known failure on windows-x64");
#if defined(_WIN32) && defined(_MSC_VER)
testTodoBegin("Known failure on windows-x64 and win32-x86");
#endif
testOk1(isnan(epicsNAN + -epicsNAN));
testOk1(isnan(-epicsNAN + epicsNAN));
#if defined(_WIN64) && defined(_MSC_VER)
#if defined(_WIN32) && defined(_MSC_VER)
testTodoEnd();
#endif
+5 -2
View File
@@ -318,10 +318,13 @@ XOR : Bitwise Exclusive Or
C<~> : One's Complement
=item *
C<<< << >>> : Left shift
C<<< << >>> : Arithmetic Left Shift
=item *
C<<< >> >>> : Right shift
C<<< >> >>> : Arithmetic Right Shift
=item *
C<<<< >>> >>>> : Logical Right Shift
=back
+5 -2
View File
@@ -350,10 +350,13 @@ XOR : Bitwise Exclusive Or
C<~> : One's Complement
=item *
C<<< << >>> : Left shift
C<<< << >>> : Arithmetic Left Shift
=item *
C<<< >> >>> : Right shift
C<<< >> >>> : Arithmetic Right Shift
=item *
C<<<< >>> >>>> : Logical Right Shift
=back