Hash :
c5a38976
Author :
Date :
2021-09-01T07:35:40
Capture/Replay: Add expression trigger validation calls. Setting the environment variable "ANGLE_CAPTURE_VALIDATION_EXPR" will make ANGLE's capture logic evaluate this expression every captured call to see if it should insert a validation checkpoint. The retracing script also accepts --validation-expr as an argument. For instance, the expression: ((frame == 2) && (call < 1189) && (call > 1100) && ((call % 5) == 0)) Will insert validation checkpoints on frame 2, between calls 1100 and 1189 and will validate every 5th call. The 'call' here is the count of captured calls, which are mostly GL calls with a few ANGLE replay calls in the mix. We add a small single-header library that can evaluate arthithmetic expressions in order to parse these expressions, as well as an option to the retracing script. Bug: angleproject:5133 Change-Id: Ic369e85d8e905a3a7a32fa098f7d8ebe7baf4ab9 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3136094 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Tim Van Patten <timvp@google.com> Reviewed-by: Cody Northrop <cnorthrop@google.com>
A C/C++ header for parsing and evaluation of arithmetic expressions.
[README file is almost identical to that of the <a href=”https://github.com/erstan/ceval#readme”>ceval</a> library]
| Function | Argument(s) | Return Value |
|---|---|---|
ceval_result() |
A mathematical expression in the form of a character array or a CPP string | The result of the expression as a floating point number |
ceval_tree() |
A mathematical expression in the form of a character array or a CPP string | The function prints the parse tree with each node properly indented depending on it's location in the tree structure |
Any valid combination of the following operators and functions, with floating point numbers as operands can be parsed by <b>ceval</b>. Parentheses can be used to override the default operator precedences.
+ (addition), - (subtraction), * (multiplication), / (division), % (modulo), ** (exponentiation), // (quotient)
== (equal), != (not equal), < (strictly less), > (strictly greater), <= (less or equal), >= (greater or equal) to compare the results of two expressions
exp(), sqrt(), cbrt(), sin(), cos(), tan(), asin(), acos(), atan(), sinh(), cosh(), tanh(), abs(), ceil(), floor(), log10(), ln(), deg2rad(), rad2deg(), signum(), int(), frac(), fact()
pow(), atan2(), gcd(), hcf(), lcm(), log() (generalized log(b, x) to any base b)
_pi, _e
…pre-defined constants are prefixed with an underscore
&&, || and !
&, |, ^, <<, >>, ~
, (Comma operator)
Comma operator returns the result of it’s rightmost operand
Ex: 2,3 would give 3; 4,3,0 would be equal to 0; and cos(_pi/2,_pi/3,_pi) would return cos(_pi) i.e, -1 e (e-operator for scientific notation)
Using the binary e operator, we can use scientific notation in our arithmetic expressions
Ex: 0.0314 could be written as 3.14e-2; 1230000 could be subsituted by 1.23e6
Include the ceval library using the #include "PATH_TO_CEVAL.H" directive your C/C++ project.
The code snippet given below is a console based interpreter that interactively takes in math expressions from stdin, and prints out their parse trees and results.
//lang=c
#include<stdio.h>
#include<stdlib.h>
#include "ceval.h"
int main(int argc, char ** argv) {
char expr[100];
while (1) {
printf("In = ");
fgets(expr, 100, stdin);
if (!strcmp(expr, "exit\n")) {
break;
} else if (!strcmp(expr, "clear\n")) {
system("clear");
continue;
} else {
ceval_tree(expr);
printf("\nOut = %f\n\n", ceval_result(expr));
}
}
return 0;
}
In = 3*7**2
2
**
7
*
3
Out = 147.000000
In = (3.2+2.8)/2
2
/
2.80
+
3.20
Out = 3.000000
In = _e**_pi>_pi**_e
2.72
**
3.14
>
3.14
**
2.72
Out = 1.000000
In = 5.4%2
2
%
5.40
Out = 1.400000
In = 5.4//2
2
//
5.40
Out = 2.000000
In = 2*2.0+1.4
1.40
+
2
*
2
Out = 5.400000
In = (5/4+3*-5)+(sin(_pi))**2+(cos(_pi))**2
2
**
3.14
cos
+
2
**
3.14
sin
+
5
-
*
3
+
4
/
5
Out = -12.750000
In = 3,4,5,6
6
,
5
,
4
,
3
Out = 6.000000
In = tanh(2/3)==(sinh(2/3)/cosh(2/3))
3
/
2
cosh
/
3
/
2
sinh
==
3
/
2
tanh
Out = 1.000000
In = (2+3/3+(3+9.7))
9.70
+
3
+
3
/
3
+
2
Out = 15.700000
In = sin(_pi/2)+cos(_pi/2)+tan(_pi/2)
2
/
3.14
tan
+
2
/
3.14
cos
+
2
/
3.14
sin
[ceval]: tan() is not defined for odd-integral multiples of _pi/2
Out = nan
In = asin(2)
2
asin
[ceval]: Numerical argument out of domain
Out = nan
In = exit
... Program finished with exit code 0
When the ceval.h file is included in a C-program, you might require the -lm flag to link math.h
gcc file.c -lm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
# ceval
A C/C++ header for parsing and evaluation of arithmetic expressions.
[README file is almost identical to that of the <a href="https://github.com/erstan/ceval#readme">ceval</a> library]
## Functions accessibe from main()
<table>
<thead><th>Function</th><th>Argument(s)</th><th>Return Value</th></thead>
<tbody>
<tr>
<td><code>ceval_result()</code></td>
<td>A mathematical expression in the form of a character array or a CPP string</td>
<td>The result of the expression as a floating point number</td>
</tr>
<tr>
<td><code>ceval_tree()</code></td>
<td>A mathematical expression in the form of a character array or a CPP string</td>
<td>The function prints the parse tree with each node properly indented depending on it's location in the tree structure</td>
</tr>
</tbody>
</table>
## Supported expressions
Any valid combination of the following operators and functions, with floating point numbers as operands can be parsed by <b>ceval</b>. Parentheses can be used to override the default operator precedences.
* Arithematic operators
`+` (addition), `-` (subtraction), `*` (multiplication), `/` (division), `%` (modulo), `**` (exponentiation), `//` (quotient)
* Relational operators
`==` (equal), `!=` (not equal), `<` (strictly less), `>` (strictly greater), `<=` (less or equal), `>=` (greater or equal) to compare the results of two expressions
* Single-argument functions
`exp()`, `sqrt()`, `cbrt()`, `sin()`, `cos()`, `tan()`, `asin()`, `acos()`, `atan()`, `sinh()`, `cosh()`, `tanh()`, `abs()`, `ceil()`, `floor()`, `log10()`, `ln()`, `deg2rad()`, `rad2deg()`, `signum()`, `int()`, `frac()`, `fact()`
* Two-argument functions
`pow()`, `atan2()`, `gcd()`, `hcf()`, `lcm()`, `log()` (generalized log(b, x) to any base `b`)
* Pre-defined math constants
`_pi`, `_e`
...pre-defined constants are prefixed with an underscore
* Logical operators
`&&`, `||` and `!`
* Bitwise operators
`&`, `|`, `^`, `<<`, `>>`, `~`
* Other operators
* `,` (Comma operator)
Comma operator returns the result of it's rightmost operand
Ex: `2,3` would give `3`; `4,3,0` would be equal to `0`; and `cos(_pi/2,_pi/3,_pi)` would return `cos(_pi)` i.e, `-1`
* `e` (e-operator for scientific notation)
Using the binary `e` operator, we can use scientific notation in our arithmetic expressions
Ex: `0.0314` could be written as `3.14e-2`; `1230000` could be subsituted by `1.23e6`
## Usage
Include the ceval library using the `#include "PATH_TO_CEVAL.H"` directive your C/C++ project.
The code snippet given below is a console based interpreter that interactively takes in math expressions from stdin, and prints out their parse trees and results.
```
//lang=c
#include<stdio.h>
#include<stdlib.h>
#include "ceval.h"
int main(int argc, char ** argv) {
char expr[100];
while (1) {
printf("In = ");
fgets(expr, 100, stdin);
if (!strcmp(expr, "exit\n")) {
break;
} else if (!strcmp(expr, "clear\n")) {
system("clear");
continue;
} else {
ceval_tree(expr);
printf("\nOut = %f\n\n", ceval_result(expr));
}
}
return 0;
}
```
## Test Run
```
In = 3*7**2
2
**
7
*
3
Out = 147.000000
In = (3.2+2.8)/2
2
/
2.80
+
3.20
Out = 3.000000
In = _e**_pi>_pi**_e
2.72
**
3.14
>
3.14
**
2.72
Out = 1.000000
In = 5.4%2
2
%
5.40
Out = 1.400000
In = 5.4//2
2
//
5.40
Out = 2.000000
In = 2*2.0+1.4
1.40
+
2
*
2
Out = 5.400000
In = (5/4+3*-5)+(sin(_pi))**2+(cos(_pi))**2
2
**
3.14
cos
+
2
**
3.14
sin
+
5
-
*
3
+
4
/
5
Out = -12.750000
In = 3,4,5,6
6
,
5
,
4
,
3
Out = 6.000000
In = tanh(2/3)==(sinh(2/3)/cosh(2/3))
3
/
2
cosh
/
3
/
2
sinh
==
3
/
2
tanh
Out = 1.000000
In = (2+3/3+(3+9.7))
9.70
+
3
+
3
/
3
+
2
Out = 15.700000
In = sin(_pi/2)+cos(_pi/2)+tan(_pi/2)
2
/
3.14
tan
+
2
/
3.14
cos
+
2
/
3.14
sin
[ceval]: tan() is not defined for odd-integral multiples of _pi/2
Out = nan
In = asin(2)
2
asin
[ceval]: Numerical argument out of domain
Out = nan
In = exit
... Program finished with exit code 0
```
## Note
When the `ceval.h` file is included in a C-program, you might require the `-lm` flag to link `math.h`
```shell
gcc file.c -lm
```