-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathvalue.c
More file actions
933 lines (778 loc) · 26 KB
/
value.c
File metadata and controls
933 lines (778 loc) · 26 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
#include "rc_internal.h"
#include <string.h> /* memset */
#include <ctype.h> /* isdigit */
#include <float.h> /* FLT_EPSILON */
#include <math.h> /* fmod */
int rc_is_valid_variable_character(char ch, int is_first) {
if (is_first) {
if (!isalpha((unsigned char)ch))
return 0;
}
else {
if (!isalnum((unsigned char)ch))
return 0;
}
return 1;
}
static void rc_parse_cond_value(rc_value_t* self, const char** memaddr, rc_parse_state_t* parse) {
rc_condset_t** next_clause;
next_clause = &self->conditions;
do
{
parse->measured_target = 0; /* passing is_value=1 should prevent any conflicts, but clear it out anyway */
*next_clause = rc_parse_condset(memaddr, parse);
if (parse->offset < 0) {
return;
}
if (**memaddr == 'S' || **memaddr == 's') {
/* alt groups not supported */
parse->offset = RC_INVALID_VALUE_FLAG;
}
else if (parse->measured_target == 0) {
parse->offset = RC_MISSING_VALUE_MEASURED;
}
else if (**memaddr == '$') {
/* maximum of */
++(*memaddr);
next_clause = &(*next_clause)->next;
continue;
}
break;
} while (1);
(*next_clause)->next = 0;
}
static void rc_parse_legacy_value(rc_value_t* self, const char** memaddr, rc_parse_state_t* parse) {
rc_condset_with_trailing_conditions_t* condset_with_conditions;
rc_condition_t** next;
rc_condset_t** next_clause;
rc_condset_t* condset;
rc_condition_t local_cond;
rc_condition_t* cond;
uint32_t num_measured_conditions;
char buffer[64] = "A:";
const char* buffer_ptr;
char* ptr;
int done;
/* convert legacy format into condset */
next_clause = &self->conditions;
do {
/* count the number of joiners and add one to determine the number of clauses. */
buffer[0] = 'A'; /* reset to AddSource */
done = 0;
num_measured_conditions = 1;
buffer_ptr = *memaddr;
do {
switch (*buffer_ptr++) {
case '_': /* add next */
++num_measured_conditions;
buffer[0] = 'A'; /* reset to AddSource */
break;
case '*': /* multiply */
if (*buffer_ptr == '-') {
/* multiplication by a negative number will convert to SubSource */
++buffer_ptr;
buffer[0] = 'B';
}
break;
case '\0': /* end of string */
case '$': /* maximum of */
case ':': /* end of leaderboard clause */
case ')': /* end of rich presence macro */
done = 1;
break;
default: /* assume everything else is valid - bad stuff will be filtered out later */
break;
}
} while (!done);
/* if last condition is not AddSource, we'll need to add a dummy condition for the Measured */
if (buffer[0] != 'A')
++num_measured_conditions;
condset_with_conditions = RC_ALLOC_WITH_TRAILING(rc_condset_with_trailing_conditions_t,
rc_condition_t, conditions, num_measured_conditions, parse);
if (parse->offset < 0)
return;
condset = (rc_condset_t*)condset_with_conditions;
memset(condset, 0, sizeof(*condset));
condset->num_measured_conditions = num_measured_conditions;
cond = &condset_with_conditions->conditions[0];
next = &condset->conditions;
for (;; ++(*memaddr)) {
buffer[0] = 'A'; /* reset to AddSource */
ptr = &buffer[2];
/* extract the next clause */
for (;; ++(*memaddr)) {
switch (**memaddr) {
case '_': /* add next */
*ptr = '\0';
break;
case '$': /* maximum of */
case '\0': /* end of string */
case ':': /* end of leaderboard clause */
case ')': /* end of rich presence macro */
/* the last condition needs to be Measured - AddSource can be changed here,
* SubSource will be handled later */
if (buffer[0] == 'A')
buffer[0] = 'M';
*ptr = '\0';
break;
case '*':
*ptr++ = '*';
buffer_ptr = *memaddr + 1;
if (*buffer_ptr == '-') {
buffer[0] = 'B'; /* change to SubSource */
++(*memaddr); /* don't copy sign */
++buffer_ptr; /* ignore sign when doing floating point check */
}
else if (*buffer_ptr == '+') {
++buffer_ptr; /* ignore sign when doing floating point check */
}
/* if it looks like a floating point number, add the 'f' prefix */
while (isdigit((unsigned char)*buffer_ptr))
++buffer_ptr;
if (*buffer_ptr == '.')
*ptr++ = 'f';
continue;
default:
*ptr++ = **memaddr;
continue;
}
break;
}
/* process the clause */
if (!parse->buffer)
cond = &local_cond;
buffer_ptr = buffer;
rc_parse_condition_internal(cond, &buffer_ptr, parse);
if (parse->offset < 0)
return;
if (*buffer_ptr) {
/* whatever we copied as a single condition was not fully consumed */
parse->offset = RC_INVALID_COMPARISON;
return;
}
if (!rc_operator_is_modifying(cond->oper)) {
parse->offset = RC_INVALID_OPERATOR;
return;
}
rc_condition_update_parse_state(cond, parse);
*next = cond;
next = &cond->next;
if (**memaddr != '_') /* add next */
break;
++cond;
}
/* -- end of clause -- */
/* clause must end in a Measured. if it doesn't, append one */
if (cond->type != RC_CONDITION_MEASURED) {
if (!parse->buffer)
cond = &local_cond;
else
++cond;
buffer_ptr = "M:0";
rc_parse_condition_internal(cond, &buffer_ptr, parse);
*next = cond;
next = &cond->next;
rc_condition_update_parse_state(cond, parse);
}
*next = NULL;
/* finalize clause */
*next_clause = condset;
next_clause = &condset->next;
if (**memaddr != '$') {
/* end of valid string */
*next_clause = NULL;
break;
}
/* max of ($), start a new clause */
++(*memaddr);
} while (1);
}
void rc_parse_value_internal(rc_value_t* self, const char** memaddr, rc_parse_state_t* parse) {
const uint8_t was_value = parse->is_value;
const rc_condition_t* condition;
parse->is_value = 1;
/* if it starts with a condition flag (M: A: B: C:), parse the conditions */
if ((*memaddr)[1] == ':')
rc_parse_cond_value(self, memaddr, parse);
else
rc_parse_legacy_value(self, memaddr, parse);
if (parse->offset >= 0 && parse->buffer) {
self->name = "(unnamed)";
self->value.value = self->value.prior = 0;
self->value.memref_type = RC_MEMREF_TYPE_VALUE;
self->value.changed = 0;
self->has_memrefs = 0;
for (condition = self->conditions->conditions; condition; condition = condition->next) {
if (condition->type == RC_CONDITION_MEASURED) {
if (rc_operand_is_float(&condition->operand1)) {
self->value.size = RC_MEMSIZE_FLOAT;
self->value.type = RC_VALUE_TYPE_FLOAT;
}
else {
self->value.size = RC_MEMSIZE_32_BITS;
self->value.type = RC_VALUE_TYPE_UNSIGNED;
}
break;
}
}
}
parse->is_value = was_value;
}
int rc_value_size(const char* memaddr) {
rc_value_with_memrefs_t* value;
rc_preparse_state_t preparse;
rc_init_preparse_state(&preparse);
value = RC_ALLOC(rc_value_with_memrefs_t, &preparse.parse);
rc_parse_value_internal(&value->value, &memaddr, &preparse.parse);
rc_preparse_alloc_memrefs(NULL, &preparse);
rc_destroy_preparse_state(&preparse);
return preparse.parse.offset;
}
rc_value_t* rc_parse_value(void* buffer, const char* memaddr, void* unused_L, int unused_funcs_idx) {
rc_value_with_memrefs_t* value;
rc_preparse_state_t preparse;
const char* preparse_memaddr = memaddr;
(void)unused_L;
(void)unused_funcs_idx;
if (!buffer || !memaddr)
return NULL;
rc_init_preparse_state(&preparse);
value = RC_ALLOC(rc_value_with_memrefs_t, &preparse.parse);
rc_parse_value_internal(&value->value, &preparse_memaddr, &preparse.parse);
rc_reset_parse_state(&preparse.parse, buffer);
value = RC_ALLOC(rc_value_with_memrefs_t, &preparse.parse);
rc_preparse_alloc_memrefs(&value->memrefs, &preparse);
rc_parse_value_internal(&value->value, &memaddr, &preparse.parse);
value->value.has_memrefs = 1;
rc_destroy_preparse_state(&preparse);
return (preparse.parse.offset >= 0) ? &value->value : NULL;
}
static void rc_update_value_memrefs(rc_value_t* self, rc_peek_t peek, void* ud) {
if (self->has_memrefs) {
rc_value_with_memrefs_t* value = (rc_value_with_memrefs_t*)self;
rc_update_memref_values(&value->memrefs, peek, ud);
}
}
int rc_evaluate_value_typed(rc_value_t* self, rc_typed_value_t* value, rc_peek_t peek, void* ud) {
rc_eval_state_t eval_state;
rc_condset_t* condset;
int valid = 0;
rc_update_value_memrefs(self, peek, ud);
value->value.i32 = 0;
value->type = RC_VALUE_TYPE_SIGNED;
for (condset = self->conditions; condset != NULL; condset = condset->next) {
memset(&eval_state, 0, sizeof(eval_state));
eval_state.peek = peek;
eval_state.peek_userdata = ud;
rc_test_condset(condset, &eval_state);
if (condset->is_paused)
continue;
if (eval_state.was_reset) {
/* if any ResetIf condition was true, reset the hit counts
* NOTE: ResetIf only affects the current condset when used in values!
*/
rc_reset_condset(condset);
}
if (eval_state.measured_value.type != RC_VALUE_TYPE_NONE) {
if (!valid) {
/* capture the first valid measurement, which may be negative */
memcpy(value, &eval_state.measured_value, sizeof(*value));
valid = 1;
}
else {
/* multiple condsets are currently only used for the MAX_OF operation.
* only keep the condset's value if it's higher than the current highest value.
*/
if (rc_typed_value_compare(&eval_state.measured_value, value, RC_OPERATOR_GT))
memcpy(value, &eval_state.measured_value, sizeof(*value));
}
}
}
return valid;
}
int32_t rc_evaluate_value(rc_value_t* self, rc_peek_t peek, void* ud, void* unused_L) {
rc_typed_value_t result;
int valid = rc_evaluate_value_typed(self, &result, peek, ud);
(void)unused_L;
if (valid) {
/* if not paused, store the value so that it's available when paused. */
rc_typed_value_convert(&result, RC_VALUE_TYPE_UNSIGNED);
rc_update_memref_value(&self->value, result.value.u32);
}
else {
/* when paused, the Measured value will not be captured, use the last captured value. */
result.value.u32 = self->value.value;
result.type = RC_VALUE_TYPE_UNSIGNED;
}
rc_typed_value_convert(&result, RC_VALUE_TYPE_SIGNED);
return result.value.i32;
}
void rc_reset_value(rc_value_t* self) {
rc_condset_t* condset = self->conditions;
while (condset != NULL) {
rc_reset_condset(condset);
condset = condset->next;
}
self->value.value = self->value.prior = 0;
self->value.changed = 0;
}
int rc_value_from_hits(rc_value_t* self)
{
rc_condset_t* condset = self->conditions;
for (; condset != NULL; condset = condset->next) {
rc_condition_t* condition = condset->conditions;
for (; condition != NULL; condition = condition->next) {
if (condition->type == RC_CONDITION_MEASURED)
return (condition->required_hits != 0);
}
}
return 0;
}
rc_value_t* rc_alloc_variable(const char* memaddr, size_t memaddr_len, rc_parse_state_t* parse) {
rc_value_t** value_ptr = parse->variables;
rc_value_t* value;
const char* name;
uint32_t measured_target;
if (!value_ptr)
return NULL;
while (*value_ptr) {
value = *value_ptr;
if (strncmp(value->name, memaddr, memaddr_len) == 0 && value->name[memaddr_len] == 0)
return value;
value_ptr = &value->next;
}
/* capture name before calling parse as parse will update memaddr pointer */
name = rc_alloc_str(parse, memaddr, memaddr_len);
if (!name)
return NULL;
/* no match found, create a new entry */
value = RC_ALLOC_SCRATCH(rc_value_t, parse);
memset(value, 0, sizeof(value->value));
value->value.size = RC_MEMSIZE_VARIABLE;
value->next = NULL;
/* the helper variable likely has a Measured condition. capture the current measured_target so we can restore it
* after generating the variable so the variable's Measured target doesn't conflict with the rest of the trigger. */
measured_target = parse->measured_target;
rc_parse_value_internal(value, &memaddr, parse);
parse->measured_target = measured_target;
/* store name after calling parse as parse will set name to (unnamed) */
value->name = name;
*value_ptr = value;
return value;
}
uint32_t rc_count_values(const rc_value_t* values) {
uint32_t count = 0;
while (values) {
++count;
values = values->next;
}
return count;
}
void rc_update_values(rc_value_t* values, rc_peek_t peek, void* ud) {
rc_typed_value_t result;
rc_value_t* value = values;
for (; value; value = value->next) {
if (rc_evaluate_value_typed(value, &result, peek, ud)) {
/* store the raw bytes and type to be restored by rc_typed_value_from_memref_value */
rc_update_memref_value(&value->value, result.value.u32);
value->value.type = result.type;
}
}
}
void rc_reset_values(rc_value_t* values) {
rc_value_t* value = values;
for (; value; value = value->next)
rc_reset_value(value);
}
void rc_typed_value_from_memref_value(rc_typed_value_t* value, const rc_memref_value_t* memref) {
/* raw value is always u32, type can mark it as something else */
value->value.u32 = memref->value;
value->type = memref->type;
}
void rc_typed_value_convert(rc_typed_value_t* value, char new_type) {
switch (new_type) {
case RC_VALUE_TYPE_UNSIGNED:
switch (value->type) {
case RC_VALUE_TYPE_UNSIGNED:
return;
case RC_VALUE_TYPE_SIGNED:
value->value.u32 = (unsigned)value->value.i32;
break;
case RC_VALUE_TYPE_FLOAT:
value->value.u32 = (unsigned)value->value.f32;
break;
default:
value->value.u32 = 0;
break;
}
break;
case RC_VALUE_TYPE_SIGNED:
switch (value->type) {
case RC_VALUE_TYPE_SIGNED:
return;
case RC_VALUE_TYPE_UNSIGNED:
value->value.i32 = (int)value->value.u32;
break;
case RC_VALUE_TYPE_FLOAT:
value->value.i32 = (int)value->value.f32;
break;
default:
value->value.i32 = 0;
break;
}
break;
case RC_VALUE_TYPE_FLOAT:
switch (value->type) {
case RC_VALUE_TYPE_FLOAT:
return;
case RC_VALUE_TYPE_UNSIGNED:
value->value.f32 = (float)value->value.u32;
break;
case RC_VALUE_TYPE_SIGNED:
value->value.f32 = (float)value->value.i32;
break;
default:
value->value.f32 = 0.0;
break;
}
break;
default:
break;
}
value->type = new_type;
}
static rc_typed_value_t* rc_typed_value_convert_into(rc_typed_value_t* dest, const rc_typed_value_t* source, char new_type) {
memcpy(dest, source, sizeof(rc_typed_value_t));
rc_typed_value_convert(dest, new_type);
return dest;
}
void rc_typed_value_negate(rc_typed_value_t* value) {
switch (value->type)
{
case RC_VALUE_TYPE_UNSIGNED:
rc_typed_value_convert(value, RC_VALUE_TYPE_SIGNED);
/* fallthrough */ /* to RC_VALUE_TYPE_SIGNED */
case RC_VALUE_TYPE_SIGNED:
value->value.i32 = -(value->value.i32);
break;
case RC_VALUE_TYPE_FLOAT:
value->value.f32 = -(value->value.f32);
break;
default:
break;
}
}
void rc_typed_value_add(rc_typed_value_t* value, const rc_typed_value_t* amount) {
rc_typed_value_t converted;
if (amount->type != value->type && value->type != RC_VALUE_TYPE_NONE) {
if (amount->type == RC_VALUE_TYPE_FLOAT)
rc_typed_value_convert(value, RC_VALUE_TYPE_FLOAT);
else
amount = rc_typed_value_convert_into(&converted, amount, value->type);
}
switch (value->type)
{
case RC_VALUE_TYPE_UNSIGNED:
value->value.u32 += amount->value.u32;
break;
case RC_VALUE_TYPE_SIGNED:
value->value.i32 += amount->value.i32;
break;
case RC_VALUE_TYPE_FLOAT:
value->value.f32 += amount->value.f32;
break;
case RC_VALUE_TYPE_NONE:
memcpy(value, amount, sizeof(rc_typed_value_t));
break;
default:
break;
}
}
void rc_typed_value_multiply(rc_typed_value_t* value, const rc_typed_value_t* amount) {
rc_typed_value_t converted;
switch (value->type)
{
case RC_VALUE_TYPE_UNSIGNED:
switch (amount->type)
{
case RC_VALUE_TYPE_UNSIGNED:
/* the c standard for unsigned multiplication is well defined as non-overflowing truncation
* to the type's size. this allows negative multiplication through twos-complements. i.e.
* 1 * -1 (0xFFFFFFFF) = 0xFFFFFFFF = -1
* 3 * -2 (0xFFFFFFFE) = 0x2FFFFFFFA & 0xFFFFFFFF = 0xFFFFFFFA = -6
* 10 * -5 (0xFFFFFFFB) = 0x9FFFFFFCE & 0xFFFFFFFF = 0xFFFFFFCE = -50
*/
value->value.u32 *= amount->value.u32;
break;
case RC_VALUE_TYPE_SIGNED:
value->value.u32 *= (unsigned)amount->value.i32;
break;
case RC_VALUE_TYPE_FLOAT:
rc_typed_value_convert(value, RC_VALUE_TYPE_FLOAT);
value->value.f32 *= amount->value.f32;
break;
default:
value->type = RC_VALUE_TYPE_NONE;
break;
}
break;
case RC_VALUE_TYPE_SIGNED:
switch (amount->type)
{
case RC_VALUE_TYPE_SIGNED:
value->value.i32 *= amount->value.i32;
break;
case RC_VALUE_TYPE_UNSIGNED:
value->value.i32 *= (int)amount->value.u32;
break;
case RC_VALUE_TYPE_FLOAT:
rc_typed_value_convert(value, RC_VALUE_TYPE_FLOAT);
value->value.f32 *= amount->value.f32;
break;
default:
value->type = RC_VALUE_TYPE_NONE;
break;
}
break;
case RC_VALUE_TYPE_FLOAT:
if (amount->type == RC_VALUE_TYPE_NONE) {
value->type = RC_VALUE_TYPE_NONE;
}
else {
amount = rc_typed_value_convert_into(&converted, amount, RC_VALUE_TYPE_FLOAT);
value->value.f32 *= amount->value.f32;
}
break;
default:
value->type = RC_VALUE_TYPE_NONE;
break;
}
}
void rc_typed_value_divide(rc_typed_value_t* value, const rc_typed_value_t* amount) {
rc_typed_value_t converted;
switch (amount->type)
{
case RC_VALUE_TYPE_UNSIGNED:
if (amount->value.u32 == 0) { /* divide by zero */
value->type = RC_VALUE_TYPE_NONE;
return;
}
switch (value->type) {
case RC_VALUE_TYPE_UNSIGNED: /* integer math */
value->value.u32 /= amount->value.u32;
return;
case RC_VALUE_TYPE_SIGNED: /* integer math */
value->value.i32 /= (int)amount->value.u32;
return;
case RC_VALUE_TYPE_FLOAT:
amount = rc_typed_value_convert_into(&converted, amount, RC_VALUE_TYPE_FLOAT);
break;
default:
value->type = RC_VALUE_TYPE_NONE;
return;
}
break;
case RC_VALUE_TYPE_SIGNED:
if (amount->value.i32 == 0) { /* divide by zero */
value->type = RC_VALUE_TYPE_NONE;
return;
}
switch (value->type) {
case RC_VALUE_TYPE_SIGNED: /* integer math */
value->value.i32 /= amount->value.i32;
return;
case RC_VALUE_TYPE_UNSIGNED: /* integer math */
value->value.u32 /= (unsigned)amount->value.i32;
return;
case RC_VALUE_TYPE_FLOAT:
amount = rc_typed_value_convert_into(&converted, amount, RC_VALUE_TYPE_FLOAT);
break;
default:
value->type = RC_VALUE_TYPE_NONE;
return;
}
break;
case RC_VALUE_TYPE_FLOAT:
break;
default:
value->type = RC_VALUE_TYPE_NONE;
return;
}
if (amount->value.f32 == 0.0) { /* divide by zero */
value->type = RC_VALUE_TYPE_NONE;
return;
}
rc_typed_value_convert(value, RC_VALUE_TYPE_FLOAT);
value->value.f32 /= amount->value.f32;
}
void rc_typed_value_modulus(rc_typed_value_t* value, const rc_typed_value_t* amount) {
rc_typed_value_t converted;
switch (amount->type)
{
case RC_VALUE_TYPE_UNSIGNED:
if (amount->value.u32 == 0) { /* divide by zero */
value->type = RC_VALUE_TYPE_NONE;
return;
}
switch (value->type) {
case RC_VALUE_TYPE_UNSIGNED: /* integer math */
value->value.u32 %= amount->value.u32;
return;
case RC_VALUE_TYPE_SIGNED: /* integer math */
value->value.i32 %= (int)amount->value.u32;
return;
case RC_VALUE_TYPE_FLOAT:
amount = rc_typed_value_convert_into(&converted, amount, RC_VALUE_TYPE_FLOAT);
break;
default:
value->type = RC_VALUE_TYPE_NONE;
return;
}
break;
case RC_VALUE_TYPE_SIGNED:
if (amount->value.i32 == 0) { /* divide by zero */
value->type = RC_VALUE_TYPE_NONE;
return;
}
switch (value->type) {
case RC_VALUE_TYPE_SIGNED: /* integer math */
value->value.i32 %= amount->value.i32;
return;
case RC_VALUE_TYPE_UNSIGNED: /* integer math */
value->value.u32 %= (unsigned)amount->value.i32;
return;
case RC_VALUE_TYPE_FLOAT:
amount = rc_typed_value_convert_into(&converted, amount, RC_VALUE_TYPE_FLOAT);
break;
default:
value->type = RC_VALUE_TYPE_NONE;
return;
}
break;
case RC_VALUE_TYPE_FLOAT:
break;
default:
value->type = RC_VALUE_TYPE_NONE;
return;
}
if (amount->value.f32 == 0.0) { /* divide by zero */
value->type = RC_VALUE_TYPE_NONE;
return;
}
rc_typed_value_convert(value, RC_VALUE_TYPE_FLOAT);
value->value.f32 = (float)fmod(value->value.f32, amount->value.f32);
}
void rc_typed_value_combine(rc_typed_value_t* value, rc_typed_value_t* amount, uint8_t oper) {
switch (oper) {
case RC_OPERATOR_MULT:
rc_typed_value_multiply(value, amount);
break;
case RC_OPERATOR_DIV:
rc_typed_value_divide(value, amount);
break;
case RC_OPERATOR_AND:
rc_typed_value_convert(value, RC_VALUE_TYPE_UNSIGNED);
rc_typed_value_convert(amount, RC_VALUE_TYPE_UNSIGNED);
value->value.u32 &= amount->value.u32;
break;
case RC_OPERATOR_XOR:
rc_typed_value_convert(value, RC_VALUE_TYPE_UNSIGNED);
rc_typed_value_convert(amount, RC_VALUE_TYPE_UNSIGNED);
value->value.u32 ^= amount->value.u32;
break;
case RC_OPERATOR_MOD:
rc_typed_value_modulus(value, amount);
break;
case RC_OPERATOR_ADD:
rc_typed_value_add(value, amount);
break;
case RC_OPERATOR_SUB:
rc_typed_value_negate(amount);
rc_typed_value_add(value, amount);
break;
}
}
static int rc_typed_value_compare_floats(float f1, float f2, char oper) {
if (f1 == f2) {
/* exactly equal */
}
else {
/* attempt to match 7 significant digits (24-bit mantissa supports just over 7 significant decimal digits) */
/* https://stackoverflow.com/questions/17333/what-is-the-most-effective-way-for-float-and-double-comparison */
const float abs1 = (f1 < 0) ? -f1 : f1;
const float abs2 = (f2 < 0) ? -f2 : f2;
const float threshold = ((abs1 < abs2) ? abs1 : abs2) * FLT_EPSILON;
const float diff = f1 - f2;
const float abs_diff = (diff < 0) ? -diff : diff;
if (abs_diff <= threshold) {
/* approximately equal */
}
else if (diff > threshold) {
/* greater */
switch (oper) {
case RC_OPERATOR_NE:
case RC_OPERATOR_GT:
case RC_OPERATOR_GE:
return 1;
default:
return 0;
}
}
else {
/* lesser */
switch (oper) {
case RC_OPERATOR_NE:
case RC_OPERATOR_LT:
case RC_OPERATOR_LE:
return 1;
default:
return 0;
}
}
}
/* exactly or approximately equal */
switch (oper) {
case RC_OPERATOR_EQ:
case RC_OPERATOR_GE:
case RC_OPERATOR_LE:
return 1;
default:
return 0;
}
}
int rc_typed_value_compare(const rc_typed_value_t* value1, const rc_typed_value_t* value2, char oper) {
rc_typed_value_t converted_value;
if (value2->type != value1->type) {
/* if either side is a float, convert both sides to float. otherwise, assume the signed-ness of the left side. */
if (value2->type == RC_VALUE_TYPE_FLOAT)
value1 = rc_typed_value_convert_into(&converted_value, value1, value2->type);
else
value2 = rc_typed_value_convert_into(&converted_value, value2, value1->type);
}
switch (value1->type) {
case RC_VALUE_TYPE_UNSIGNED:
switch (oper) {
case RC_OPERATOR_EQ: return value1->value.u32 == value2->value.u32;
case RC_OPERATOR_NE: return value1->value.u32 != value2->value.u32;
case RC_OPERATOR_LT: return value1->value.u32 < value2->value.u32;
case RC_OPERATOR_LE: return value1->value.u32 <= value2->value.u32;
case RC_OPERATOR_GT: return value1->value.u32 > value2->value.u32;
case RC_OPERATOR_GE: return value1->value.u32 >= value2->value.u32;
default: return 1;
}
case RC_VALUE_TYPE_SIGNED:
switch (oper) {
case RC_OPERATOR_EQ: return value1->value.i32 == value2->value.i32;
case RC_OPERATOR_NE: return value1->value.i32 != value2->value.i32;
case RC_OPERATOR_LT: return value1->value.i32 < value2->value.i32;
case RC_OPERATOR_LE: return value1->value.i32 <= value2->value.i32;
case RC_OPERATOR_GT: return value1->value.i32 > value2->value.i32;
case RC_OPERATOR_GE: return value1->value.i32 >= value2->value.i32;
default: return 1;
}
case RC_VALUE_TYPE_FLOAT:
return rc_typed_value_compare_floats(value1->value.f32, value2->value.f32, oper);
default:
return 1;
}
}