-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconformance.cpp
More file actions
1786 lines (1616 loc) · 62.3 KB
/
Copy pathconformance.cpp
File metadata and controls
1786 lines (1616 loc) · 62.3 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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
serialize
Copyright © 2016 - 2026, Más Bandwidth LLC.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Runs the shared conformance corpus through this library's reader, writer and measure.
The corpus is the conformance/ directory: one file per covered operation, holding the
accepted and refused vectors STANDARD.md's rules require. It is the conformance instrument every
implementation in the family runs, and it is deliberately not generated from this code — a
suite that regenerates its own expectations proves only that a port agrees with itself.
Each vector states an operation, its parameters, the stream bytes, and either the value a
conforming reader decodes together with the bits it consumes, or the word `refused`. An
accepted vector must yield exactly that value and consume exactly that many bits; a refused
vector must be refused, and must leave the caller's scalar destination unwritten, which is the
obligation Reader Obligations states for every refusal. STANDARD.md leaves a caller-owned
BUFFER unspecified after a refusal — bytes, string and wstring — so this runner checks the
destination only for the scalar operations, exactly as far as the document reaches.
A vector carrying `writer = canonical` additionally pins the bytes a conforming writer emits
for its value: the runner writes the value back and requires the emission to be the vector's
bytes exactly, flush included, which is where the trailing-bits writer obligation bites. A
vector without the mark binds the reader only.
A sequence vector carrying `measure_at_least` pins the floor a conforming measure may report,
which STANDARD.md makes a bound rather than an equality.
THE BUFFER CONTRACT. STANDARD.md, "Past-end memory is an implementation contract": the C++
implementation loads 64-bit windows at byte granularity and therefore requires its caller to
allocate at least 8 bytes beyond the data. Every stream this runner presents carries that
slack, and the slack bytes are set to a non-zero pattern rather than zero, so a vector that
only passes because uninterpreted bytes past the end read as zero fails here.
A corpus file whose operation this runner cannot drive is a gap in the runner, not a pass:
such a vector FAILS. So does a vector naming a parameter the runner does not understand, and
a fixed point vector naming a Q format declaration the runner does not carry — the fixed
point declaration is a compile time constant of its call site, so a runner supports a fixed
set of them and must say so out loud rather than skip.
A refused vector must be refused, must leave the caller's scalar destination unwritten, and
must leave the stream TERMINAL. Terminality is checked by behavior rather than by an
accessor, so the check ports to every implementation in the family: a further read on the
same stream must also fail, consume no bits and write nothing.
The vector files are named on the command line, and CMake discovers them: the glob is in
CMakeLists.txt, with CONFIGURE_DEPENDS so a vendored file that no one named still runs.
STANDARD.md, "The vector format", specifies the syntax.
*/
#include "serialize.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// ---------------------------------------------------------------------------------------
// vector file parsing
const int MaxLine = 1024;
const int MaxBytes = 256;
const int MaxSlack = 8; // the buffer contract: at least 8 bytes past the data
const uint8_t SlackFill = 0xA5; // non-zero, so a read that strays past the end is visible
const int MaxParams = 8;
const int MaxSteps = 48; // the golden message is 28 operations long
enum ExpectKind
{
EXPECT_REFUSED,
EXPECT_VALUE,
EXPECT_BITS // compared as a bit pattern, never as a value
};
struct Param
{
char name[MaxLine];
char value[MaxLine];
};
struct Vector
{
char file[MaxLine];
char operation[MaxLine];
char name[MaxLine];
char expect[MaxLine];
Param params[MaxParams];
int numParams;
char steps[MaxSteps][MaxLine];
int numSteps;
uint8_t bytes[MaxBytes + MaxSlack];
int numBytes;
int64_t consumed;
bool hasConsumed;
int64_t measureAtLeast;
bool hasMeasure;
bool writerCanonical;
ExpectKind expectKind;
};
static void vector_reset( Vector & vector, const char * file )
{
memset( &vector, 0, sizeof( Vector ) );
strncpy( vector.file, file, MaxLine - 1 );
vector.expectKind = EXPECT_VALUE;
}
static bool vector_empty( const Vector & vector )
{
return vector.operation[0] == '\0';
}
// strips surrounding whitespace, in place
static char * trim( char * text )
{
while ( *text == ' ' || *text == '\t' )
{
text++;
}
char * end = text + strlen( text );
while ( end > text && ( end[-1] == ' ' || end[-1] == '\t' || end[-1] == '\r' || end[-1] == '\n' ) )
{
end--;
}
*end = '\0';
return text;
}
static bool parse_hex_digit( char c, int & out )
{
if ( c >= '0' && c <= '9' ) { out = c - '0'; return true; }
if ( c >= 'a' && c <= 'f' ) { out = 10 + c - 'a'; return true; }
if ( c >= 'A' && c <= 'F' ) { out = 10 + c - 'A'; return true; }
return false;
}
// hexadecimal byte pairs, whitespace separated, into a byte array
static bool parse_hex_bytes( const char * text, uint8_t * out, int maxBytes, int & numBytes )
{
numBytes = 0;
while ( *text )
{
if ( *text == ' ' || *text == '\t' )
{
text++;
continue;
}
int high = 0;
int low = 0;
if ( !parse_hex_digit( text[0], high ) || !parse_hex_digit( text[1], low ) )
{
return false;
}
if ( numBytes >= maxBytes )
{
return false;
}
out[numBytes++] = (uint8_t) ( high * 16 + low );
text += 2;
}
return true;
}
/*
Numbers in a vector are signed decimal or 0x hexadecimal, parsed to 128 bits, because a
vector's value can be wider than any built in strtol and the corpus states wide bounds as
hexadecimal where the decimal would be unreadable.
*/
static bool parse_number( const char * text, serialize::int128_t & out )
{
bool negative = false;
if ( *text == '-' ) { negative = true; text++; }
else if ( *text == '+' ) { text++; }
if ( *text == '\0' )
{
return false;
}
// The accumulation runs in the UNSIGNED domain and the sign is applied there too. The corpus
// states 128 bit bounds at both extremes -- the full signed range's minimum, and the unsigned
// maximum as a decimal -- and accumulating either of those in a signed type overflows, which
// is undefined behaviour rather than the wrap the value needs. Unsigned arithmetic wraps by
// definition, so the digits land where they should and the two's complement reading happens
// once, at the end.
serialize::uint128_t value = 0;
if ( text[0] == '0' && ( text[1] == 'x' || text[1] == 'X' ) )
{
text += 2;
if ( *text == '\0' )
{
return false;
}
for ( ; *text; text++ )
{
int digit = 0;
if ( !parse_hex_digit( *text, digit ) )
{
return false;
}
value = value * serialize::uint128_t( 16 ) + serialize::uint128_t( digit );
}
}
else
{
for ( ; *text; text++ )
{
if ( *text < '0' || *text > '9' )
{
return false;
}
value = value * serialize::uint128_t( 10 ) + serialize::uint128_t( *text - '0' );
}
}
if ( negative )
{
value = serialize::uint128_t( 0 ) - value;
}
out = serialize::int128_t( value );
return true;
}
// ---------------------------------------------------------------------------------------
// failure reporting
static int failures = 0;
static int checked = 0;
static int writerChecked = 0;
static int measureChecked = 0;
static void fail( const Vector & vector, const char * detail )
{
printf( " FAIL %s: %s [%s]\n", vector.name, detail, vector.file );
failures++;
}
// Failure is terminal (STANDARD.md, Reader Obligations), and a refused vector is where that
// rule is testable: the stream is checked by behavior rather than by an accessor, so the same
// check ports to every implementation in the family. A further read must fail, consume no bits
// and leave its destination alone.
static void fail_unless_stream_is_terminal( const Vector & vector, serialize::ReadStream & stream )
{
uint32_t after = 0xFFFFFFFF;
const int64_t bitsBefore = stream.GetBitsProcessed();
if ( stream.SerializeBits( after, 8 ) )
{
fail( vector, "the stream accepted a read after the refusal: failure is not terminal" );
return;
}
if ( after != 0xFFFFFFFF )
{
fail( vector, "the read after the refusal wrote to its destination" );
return;
}
if ( stream.GetBitsProcessed() != bitsBefore )
{
fail( vector, "the read after the refusal consumed bits" );
}
}
// ---------------------------------------------------------------------------------------
// parameter access. A parameter the runner does not understand is a failure rather than a
// silent default: a vector whose declaration is not the one being exercised proves nothing.
static const char * param_string( const Vector & vector, const char * name )
{
for ( int i = 0; i < vector.numParams; i++ )
{
if ( strcmp( vector.params[i].name, name ) == 0 )
{
return vector.params[i].value;
}
}
return NULL;
}
static bool param_number( const Vector & vector, const char * name, serialize::int128_t & out )
{
const char * text = param_string( vector, name );
if ( !text )
{
return false;
}
return parse_number( text, out );
}
static bool param_int( const Vector & vector, const char * name, int64_t & out )
{
serialize::int128_t value = 0;
if ( !param_number( vector, name, value ) )
{
return false;
}
out = (int64_t) value;
return true;
}
static bool param_float( const Vector & vector, const char * name, float & out )
{
const char * text = param_string( vector, name );
if ( !text )
{
return false;
}
char * end = NULL;
out = (float) strtod( text, &end );
return end != text && *end == '\0';
}
// ---------------------------------------------------------------------------------------
// stream construction. Every stream the runner hands the reader carries the slack the buffer
// contract requires, filled with a non-zero pattern so that a decode which depends on memory
// past the end cannot pass by reading zeros.
struct StreamBuffer
{
uint8_t data[MaxBytes + MaxSlack];
int64_t bytes;
};
static void stream_buffer_init( StreamBuffer & buffer, const Vector & vector )
{
memset( buffer.data, SlackFill, sizeof( buffer.data ) );
memcpy( buffer.data, vector.bytes, (size_t) vector.numBytes );
buffer.bytes = vector.numBytes;
}
// ---------------------------------------------------------------------------------------
// the operations under test, called through the public macros so the vectors exercise the
// surface a consumer uses rather than the stream methods underneath it
template <typename Stream> bool op_bits( Stream & stream, uint64_t & value, int bits )
{
serialize_bits( stream, value, bits );
return true;
}
template <typename Stream> bool op_bool( Stream & stream, bool & value )
{
serialize_bool( stream, value );
return true;
}
template <typename Stream> bool op_uint128( Stream & stream, serialize::uint128_t & value )
{
serialize_uint128( stream, value );
return true;
}
template <typename Stream> bool op_align( Stream & stream )
{
serialize_align( stream );
return true;
}
template <typename Stream> bool op_int( Stream & stream, int32_t & value, int32_t min, int32_t max )
{
serialize_int( stream, value, min, max );
return true;
}
template <typename Stream> bool op_int64( Stream & stream, int64_t & value, int64_t min, int64_t max )
{
serialize_int64( stream, value, min, max );
return true;
}
template <typename Stream> bool op_int128( Stream & stream, serialize::int128_t & value, serialize::int128_t min, serialize::int128_t max )
{
serialize_int128( stream, value, min, max );
return true;
}
template <typename Stream> bool op_int_relative( Stream & stream, int32_t previous, int32_t & current )
{
serialize_int_relative( stream, previous, current );
return true;
}
template <typename Stream> bool op_float( Stream & stream, float & value )
{
serialize_float( stream, value );
return true;
}
template <typename Stream> bool op_double( Stream & stream, double & value )
{
serialize_double( stream, value );
return true;
}
template <typename Stream> bool op_compressed_float( Stream & stream, float & value, float min, float max, float res )
{
serialize_compressed_float( stream, value, min, max, res );
return true;
}
template <typename Stream> bool op_bytes( Stream & stream, uint8_t * data, int count )
{
serialize_bytes( stream, data, count );
return true;
}
template <typename Stream> bool op_string( Stream & stream, char * string, int bufferSize )
{
serialize_string( stream, string, bufferSize );
return true;
}
template <typename Stream> bool op_wstring( Stream & stream, wchar_t * string, int bufferSize )
{
serialize_wstring( stream, string, bufferSize );
return true;
}
// ---------------------------------------------------------------------------------------
// fixed point. Every parameter is a compile time constant of the call site, so the runner
// carries a table of declarations and a vector naming one that is not in it FAILS rather than
// passes. Adding a declaration to the corpus means adding a row here.
template <int I, int F, int64_t Lo, int64_t Hi, typename Storage, typename Stream>
bool op_fixed( Stream & stream, Storage & value )
{
serialize_fixed( stream, value, I, F, Lo, Hi );
return true;
}
enum FixedDeclaration
{
FIXED_NONE,
FIXED_Q8_8_M100_100,
FIXED_Q32_0_0_7,
FIXED_Q32_0_0_5,
FIXED_Q16_16_7_7,
FIXED_Q16_16_M32768_32767,
FIXED_Q16_16_M2000_2000,
FIXED_Q16_16_0_30000,
FIXED_Q48_16_0_131072,
FIXED_Q48_16_M100000_100000,
FIXED_Q112_16_M9_M9,
FIXED_Q112_16_0_2P58,
FIXED_Q112_16_M2P57_2P57,
FIXED_Q64_64_0_0,
FIXED_Q64_64_3_3,
FIXED_Q64_64_0_INT64MAX,
FIXED_Q64_64_FULL_INT64
};
static FixedDeclaration fixed_declaration( int64_t integerBits, int64_t fractionBits, serialize::int128_t min, serialize::int128_t max )
{
const serialize::int128_t two58 = serialize::int128_t( 288230376151711744LL );
const serialize::int128_t two57 = serialize::int128_t( 144115188075855872LL );
if ( integerBits == 8 && fractionBits == 8 && min == -100 && max == 100 ) return FIXED_Q8_8_M100_100;
if ( integerBits == 32 && fractionBits == 0 && min == 0 && max == 7 ) return FIXED_Q32_0_0_7;
if ( integerBits == 32 && fractionBits == 0 && min == 0 && max == 5 ) return FIXED_Q32_0_0_5;
if ( integerBits == 16 && fractionBits == 16 && min == 7 && max == 7 ) return FIXED_Q16_16_7_7;
if ( integerBits == 16 && fractionBits == 16 && min == -32768 && max == 32767 ) return FIXED_Q16_16_M32768_32767;
if ( integerBits == 16 && fractionBits == 16 && min == -2000 && max == 2000 ) return FIXED_Q16_16_M2000_2000;
if ( integerBits == 16 && fractionBits == 16 && min == 0 && max == 30000 ) return FIXED_Q16_16_0_30000;
if ( integerBits == 48 && fractionBits == 16 && min == 0 && max == 131072 ) return FIXED_Q48_16_0_131072;
if ( integerBits == 48 && fractionBits == 16 && min == -100000 && max == 100000 ) return FIXED_Q48_16_M100000_100000;
if ( integerBits == 112 && fractionBits == 16 && min == -9 && max == -9 ) return FIXED_Q112_16_M9_M9;
if ( integerBits == 112 && fractionBits == 16 && min == 0 && max == two58 ) return FIXED_Q112_16_0_2P58;
if ( integerBits == 112 && fractionBits == 16 && min == -two57 && max == two57 ) return FIXED_Q112_16_M2P57_2P57;
if ( integerBits == 64 && fractionBits == 64 && min == 0 && max == 0 ) return FIXED_Q64_64_0_0;
if ( integerBits == 64 && fractionBits == 64 && min == 3 && max == 3 ) return FIXED_Q64_64_3_3;
if ( integerBits == 64 && fractionBits == 64 && min == 0 && max == serialize::int128_t( 9223372036854775807LL ) ) return FIXED_Q64_64_0_INT64MAX;
if ( integerBits == 64 && fractionBits == 64 && min == serialize::int128_t( INT64_MIN ) && max == serialize::int128_t( 9223372036854775807LL ) ) return FIXED_Q64_64_FULL_INT64;
return FIXED_NONE;
}
// The raw value travels through the runner as a 128 bit integer whatever the declaration's
// storage width, so one code path drives every row of the table above.
template <typename Stream>
static bool run_fixed_declaration( Stream & stream, FixedDeclaration declaration, serialize::int128_t & raw )
{
switch ( declaration )
{
case FIXED_Q8_8_M100_100:
{
int16_t value = (int16_t) (int64_t) raw;
if ( !op_fixed<8, 8, -100, 100, int16_t>( stream, value ) ) return false;
raw = serialize::int128_t( (int64_t) value );
return true;
}
case FIXED_Q32_0_0_7:
{
int32_t value = (int32_t) (int64_t) raw;
if ( !op_fixed<32, 0, 0, 7, int32_t>( stream, value ) ) return false;
raw = serialize::int128_t( (int64_t) value );
return true;
}
case FIXED_Q32_0_0_5:
{
int32_t value = (int32_t) (int64_t) raw;
if ( !op_fixed<32, 0, 0, 5, int32_t>( stream, value ) ) return false;
raw = serialize::int128_t( (int64_t) value );
return true;
}
case FIXED_Q16_16_7_7:
{
int32_t value = (int32_t) (int64_t) raw;
if ( !op_fixed<16, 16, 7, 7, int32_t>( stream, value ) ) return false;
raw = serialize::int128_t( (int64_t) value );
return true;
}
case FIXED_Q16_16_M32768_32767:
{
int32_t value = (int32_t) (int64_t) raw;
if ( !op_fixed<16, 16, -32768, 32767, int32_t>( stream, value ) ) return false;
raw = serialize::int128_t( (int64_t) value );
return true;
}
case FIXED_Q16_16_M2000_2000:
{
int32_t value = (int32_t) (int64_t) raw;
if ( !op_fixed<16, 16, -2000, 2000, int32_t>( stream, value ) ) return false;
raw = serialize::int128_t( (int64_t) value );
return true;
}
case FIXED_Q16_16_0_30000:
{
int32_t value = (int32_t) (int64_t) raw;
if ( !op_fixed<16, 16, 0, 30000, int32_t>( stream, value ) ) return false;
raw = serialize::int128_t( (int64_t) value );
return true;
}
case FIXED_Q48_16_0_131072:
{
int64_t value = (int64_t) raw;
if ( !op_fixed<48, 16, 0, 131072, int64_t>( stream, value ) ) return false;
raw = serialize::int128_t( value );
return true;
}
case FIXED_Q48_16_M100000_100000:
{
int64_t value = (int64_t) raw;
if ( !op_fixed<48, 16, -100000, 100000, int64_t>( stream, value ) ) return false;
raw = serialize::int128_t( value );
return true;
}
case FIXED_Q112_16_M9_M9:
{
serialize::int128_t value = raw;
if ( !op_fixed<112, 16, -9, -9, serialize::int128_t>( stream, value ) ) return false;
raw = value;
return true;
}
case FIXED_Q112_16_0_2P58:
{
serialize::int128_t value = raw;
if ( !op_fixed<112, 16, 0, 288230376151711744LL, serialize::int128_t>( stream, value ) ) return false;
raw = value;
return true;
}
case FIXED_Q112_16_M2P57_2P57:
{
serialize::int128_t value = raw;
if ( !op_fixed<112, 16, -144115188075855872LL, 144115188075855872LL, serialize::int128_t>( stream, value ) ) return false;
raw = value;
return true;
}
case FIXED_Q64_64_0_0:
{
serialize::int128_t value = raw;
if ( !op_fixed<64, 64, 0, 0, serialize::int128_t>( stream, value ) ) return false;
raw = value;
return true;
}
case FIXED_Q64_64_3_3:
{
serialize::int128_t value = raw;
if ( !op_fixed<64, 64, 3, 3, serialize::int128_t>( stream, value ) ) return false;
raw = value;
return true;
}
case FIXED_Q64_64_0_INT64MAX:
{
serialize::int128_t value = raw;
if ( !op_fixed<64, 64, 0, 9223372036854775807LL, serialize::int128_t>( stream, value ) ) return false;
raw = value;
return true;
}
case FIXED_Q64_64_FULL_INT64:
{
serialize::int128_t value = raw;
if ( !op_fixed<64, 64, INT64_MIN, 9223372036854775807LL, serialize::int128_t>( stream, value ) ) return false;
raw = value;
return true;
}
default:
return false;
}
}
// ---------------------------------------------------------------------------------------
// the step machine, which drives both the single operation files and the sequence files. A
// single operation vector is a one step sequence whose step is built from the record's own
// parameters, so there is exactly one execution path and the sequence files cannot drift away
// from the operation files.
enum StepKind
{
STEP_BITS,
STEP_BOOL,
STEP_UINT128,
STEP_ALIGN,
STEP_INT,
STEP_INT64,
STEP_INT128,
STEP_INT_RELATIVE,
STEP_FLOAT,
STEP_DOUBLE,
STEP_COMPRESSED_FLOAT,
STEP_BYTES,
STEP_STRING,
STEP_WSTRING,
STEP_FIXED,
STEP_OBJECT // opens a nested object over the steps that follow
};
struct Step
{
StepKind kind;
int64_t width; // bits, count, buffer_size or preceding_bits
serialize::int128_t min;
serialize::int128_t max;
float fmin;
float fmax;
float fres;
FixedDeclaration fixedDeclaration;
int32_t previous;
// outputs
serialize::uint128_t bits; // the decoded value, as a bit pattern where that is what is pinned
serialize::int128_t number;
bool boolean;
uint8_t buffer[MaxBytes + 1];
int bufferBytes;
wchar_t wbuffer[MaxBytes + 1];
int wbufferUnits;
};
/*
Runs one step against any stream. The destination sentinel rule lives at the call site: for
the scalar operations the caller seeds the destination and checks it afterwards, and for the
caller-owned buffers it does not, because STANDARD.md leaves those unspecified after a refusal.
*/
template <typename Stream> static bool run_step( Stream & stream, Step & step )
{
switch ( step.kind )
{
case STEP_BITS:
{
uint64_t value = uint64_t( step.bits );
const bool ok = op_bits( stream, value, (int) step.width );
step.bits = serialize::uint128_t( value );
return ok;
}
case STEP_BOOL:
return op_bool( stream, step.boolean );
case STEP_UINT128:
{
serialize::uint128_t value = step.bits;
const bool ok = op_uint128( stream, value );
step.bits = value;
return ok;
}
case STEP_ALIGN:
return op_align( stream );
case STEP_INT:
{
int32_t value = (int32_t) (int64_t) step.number;
const bool ok = op_int( stream, value, (int32_t) (int64_t) step.min, (int32_t) (int64_t) step.max );
step.number = serialize::int128_t( (int64_t) value );
return ok;
}
case STEP_INT64:
{
int64_t value = (int64_t) step.number;
const bool ok = op_int64( stream, value, (int64_t) step.min, (int64_t) step.max );
step.number = serialize::int128_t( value );
return ok;
}
case STEP_INT128:
{
serialize::int128_t value = step.number;
const bool ok = op_int128( stream, value, step.min, step.max );
step.number = value;
return ok;
}
case STEP_INT_RELATIVE:
{
int32_t value = (int32_t) (int64_t) step.number;
const bool ok = op_int_relative( stream, step.previous, value );
step.number = serialize::int128_t( (int64_t) value );
return ok;
}
case STEP_FLOAT:
{
uint32_t pattern = (uint32_t) uint64_t( step.bits );
float value;
memcpy( &value, &pattern, 4 );
const bool ok = op_float( stream, value );
memcpy( &pattern, &value, 4 );
step.bits = serialize::uint128_t( uint64_t( pattern ) );
return ok;
}
case STEP_DOUBLE:
{
uint64_t pattern = uint64_t( step.bits );
double value;
memcpy( &value, &pattern, 8 );
const bool ok = op_double( stream, value );
memcpy( &pattern, &value, 8 );
step.bits = serialize::uint128_t( pattern );
return ok;
}
case STEP_COMPRESSED_FLOAT:
{
uint32_t pattern = (uint32_t) uint64_t( step.bits );
float value;
memcpy( &value, &pattern, 4 );
const bool ok = op_compressed_float( stream, value, step.fmin, step.fmax, step.fres );
memcpy( &pattern, &value, 4 );
step.bits = serialize::uint128_t( uint64_t( pattern ) );
return ok;
}
case STEP_BYTES:
return op_bytes( stream, step.buffer, (int) step.width );
case STEP_STRING:
return op_string( stream, (char*) step.buffer, (int) step.width );
case STEP_WSTRING:
return op_wstring( stream, step.wbuffer, (int) step.width );
case STEP_FIXED:
return run_fixed_declaration( stream, step.fixedDeclaration, step.number );
case STEP_OBJECT:
// nesting is driven by run_steps, which owns the step range an object wraps; a bare
// object step reaching here is a runner bug rather than a vector one
return false;
}
return false;
}
// the field of a step that holds the value, which is the destination
// "a refused primitive read must leave its destination unwritten" reaches
static bool step_value_is_a_bit_pattern( StepKind kind )
{
return kind == STEP_BITS || kind == STEP_UINT128 || kind == STEP_FLOAT || kind == STEP_DOUBLE || kind == STEP_COMPRESSED_FLOAT;
}
static bool step_value_is_a_number( StepKind kind )
{
return kind == STEP_INT || kind == STEP_INT64 || kind == STEP_INT128 || kind == STEP_INT_RELATIVE || kind == STEP_FIXED;
}
/*
STANDARD.md, "object": serialize_object invokes the object's own serialize function inline
and contributes NO BYTES OF ITS OWN — it is composition, not an encoding, with no framing,
length prefix or alignment inserted around it. A step spelled `object <n>` wraps the next n
steps in a nested object, so a vector can state the same operations twice, once nested and
once flat, and require identical bytes.
The nested object is driven through the public serialize_object macro rather than by calling
the steps directly, so what the vectors exercise is the composition the macro performs.
*/
template <typename Stream> static bool run_steps( Stream & stream, Step * steps, int count, int * stoppedAt = NULL );
static Step * g_failedStep = NULL; // the step a run stopped on, for the destination check
struct NestedObject
{
Step * steps;
int count;
template <typename Stream> bool Serialize( Stream & stream )
{
return run_steps( stream, steps, count );
}
};
template <typename Stream> static bool run_nested_object( Stream & stream, NestedObject & object )
{
serialize_object( stream, object );
return true;
}
// advances past the steps a nested object owns, so a top level walk sees one step per object
static int step_span( const Step * steps, int index )
{
if ( steps[index].kind == STEP_OBJECT )
{
return 1 + (int) steps[index].width;
}
return 1;
}
template <typename Stream> static bool run_steps( Stream & stream, Step * steps, int count, int * stoppedAt )
{
for ( int i = 0; i < count; i += step_span( steps, i ) )
{
if ( steps[i].kind == STEP_OBJECT )
{
NestedObject object;
object.steps = steps + i + 1;
object.count = (int) steps[i].width;
if ( !run_nested_object( stream, object ) )
{
if ( stoppedAt ) *stoppedAt = i;
return false;
}
continue;
}
if ( !run_step( stream, steps[i] ) )
{
g_failedStep = &steps[i];
if ( stoppedAt ) *stoppedAt = i;
return false;
}
}
return true;
}
// ---------------------------------------------------------------------------------------
// building steps
static bool step_from_words( const Vector & vector, const char * text, Step & step )
{
memset( &step, 0, sizeof( Step ) );
step.fixedDeclaration = FIXED_NONE;
char work[MaxLine];
strncpy( work, text, MaxLine - 1 );
work[MaxLine - 1] = '\0';
const char * words[8];
int numWords = 0;
char * cursor = work;
while ( *cursor && numWords < 8 )
{
while ( *cursor == ' ' || *cursor == '\t' ) cursor++;
if ( *cursor == '\0' ) break;
words[numWords++] = cursor;
while ( *cursor && *cursor != ' ' && *cursor != '\t' ) cursor++;
if ( *cursor ) *cursor++ = '\0';
}
if ( numWords == 0 )
{
return false;
}
serialize::int128_t a = 0;
serialize::int128_t b = 0;
if ( strcmp( words[0], "bits" ) == 0 && numWords == 2 && parse_number( words[1], a ) )
{
step.kind = STEP_BITS;
step.width = (int64_t) a;
return true;
}
if ( strcmp( words[0], "bool" ) == 0 && numWords == 1 )
{
step.kind = STEP_BOOL;
return true;
}
if ( strcmp( words[0], "object" ) == 0 && numWords == 2 && parse_number( words[1], a ) )
{
step.kind = STEP_OBJECT;
step.width = (int64_t) a;
return true;
}
if ( strcmp( words[0], "align" ) == 0 && numWords == 1 )
{
step.kind = STEP_ALIGN;
return true;
}
if ( strcmp( words[0], "float" ) == 0 && numWords == 1 )
{
step.kind = STEP_FLOAT;
return true;
}
if ( strcmp( words[0], "double" ) == 0 && numWords == 1 )
{
step.kind = STEP_DOUBLE;
return true;
}
if ( strcmp( words[0], "uint128" ) == 0 && numWords == 1 )
{
step.kind = STEP_UINT128;
return true;
}
if ( strcmp( words[0], "int_relative" ) == 0 && numWords == 2 && parse_number( words[1], a ) )
{
step.kind = STEP_INT_RELATIVE;
step.previous = (int32_t) (int64_t) a;
return true;
}
if ( strcmp( words[0], "compressed_float" ) == 0 && numWords == 4 )
{
char * end = NULL;
step.fmin = (float) strtod( words[1], &end );
if ( end == words[1] || *end != '\0' ) return false;
step.fmax = (float) strtod( words[2], &end );
if ( end == words[2] || *end != '\0' ) return false;
step.fres = (float) strtod( words[3], &end );
if ( end == words[3] || *end != '\0' ) return false;
step.kind = STEP_COMPRESSED_FLOAT;
return true;
}
if ( strcmp( words[0], "bytes" ) == 0 && numWords == 2 && parse_number( words[1], a ) )
{
step.kind = STEP_BYTES;
step.width = (int64_t) a;
return true;
}
if ( strcmp( words[0], "string" ) == 0 && numWords == 2 && parse_number( words[1], a ) )
{
step.kind = STEP_STRING;
step.width = (int64_t) a;
return true;
}
if ( strcmp( words[0], "wstring" ) == 0 && numWords == 2 && parse_number( words[1], a ) )
{
step.kind = STEP_WSTRING;
step.width = (int64_t) a;
return true;
}
if ( ( strcmp( words[0], "int" ) == 0 || strcmp( words[0], "int64" ) == 0 || strcmp( words[0], "int128" ) == 0 )
&& numWords == 3 && parse_number( words[1], a ) && parse_number( words[2], b ) )
{
step.kind = strcmp( words[0], "int" ) == 0 ? STEP_INT : ( strcmp( words[0], "int64" ) == 0 ? STEP_INT64 : STEP_INT128 );
step.min = a;
step.max = b;
return true;
}
if ( strcmp( words[0], "fixed" ) == 0 && numWords == 5 )
{
serialize::int128_t ib = 0, fb = 0, lo = 0, hi = 0;
if ( !parse_number( words[1], ib ) || !parse_number( words[2], fb ) || !parse_number( words[3], lo ) || !parse_number( words[4], hi ) )
{
return false;
}
step.kind = STEP_FIXED;
step.fixedDeclaration = fixed_declaration( (int64_t) ib, (int64_t) fb, lo, hi );
if ( step.fixedDeclaration == FIXED_NONE )
{
printf( " FAIL %s: no runner for this fixed point declaration [%s]\n", vector.name, vector.file );
failures++;
return false;
}
return true;
}
return false;
}
/*
Builds the step list for a vector. A single operation vector becomes a one or two step
sequence: the operations whose interesting behaviour only exists at a non-zero bit index
take a `preceding_bits` parameter, which becomes a leading bits step.
*/
static bool build_steps( const Vector & vector, Step * steps, int & numSteps )
{
numSteps = 0;
if ( strcmp( vector.operation, "sequence" ) == 0 )
{