1
1
#include "expression.h"
2
+ #include "fixed-point.h"
2
3
3
4
#include <linux/ctype.h> /* for isspace */
4
5
#include <linux/kernel.h>
@@ -172,28 +173,52 @@ static enum expr_type expr_op(const char *s, size_t len, int unary)
172
173
return OP_UNKNOWN ;
173
174
}
174
175
175
- static int expr_parse_number (const char * s , size_t len )
176
+ static uint64_t expr_parse_number (const char * s , size_t len )
176
177
{
177
- int num = 0 ;
178
+ fixedp num = { 0 } ;
178
179
int frac = 0 ;
179
180
int dot = 0 ; /* FIXME: not good enough */
180
181
unsigned int digits = 0 ;
181
182
for (unsigned int i = 0 ; i < len ; i ++ ) {
182
183
if (s [i ] == '.' && dot == 0 ) {
183
- dot = 1 ;
184
+ dot = i + 1 ;
184
185
continue ;
185
186
}
186
187
if (isdigit (s [i ])) {
187
- digits ++ ;
188
188
if (dot )
189
- -- frac ;
190
- num = num * 10 + (s [i ] - '0' );
189
+ frac ++ ;
190
+ else
191
+ digits ++ ;
192
+ // num = num * 10 + (s[i] - '0');
191
193
} else
192
194
return NAN_INT ;
193
195
}
194
196
195
- num = FP2INT (num , frac );
196
- return (digits > 0 ? num : NAN_INT );
197
+ static int pow10 [] = {
198
+ 1 , 10 , 100 , 1000 , 10000 ,
199
+ 100000 , 1000000 , 10000000 , 100000000 , 1000000000 ,
200
+ };
201
+
202
+ if (dot ) {
203
+ uint32_t ipt = 0 , tmp ;
204
+ uint32_t mask = pow10 [frac ];
205
+ int i = 31 ;
206
+ sscanf (s , "%u.%u" , & tmp , & ipt );
207
+ pr_alert ("parse integer: %u" , ipt );
208
+ while (ipt && i ) {
209
+ ipt <<= 1 ;
210
+ if (ipt >= mask ) {
211
+ num .frac |= 1 << i ;
212
+ ipt %= mask ;
213
+ }
214
+ i -- ;
215
+ }
216
+ }
217
+
218
+ if (digits )
219
+ sscanf (s , "%u" , & num .inte );
220
+
221
+ return (digits > 0 ? num .data : NAN_INT );
197
222
}
198
223
199
224
/*
@@ -233,36 +258,19 @@ struct expr_var *expr_var(struct expr_var_list *vars, const char *s, size_t len)
233
258
return v ;
234
259
}
235
260
236
- static int mult (int a , int b )
261
+ static uint64_t mult (uint64_t a , uint64_t b )
237
262
{
238
- int frac1 = GET_FRAC (a );
239
- int frac2 = GET_FRAC (b );
240
- int n1 = GET_NUM (a );
241
- int n2 = GET_NUM (b );
242
- int n3 = n1 * n2 ;
243
-
244
- return FP2INT (n3 , (frac1 + frac2 ));
263
+ /* (a + b) * (c + d) = ac + ad + bc + bd */
264
+ fixedp result = {0 };
265
+ fixedp fa = {.data = a }, fb = {.data = b };
266
+ result .data = a * b ;
267
+ result .inte = fa .inte * fb .inte ;
268
+ return result .data ;
245
269
}
246
270
247
- static int divid (int a , int b )
271
+ static int divid (uint64_t a , uint64_t b )
248
272
{
249
- int frac1 = GET_FRAC (a );
250
- int frac2 = GET_FRAC (b );
251
- int n1 = GET_NUM (a );
252
- int n2 = GET_NUM (b );
253
- if (n1 == 0 && n2 == 0 )
254
- return NAN_INT ;
255
- if (n2 == 0 )
256
- return INF_INT ;
257
-
258
- while (n1 * 10 < ((1 << 25 ) - 1 )) {
259
- -- frac1 ;
260
- n1 *= 10 ;
261
- }
262
- int n3 = n1 / n2 ;
263
- int frac3 = frac1 - frac2 ;
264
-
265
- return FP2INT (n3 , frac3 );
273
+ return (a << 32 ) / (b << 32 );
266
274
}
267
275
268
276
static int remain (int a , int b )
@@ -289,10 +297,10 @@ static int remain(int a, int b)
289
297
return FP2INT (n1 , frac1 );
290
298
}
291
299
292
- static int right_shift (int a , int b )
300
+ static uint64_t right_shift (uint64_t a , int b )
293
301
{
294
302
/* FIXME: should use 2-base? */
295
- return divid ( a , mult ( 2 << 4 , b )) ;
303
+ return a >> b ;
296
304
}
297
305
298
306
static int power (int a , int b )
@@ -322,77 +330,30 @@ static int power(int a, int b)
322
330
return FP2INT (n1 , frac1 );
323
331
}
324
332
325
- static int left_shift (int a , int b )
333
+ static uint64_t left_shift (uint64_t a , int b )
326
334
{
327
335
/* FIXME: should use 2-base? */
328
- return mult ( a , power ( 2 << 4 , b )) ;
336
+ return a << b ;
329
337
}
330
338
331
- static int plus (int a , int b )
339
+ static uint64_t plus (uint64_t a , uint64_t b )
332
340
{
333
- int frac1 = GET_FRAC (a );
334
- int frac2 = GET_FRAC (b );
335
- int n1 = GET_NUM (a );
336
- int n2 = GET_NUM (b );
337
-
338
- while (frac1 != frac2 ) {
339
- if (frac1 > frac2 ) {
340
- -- frac1 ;
341
- n1 *= 10 ;
342
- } else if (frac1 < frac2 ) {
343
- -- frac2 ;
344
- n2 *= 10 ;
345
- }
346
- }
347
-
348
- n1 += n2 ;
349
-
350
- return FP2INT (n1 , frac1 );
341
+ return a + b ;
351
342
}
352
343
353
- static int minus (int a , int b )
344
+ static uint64_t minus (uint64_t a , uint64_t b )
354
345
{
355
- int frac1 = GET_FRAC (a );
356
- int frac2 = GET_FRAC (b );
357
- int n1 = GET_NUM (a );
358
- int n2 = GET_NUM (b );
359
-
360
- while (frac1 != frac2 ) {
361
- if (frac1 > frac2 ) {
362
- -- frac1 ;
363
- n1 *= 10 ;
364
- } else {
365
- -- frac2 ;
366
- n2 *= 10 ;
367
- }
368
- }
369
-
370
- n1 -= n2 ;
371
- return FP2INT (n1 , frac1 );
346
+ return a - b ;
372
347
}
373
348
374
- static int compare (int a , int b )
349
+ static int compare (uint64_t a , uint64_t b )
375
350
{
376
- int frac1 = GET_FRAC (a );
377
- int frac2 = GET_FRAC (b );
378
- int n1 = GET_NUM (a );
379
- int n2 = GET_NUM (b );
380
- while (frac1 != frac2 ) {
381
- if (frac1 > frac2 ) {
382
- -- frac1 ;
383
- n1 *= 10 ;
384
- } else {
385
- -- frac2 ;
386
- n2 *= 10 ;
387
- }
388
- }
389
-
390
351
int flags = 0 ;
391
- if (n1 < n2 )
352
+ if (a < b )
392
353
flags |= LOWER ;
393
- if (n1 > n2 )
354
+ if (a > b )
394
355
flags |= GREATER ;
395
- if (n1 == n2 )
356
+ if (a == b )
396
357
flags |= EQUAL ;
397
358
398
359
return flags ;
@@ -443,21 +404,18 @@ static int bitwise_op(int a, int b, int op)
443
404
return FP2INT (n1 , frac1 );
444
405
}
445
406
446
- static int not (int a )
407
+ static uint64_t not (uint64_t a )
447
408
{
448
- int frac = GET_FRAC (a );
449
- int n = GET_NUM (a );
450
-
451
- return FP2INT (!n , frac );
409
+ return !a ;
452
410
}
453
411
454
412
/* TODO: change logic */
455
- int expr_eval (struct expr * e )
413
+ uint64_t expr_eval (struct expr * e )
456
414
{
457
415
int n ;
458
416
switch (e -> type ) {
459
417
case OP_UNARY_MINUS : /* OK */
460
- return minus (FP2INT ( 0 , 0 ) , expr_eval (& e -> param .op .args .buf [0 ]));
418
+ return minus (0 , expr_eval (& e -> param .op .args .buf [0 ]));
461
419
case OP_UNARY_LOGICAL_NOT : /* OK */
462
420
return not (expr_eval (& e -> param .op .args .buf [0 ]));
463
421
case OP_UNARY_BITWISE_NOT : /* OK */
@@ -511,14 +469,14 @@ int expr_eval(struct expr *e)
511
469
expr_eval (& e -> param .op .args .buf [1 ])) &
512
470
(EQUAL )));
513
471
case OP_BITWISE_AND : /* OK */
514
- return ( bitwise_op ( expr_eval (& e -> param .op .args .buf [0 ]),
515
- expr_eval (& e -> param .op .args .buf [1 ]), 0 ) );
472
+ return expr_eval (& e -> param .op .args .buf [0 ]) &
473
+ expr_eval (& e -> param .op .args .buf [1 ]);
516
474
case OP_BITWISE_OR : /* OK */
517
- return MASK ( bitwise_op ( expr_eval (& e -> param .op .args .buf [0 ]),
518
- expr_eval (& e -> param .op .args .buf [1 ]), 1 ) );
475
+ return expr_eval (& e -> param .op .args .buf [0 ]) |
476
+ expr_eval (& e -> param .op .args .buf [1 ]);
519
477
case OP_BITWISE_XOR : /* OK */
520
- return ( bitwise_op ( expr_eval (& e -> param .op .args .buf [0 ]),
521
- expr_eval (& e -> param .op .args .buf [1 ]), 2 ) );
478
+ return expr_eval (& e -> param .op .args .buf [0 ]) ^
479
+ expr_eval (& e -> param .op .args .buf [1 ]);
522
480
case OP_LOGICAL_AND : /* OK */
523
481
n = expr_eval (& e -> param .op .args .buf [0 ]);
524
482
if (GET_NUM (n ) != 0 ) {
@@ -668,7 +626,7 @@ static int expr_bind(const char *s, size_t len, vec_expr_t *es)
668
626
return 0 ;
669
627
}
670
628
671
- static struct expr expr_const (int value )
629
+ static struct expr expr_const (uint64_t value )
672
630
{
673
631
struct expr e = expr_init ();
674
632
e .type = OP_CONST ;
@@ -731,7 +689,7 @@ struct expr *expr_create(const char *s,
731
689
struct expr_var_list * vars ,
732
690
struct expr_func * funcs )
733
691
{
734
- int num ;
692
+ uint64_t num ;
735
693
struct expr_var * v ;
736
694
const char * id = NULL ;
737
695
size_t idn = 0 ;
0 commit comments