forked from billvaglienti/ProtoGen
-
Notifications
You must be signed in to change notification settings - Fork 5
/
protocolfield.cpp
5137 lines (4255 loc) · 169 KB
/
protocolfield.cpp
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
#include "protocolfield.h"
#include "protocolparser.h"
#include "shuntingyard.h"
#include "enumcreator.h"
#include "protocolstructuremodule.h"
#include "protocolbitfield.h"
#include "prebuiltSources/floatspecial.h"
#include <cmath>
#include <iomanip>
#include <sstream>
#include <regex>
#include <limits>
TypeData::TypeData(ProtocolSupport sup) :
isBool(false),
isStruct(false),
isSigned(false),
isBitfield(false),
isFloat(false),
isEnum(false),
isString(false),
isFixedString(false),
isNull(false),
bits(8),
sigbits(0),
enummax(0),
support(sup)
{
}
TypeData::TypeData(const TypeData& that) :
isBool(that.isBool),
isStruct(that.isStruct),
isSigned(that.isSigned),
isBitfield(that.isBitfield),
isFloat(that.isFloat),
isEnum(that.isEnum),
isString(that.isString),
isFixedString(that.isFixedString),
isNull(that.isNull),
bits(that.bits),
sigbits(that.sigbits),
enummax(that.enummax),
enumName(that.enumName),
support(that.support)
{
}
/*!
* Reset all members to defaults except the protocol support
*/
void TypeData::clear(void)
{
isBool = false;
isStruct = false;
isSigned = false;
isBitfield = false;
isFloat = false;
isEnum = false;
isString = false;
isFixedString = false;
isNull = false;
bits = 8;
sigbits = 0;
enummax = 0;
enumName.clear();
}
/*!
* Determine the typename of this field (for example uint8_t).
* \param structName is the structure name, if this is a structure.
* \return the type name that should appear in code.
*/
std::string TypeData::toTypeString(const std::string& structName) const
{
std::string typeName;
if(isString)
typeName = "char";
else if(isBitfield)
{
if((bits > 32) && (support.longbitfield))
typeName = "uint64_t";
else
typeName = "unsigned";
}
else if(isEnum)
{
typeName = enumName;
}
else if(isStruct)
{
typeName = trimm(structName);
// Make sure it ends with the suffix;
if(!endsWith(typeName, support.typeSuffix))
{
typeName += support.typeSuffix;
}
}
else
{
// create the type name
if(isBool)
typeName = "bool";
else if(isFloat)
{
if(bits > 32)
typeName = "double";
else
typeName = "float";
}// if floating point type
else
{
if(isSigned)
typeName = "int";
else
typeName = "uint";
// Add the bits, we use only valid native type widths
if(bits > 32)
typeName += "64_t";
else if(bits > 16)
typeName += "32_t";
else if(bits > 8)
typeName += "16_t";
else
typeName += "8_t";
}// else if integer type
}
return typeName;
}// toTypeString
/*!
* Determine the signature of this field (for example uint8).
* \return the signature name of this field.
*/
std::string TypeData::toSigString(void) const
{
if(isString || isBitfield || isStruct || isBool)
return "unknown";
else if(isFloat)
{
return "float" + std::to_string(bits);
}// if floating point type
else
{
if(isSigned)
return "int" + std::to_string(bits);
else
return "uint" + std::to_string(bits);
}// else if integer type
}// toSigString
/*!
* Determine the maximum floating point value this TypeData can hold
* \return the maximum floating point value of this TypeData
*/
double TypeData::getMaximumFloatValue(void) const
{
if(isString || isStruct)
return 0;
else if(isEnum)
{
return enummax;
}
else if(isFloat)
{
// Float encodings use float rules
if(bits <= 16)
{
return float16ToFloat32(float32ToFloat16( std::numeric_limits<float>::max(), sigbits), sigbits);
}
else if(bits <= 24)
{
return float24ToFloat32(float32ToFloat24( std::numeric_limits<float>::max(), sigbits), sigbits);
}
else if(bits <= 32)
{
return std::numeric_limits<float>::max();
}
else
return std::numeric_limits<double>::max();
}
else
{
return (double)getMaximumIntegerValue();
}
}// TypeData::getMaximumFloatValue
/*!
* Determine the minimum floating point value this TypeData can hold
* \return the minimum floating point value of this TypeData
*/
double TypeData::getMinimumFloatValue(void) const
{
if(isString || isStruct)
return 0;
else if(isFloat)
{
// Float encodings use float rules
if(bits <= 16)
{
// Note that min() is the smallest positive number
return float16ToFloat32(float32ToFloat16(-1*std::numeric_limits<float>::max(), sigbits), sigbits);
}
else if(bits <= 24)
{
return float24ToFloat32(float32ToFloat24(-1*std::numeric_limits<float>::max(), sigbits), sigbits);
}
else if(bits <= 32)
{
return -1*std::numeric_limits<float>::max();
}
else
return -1*std::numeric_limits<double>::max();
}
else
{
return (double)getMinimumIntegerValue();
}
}// TypeData::getMinimumFloatValue
/*!
* Determine the maximum integer value this TypeData can hold
* \return the maximum integer value of this TypeData
*/
uint64_t TypeData::getMaximumIntegerValue(void) const
{
if(isString || isStruct)
return 0;
else if(isEnum)
{
return enummax;
}
else if(isFloat)
{
return (uint64_t)getMaximumFloatValue();
}
else if(isSigned)
{
uint64_t max = (0x1ull << (bits-1)) - 1;
return max;
}
else
{
uint64_t max = (0x1ull << bits) - 1;
return max;
}
}// TypeData::getMaximumIntegerValue
/*!
* Determine the minimum integer value this TypeData can hold
* \return the minimum integer value of this TypeData
*/
int64_t TypeData::getMinimumIntegerValue(void) const
{
if(isString || isStruct)
return 0;
else if(isFloat)
{
return (int64_t)getMinimumFloatValue();
}
else if(isSigned)
{
// We have to deal with the sign extension here. For example -128 for an
// 8-bit number is 0x80, but -128 for a 16-bit number is 0xFF80. Since we
// are returning a 64-bit number it is almost always bigger than the
// number of bits and sign extensions must be applied
int64_t min = (0xFFFFFFFFFFFFFFFFll << (bits-1));
return min;
}
else
{
return 0;
}
}// TypeData::getMinimumIntegerValue
/*!
* Given a constant string (like default value) apply the type correct suffix
* for the type, or apply a cast if a suffix cannot be used.
* \param number is the input number whose type must be set.
* \return The correctly typed number, using either a constant suffix or a cast.
*/
std::string TypeData::applyTypeToConstant(const std::string& number) const
{
if(isString || isStruct)
return number;
std::string output = trimm(number);
if(output.empty())
{
if(isEnum && !enumName.empty())
return "(" + enumName + ")0";
else
return "0";
}
// Remove the existing suffix
if(startsWith(number, "0b"))
{
while(!output.empty() && !ShuntingYard::isNumber(output.back(), false, true))
output.erase(output.size() - 1, 1);
}
else if(startsWith(number, "0x"))
{
while(!output.empty() && !ShuntingYard::isNumber(output.back(), true, false))
output.erase(output.size() - 1, 1);
}
else
{
while(!output.empty() && !ShuntingYard::isNumber(output.back(), false, false))
output.erase(output.size() - 1, 1);
}
// Is the result still a number? It might not be if the input was an
// enumeration or defined constant or some such. In that case we just
// apply a cast and hope for the best.
bool ok;
double value = ShuntingYard::toNumber(output, &ok);
if(!ok)
return "(" + toTypeString() + ")" + number;
// Add the correct suffix for the numeric type
if(isFloat)
{
// Make sure we have a decimal point. If we already have one, but its
// the last character, add the 0. So "10" -> "10.0", or "10." -> "10.0"
if(output.find('.') == std::string::npos)
output += ".0";
else if(output.back() == '.')
output += "0";
// Finally put the 'f' at the end if this is a float
if(bits <= 32)
output += "f";
}
else
{
std::string suffix;
// In this case we only need the suffix if the integer constant is large enough
value = fabs(value);
if(value >= 4294967295.0)
suffix = "ll";
else if((value >= 2147483647.0) && isSigned)
suffix = "ll";
else if(value >= 65535.0)
suffix = "l";
else if((value >= 32767.0) && isSigned)
suffix = "l";
if(!suffix.empty())
{
if(!isSigned)
output += "u";
output += suffix;
}
}
return output;
}// TypeData::applyTypeToConstant
/*!
* Pull a positive integer value from a string
* \param string is the source string, which can contain a decimal or hexadecimal (0x) value
* \param ok receives false if there was a problem with the decode
* \return the positive integer value or 0 if there was a problem
*/
int TypeData::extractPositiveInt(const std::string& string, bool* ok)
{
int base = 10;
std::regex regex;
int value = 0;
if(contains(string, "0b"))
{
base = 2;
regex = std::regex(R"([^01])");
}
else if(contains(string, "0x"))
{
base = 16;
regex = std::regex(R"([^0123456789AaBbCcDdEeFf])");
}
else
{
base = 10;
regex = std::regex(R"([^0123456789])");
}
if(ok != nullptr)
(*ok) = true;
try
{
value = std::stoi(std::regex_replace(string, regex, ""), nullptr, base);
}
catch (...)
{
value = 0;
if(ok != nullptr)
(*ok) = false;
}
return value;
}
/*!
* Pull a double precision value from a string
* \param string is the source string
* \param ok receives false if there was a problem with the decode
* \return the double precision value or 0 if there was a problem
*/
double TypeData::extractDouble(const std::string& string, bool* ok)
{
int base = 10;
std::regex regex;
double value = 0;
if(contains(string, "0b") && !contains(string, "."))
{
base = 2;
regex = std::regex(R"([^01])");
}
else if(contains(string, "0x") && !contains(string, "."))
{
base = 16;
regex = std::regex(R"([^0123456789AaBbCcDdEeFf])");
}
else
{
base = 10;
regex = std::regex(R"([^0123456789\-\.])");
}
if(ok != nullptr)
(*ok) = true;
try
{
if(base == 10)
value = std::stod(std::regex_replace(string, regex, ""), nullptr);
else
value = std::stol(std::regex_replace(string, regex, ""), nullptr, base);
}
catch (...)
{
value = 0;
if(ok != nullptr)
(*ok) = false;
}
return value;
}
/*!
* Construct a blank protocol field
* \param parse points to the global protocol parser that owns everything
* \param parent is the hierarchical name of the parent object
* \param supported indicates what the protocol can support
*/
ProtocolField::ProtocolField(ProtocolParser* parse, std::string parent, ProtocolSupport supported):
Encodable(parse, parent, supported),
encodedMin(0),
encodedMax(0),
scaler(1),
hasVerifyMinValue(false),
verifyMinValue(0),
limitMinValue(0),
hasVerifyMaxValue(false),
verifyMaxValue(0),
limitMaxValue(0),
checkConstant(false),
overridesPrevious(false),
isOverriden(false),
inMemoryType(supported),
encodedType(supported),
prevField(0),
hidden(false),
neverOmit(false),
mapOptions(MAP_BOTH)
{
attriblist = {"name",
"title",
"inMemoryType",
"encodedType",
"struct",
"max",
"min",
"scaler",
"printscaler",
"array",
"variableArray",
"array2d",
"variable2dArray",
"dependsOn",
"dependsOnValue",
"dependsOnCompare",
"enum",
"default",
"constant",
"checkConstant",
"comment",
"Units",
"Range",
"Notes",
"bitfieldGroup",
"hidden",
"neverOmit",
"initialValue",
"verifyMinValue",
"verifyMaxValue",
"map",
"limitOnEncode"};
}
/*!
* Reset all data to defaults
*/
void ProtocolField::clear(void)
{
Encodable::clear();
encodedMin = encodedMax = 0;
scaler = 1;
defaultString.clear();
defaultStringForDisplay.clear();
constantString.clear();
constantStringForDisplay.clear();
checkConstant = false;
overridesPrevious = false;
isOverriden = false;
encodedType = inMemoryType = TypeData(support);
bitfieldData.clear();
scalerString.clear();
printScalerString.clear();
readScalerString.clear();
minString.clear();
maxString.clear();
prevField = 0;
extraInfoNames.clear();
extraInfoValues.clear();
hidden = false;
neverOmit = false;
initialValueString.clear();
verifyMinString.clear();
verifyMaxString.clear();
initialValueStringForDisplay.clear();
verifyMinStringForDisplay.clear();
verifyMaxStringForDisplay.clear();
limitMinString.clear();
limitMinStringForComment.clear();
limitMinValue = 0;
hasVerifyMinValue = false;
verifyMinValue = 0;
limitMaxString.clear();
limitMaxStringForComment.clear();
limitMaxValue = 0;
hasVerifyMaxValue = false;
verifyMaxValue = 0;
}// ProtocolField::clear
//! Provide the pointer to a previous encodable in the list
void ProtocolField::setPreviousEncodable(Encodable* prev)
{
prevField = NULL;
// We need to know when the bitfields end
if(prev != NULL)
{
// Does prev point to a ProtocolField?
prevField = dynamic_cast<ProtocolField*>(prev);
}
if(prevField == NULL)
return;
// Are we the start of part of a new bitfield group (or are we not a bitfield at all)?
// Which means the previous field terminates that group (if any)
if(bitfieldData.groupStart || !encodedType.isBitfield)
prevField->setTerminatesBitfield(true);
if(prevField->isBitfield() && (encodedType.isBitfield))
{
// Are we part of a bitfield group?
if(!bitfieldData.groupStart)
{
// We did not start a group, we might be a member of a previous group
bitfieldData.groupMember = prevField->bitfieldData.groupMember;
// Previous bitfield does not terminate the bitfields
prevField->setTerminatesBitfield(false);
// We start at some nonzero bitcount that continues from the previous
setStartingBitCount(prevField->getEndingBitCount());
// Now that we know our starting bitcount we can apply the bitfield defaults warning
if(!defaultString.empty() && (defaultString != "0") && (bitfieldData.startingBitCount != 0) && !bitfieldCrossesByteBoundary())
{
// The key concept is this: bitfields can have defaults, but if
// the bitfield does not start a new byte it is possible (not
// guaranteed) that the default will be overwritten because the
// previous bitfield will have caused the packet length to
// satisfy the length requirement. As an example:
//
// Old packet = 1 byte + 1 bit.
// New augmented packet = 1 byte + 2 bits (second bit has a default).
//
// The length of the old and new packets is the same (2 bytes)
// which means the default value for the augmented bit will
// always be overwritten with zero.
emitWarning("Bitfield with non-zero default which does not start a new byte; default may not be obeyed");
}
}
}// if previous and us are bitfields
computeEncodedLength();
}// ProtocolField::setPreviousEncodable
/*!
* Get overriden type information.
* \param prev is the previous encodable to test if its the source of the data being overriden by this encodable. Can be null
* \return true if prev is the source of data being overriden
*/
bool ProtocolField::getOverriddenTypeData(ProtocolField* prev)
{
// If we are overriding then this function is not interesting
if(!overridesPrevious)
return false;
// Check to make sure that this previous actually exists
if(prev == NULL)
return false;
// Must have the same name if we are overriding it
if(prev->name != name)
return false;
// Must exist in memory, or we can't be overriding it
if(prev->isNotInMemory())
return false;
// Let the previous one know that we are overriding it
prev->isOverriden = true;
// If we get here, then this is our baby. Update the data being overriden.
inMemoryType = prev->inMemoryType;
if(!inMemoryType.enumName.empty())
emitWarning("Enumeration name ignored for overridden field");
inMemoryType.enumName = prev->inMemoryType.enumName;
if(!array.empty())
emitWarning("Array information ignored for overridden field");
array = prev->array;
if(!array2d.empty())
emitWarning("2D Array information ignored for overridden field");
array2d = prev->array2d;
// This information can be modified, but is typically taken from the original.
if(variableArray.empty())
variableArray = prev->variableArray;
if(variable2dArray.empty())
variable2dArray = prev->variable2dArray;
if(dependsOn.empty())
dependsOn = prev->dependsOn;
if(comment.empty())
comment = prev->comment;
// Recompute the length now that the array data are up to date
computeEncodedLength();
return true;
}// ProtocolField::getOverriddenTypeData
//! Get the maximum number of temporary bytes needed for a bitfield group of our children
void ProtocolField::getBitfieldGroupNumBytes(int* num) const
{
if(encodedType.isBitfield && bitfieldData.lastBitfield && bitfieldData.groupMember)
{
int length = ((bitfieldData.groupBits+7)/8);
if(length > (*num))
(*num) = length;
}
}
/*!
* Extract the type information from the type string, for in memory types
* \param data holds the extracted type
* \param type is the type string
* \param inMemory is true if this is an in-memory type string, else encoded
* \param _enumName is the name of the enumeration, if this is an enumerated type.
*/
void ProtocolField::extractType(TypeData& data, const std::string& typeString, bool inMemory, const std::string& _enumName)
{
std::string type(typeString);
data.clear();
if(startsWith(type, "n"))
data.isNull = true;
else if(startsWith(type, "over") && inMemory)
{
overridesPrevious = true;
// This is just a place holder, it will get overriden later
data.bits = 32;
}
else if(startsWith(type, "stru"))
{
if(inMemory)
data.isStruct = true;
else
return;
}
else if(startsWith(type, "string"))
{
data.isString = true;
data.isFixedString = false;
data.bits = 8;
}
else if(startsWith(type, "fixedstring"))
{
data.isString = true;
data.isFixedString = true;
data.bits = 8;
}
else if(startsWith(type, "bo"))
{
// default to unsigned 8
data.bits = 8;
if(support.supportbool == false)
{
emitWarning("bool support is disabled in this protocol");
}
else if(!inMemory)
{
emitWarning("bool is not a valid encoded type - it can only be used for in-memory types");
}
else
{
data.isBool = true;
}
}
else if(startsWith(type, "bi"))
{
// Get the number of bits, between 1 and 32 inclusive
data.bits = data.extractPositiveInt(type);
if(support.bitfield == false)
{
emitWarning("bitfield support is disabled in this protocol");
// if bits is 1, then it becomes 8. If it is 8 then it
// becomes 8, if its 9 it becomes 16, etc.
data.bits = 8*((data.bits + 7)/8);
}
else
{
// Bitfields cannot be floats and cannot be signed
data.isBitfield = true;
// Bitfields must have at least one bit, and less than 33 bits
if(data.bits < 1)
{
data.bits = 1;
emitWarning("bitfields must have a bit width of at least one");
}
else if((data.bits > 32) && (support.longbitfield == false))
{
emitWarning("bitfields must have a bit width of 32 or less");
data.bits = 32;
}
else if(data.bits > 64)
{
emitWarning("bitfields must have a bit width of 64 or less");
data.bits = 64;
}
}
}
else if(startsWith(type, "e"))
{
// enumeration types are only for in-memory, never encoded
if(inMemory)
{
data.isEnum = true;
data.enumName = _enumName;
}
else
{
data.isEnum = false;
}
data.bits = 8;
}
else
{
// Get the number of bits, between 1 and 32 inclusive
data.bits = data.extractPositiveInt(type);
if(startsWith(type, "u"))
{
data.isSigned = false;
}
else
{
data.isSigned = true;
if(startsWith(type, "f"))
{
// we want to handle the cas where the user types "float16:10" to specify the number of significands
if(type.find(':') != std::string::npos)
{
std::vector<std::string> list = split(type, ":");
if(list.size() >= 2)
{
data.bits = data.extractPositiveInt(list.at(0));
data.sigbits = data.extractPositiveInt(list.at(1));
}
}
data.isFloat = true;
// "float" is not a warning
if(data.bits == 0)
data.bits = 32;
}
else if(startsWith(type, "d"))
{
data.isFloat = true;
// "double" is not a warning
if(data.bits == 0)
data.bits = 64;
}
else if(!startsWith(type, "s") && !startsWith(type, "i"))
{
emitWarning("in memory type name not understood, signed integer assumed");
}
}
if(data.isFloat)
{
if(inMemory)
{
if((data.bits != 32) && (data.bits != 64))
{
emitWarning("in memory float types must be 32 or 64 bits");
if(data.bits < 32)
data.bits = 32;
else
data.bits = 64;
}
if(data.sigbits != 0)
{
data.sigbits = 0;
emitWarning("in memory float types do not have variable resolution");
}
}
else
{
if((data.bits != 16) && (data.bits != 24) && (data.bits != 32) && (data.bits != 64))
{
emitWarning("encoded float types must be 16, 24, 32, or 64 bits");
if(data.bits < 16)
data.bits = 16;
else if(data.bits < 24)
data.bits = 24;
else if(data.bits < 32)
data.bits = 32;
else
data.bits = 64;
if((data.bits < 32) && (support.specialFloat == false))
{
emitWarning("non-standard float bit widths are disabled in this protocol");
data.bits = 32;
data.sigbits = 0;
}
}
if(data.sigbits != 0)
{
if(data.bits >= 32)
{
emitWarning("float type must be 16 or 24 bit to specify resolution");
data.sigbits = 0;
}
else if(data.bits == 24)
{
if((data.sigbits < 4) || (data.sigbits > 20))
{
emitWarning("significand (resolution) of float24 must be between 4 and 20 bits inclusive, defaulted to 15");
data.sigbits = 15;
}
}
else if(data.bits == 16)
{
if((data.sigbits < 4) || (data.sigbits > 12))
{
emitWarning("significand (resolution) of float16 must be between 4 and 12 bits inclusive, defaulted to 9");
data.sigbits = 9;
}
}
}// if user specified significant bits
else
{
// Default significand bits for float16 and float24
if(data.bits == 16)
data.sigbits = 9;
else if(data.bits == 24)
data.sigbits = 15;
}
}// else if not in memory float
if((data.bits > 32) && (support.float64 == false))
{
emitWarning("64 bit float support is disabled in this protocol");
data.bits = 32;
}
}// if float
else
{
if(inMemory)
{
if((data.bits != 8) && (data.bits != 16) && (data.bits != 32) && (data.bits != 64))
{
emitWarning("in memory integer types must be 8, 16, 32, or 64 bits");
if(data.bits > 32)
data.bits = 64;
else if(data.bits > 16)
data.bits = 32;
else if(data.bits > 8)
data.bits = 16;
else
data.bits = 8;
}
}
else if(((data.bits % 8) != 0) || (data.bits > 64))
{
emitWarning("encoded integer types must be 8, 16, 24, 32, 40, 48, 56, or 64 bits");