Skip to content

Commit ad43ded

Browse files
committed
Target signals type selection optimized.
1 parent 83d894d commit ad43ded

File tree

4 files changed

+75
-81
lines changed

4 files changed

+75
-81
lines changed

src/codegen/c-main-generator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ void CiMainGenerator::WriteSigStructField(const SignalDescriptor_t& sig, bool bi
581581

582582
std::string dtype = "";
583583

584-
dtype += " " + PrintType((int)sig.Type) + " " + sig.Name;
584+
dtype += " " + PrintType((int)sig.TypeRo) + " " + sig.Name;
585585

586586
if (bits && (sig.LengthBit < 8))
587587
{
@@ -656,7 +656,7 @@ void CiMainGenerator::WriteSigStructField(const SignalDescriptor_t& sig, bool bi
656656
}
657657
else
658658
{
659-
fwriter->AppendLine(StrPrint(" %s %s;", PrintType((int)sig.Type).c_str(), sig.NameFloat.c_str()));
659+
fwriter->AppendLine(StrPrint(" %s %s;", PrintType((int)sig.TypePhys).c_str(), sig.NameFloat.c_str()));
660660
}
661661

662662
fwriter->AppendLine(StrPrint("#endif // %s", fdesc->usesigfloat_def.c_str()), 2);

src/codegen/c-sigprinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ std::string CSigPrinter::PrintPhysicalToRaw(const SignalDescriptor_t* sig, const
6565
}
6666

6767
retstr += StrPrint("#define %s_%s_toS(x) ( (%s) ", drvname.c_str(), sig->Name.c_str(),
68-
PrintType((uint8_t)sig->Type).c_str());
68+
PrintType((uint8_t)sig->TypeRo).c_str());
6969

7070
if (sig->IsDoubleSig)
7171
{

src/parser/dbclineparser.cpp

Lines changed: 68 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ bool DbcLineParser::ParseSignalLine(SignalDescriptor_t* sig, const std::string&
262262
// value_type = '+' | '-'; (*+= unsigned, -=signed*)
263263
sig->Signed = (valpart[2].find('-') == std::string::npos) ? 0 : 1;
264264

265-
sig->Type = GetSigType(sig);
265+
GetSigType(sig);
266266

267267
// mark all simple signals to make using them easier
268268
if (!sig->IsDoubleSig && (sig->Factor == 1) && (sig->Offset == 0))
@@ -312,109 +312,101 @@ SigType DbcLineParser::GetSigType(SignalDescriptor_t* sig)
312312

313313
uint64_t max_v = 0;
314314

315-
if (!sig->IsDoubleSig)
315+
// 1 step is to detect type of _ro
316+
int64_t max_abs, min_abs;
317+
318+
int64_t addon = 0;
319+
320+
if (sig->Signed)
316321
{
317-
int64_t i_offset = (int64_t)sig->Offset;
318-
int64_t i_factor = (int64_t)sig->Factor;
322+
addon = 1;
323+
max_abs = static_cast<int64_t>( (std::pow(2, sig->LengthBit - 1) - 1) );
324+
min_abs = (max_abs + 1) * -1;
319325

320-
// for this case physical value needs to be allowed to
321-
// fit inside field type
322-
if (sig->Signed)
326+
for (size_t i = 0; i < 4; i++)
323327
{
324-
// physical_value = raw_value * factor + offset
325-
// 1 get the max value for the positive part
326-
max_v = (uint64_t)(std::pow(2, sig->LengthBit - 1));
327-
// 2 scale to max value
328-
max_v *= i_factor;
329-
330-
// 3 add offset
331-
// factor affects on difference between min and max values
332-
// abs(max_neg)-(max_pos) = sig->Factor;
333-
// so if offset == Factor
334-
if (sig->Offset > (int32_t)(sig->Factor - 1))
335-
{
336-
// positive value less then
337-
max_v = (max_v + i_offset - ((int64_t)sig->Factor));
338-
}
339-
else
328+
sig->TypeRo = (SigType)(i);
329+
330+
if (max_abs <= __maxsignedvals[i])
340331
{
341-
max_v = (max_v - i_offset) - 1;
332+
break;
342333
}
334+
}
335+
}
336+
else
337+
{
338+
max_abs = static_cast<int64_t>( (std::pow(2, sig->LengthBit) - 1) );
339+
min_abs = 0;
343340

344-
for (uint8_t i = 0; i < 4; i++)
341+
for (size_t i = 0; i < 4; i++)
342+
{
343+
sig->TypeRo = (SigType)(i + 4);
344+
345+
if (max_abs <= __maxunsigvalues[i])
345346
{
346-
if (max_v <= (__maxsignedvals[i]))
347-
{
348-
ret = (SigType)(i + (is_unsigned * 4));
349-
break;
350-
}
347+
break;
351348
}
352349
}
353-
else
354-
{
355-
is_unsigned = 1;
350+
}
356351

357-
max_v = (uint64_t)(std::pow(2, sig->LengthBit)) - 1;
352+
if (sig->IsSimpleSig)
353+
{
354+
// the most simple case, TypePhys is the same as TypeRo
355+
sig->TypePhys = sig->TypeRo;
356+
}
358357

359-
max_v *= (int32_t)sig->Factor;
358+
else if (sig->IsDoubleSig == false)
359+
{
360+
int64_t i_offset = (int64_t)sig->Offset;
361+
int64_t i_factor = (int64_t)sig->Factor;
360362

361-
if (sig->Offset >= 0)
363+
// get max and min values with applied factor and offset (physical values)
364+
max_abs = max_abs * i_factor + i_offset;
365+
min_abs = min_abs * i_factor + i_offset;
366+
367+
if (sig->Signed || max_abs < 0 || min_abs < 0)
368+
{
369+
// phys value must be signed
370+
uint64_t max_v = std::abs(max_abs);
371+
uint64_t addon = 0;
372+
373+
if ((max_v + 1) < std::abs(min_abs))
362374
{
363-
// when offset positive the max physical value is got just
364-
// adding roffset value
365-
max_v += roffset;
375+
// low part is main
376+
addon = 1;
377+
max_v = std::abs(min_abs);
366378
}
367-
else
379+
380+
for (size_t i = 0; i < 4; i++)
368381
{
369-
is_unsigned = 0;
370-
// this code must determmine which part of range is larger - positive or negative
371-
// the largest part will define which sig type will be used for signal
372-
// roffset here - negative value
373-
374-
// max positive value fot the LenBits if it would have signed type
375-
uint64_t maxpos = (uint64_t)(std::pow(2, sig->LengthBit) - 1);
376-
// max negative value
377-
uint64_t maxneg = (uint64_t)(std::abs(roffset));
378-
379-
max_v = std::max(maxpos, maxneg);
380-
// mul 2 for using unsinged compare levels (int8_t (127) like uint8_t (255))
381-
max_v *= 2;
382-
}
382+
sig->TypePhys = (SigType)(i);
383383

384+
if (max_v <= __maxsignedvals[i] + addon)
385+
{
386+
break;
387+
}
388+
}
389+
}
390+
else
391+
{
392+
// phys value must be unsigned
384393
for (uint8_t i = 0; i < 4; i++)
385394
{
386-
if (max_v <= (__maxunsigvalues[i]))
395+
if ((uint64_t)max_abs <= __maxunsigvalues[i])
387396
{
388-
ret = (SigType)(i + (is_unsigned * 4));
397+
sig->TypePhys = (SigType)(i + 4);
389398
break;
390399
}
391400
}
392401
}
393402
}
394403
else
395404
{
396-
// this type definition is simple (without
397-
// additional type-expanded operations inside
398-
// main driver, so to determine type simple
399-
// operations is needed
400-
max_v = (uint64_t)(std::pow(2, sig->LengthBit) - 1);
401-
402-
if (!sig->Signed)
403-
{
404-
is_unsigned = 1;
405-
}
406-
407-
for (uint8_t i = 0; i < 4; i++)
408-
{
409-
if (max_v <= __maxunsigvalues[i])
410-
{
411-
ret = (SigType)(i + (is_unsigned * 4));
412-
break;
413-
}
414-
}
405+
// in this case TypePhys will be (sigfloat_t), so
406+
// there is no necessity to determine physical signal type
415407
}
416408

417-
return ret;
409+
return sig->TypeRo;
418410
}
419411

420412
bool DbcLineParser::ParseCommentLine(Comment_t* cm, const std::string& line)

src/types/message.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef struct
5858

5959
// this flag shows if the signal has factor = 1 and offset = 0
6060
// to reject any sigfloat or "toS"/"fromS" operations
61-
// this only when : IsDoubleSig == true || ((s.Factor != 1) || (s.Offset != 0)
61+
// SimpleSig is true when: IsDoubleSig == false && Factor == 1 && Offset == 0
6262
bool IsSimpleSig;
6363

6464
double Factor;
@@ -71,7 +71,9 @@ typedef struct
7171

7272
bool Signed;
7373

74-
SigType Type;
74+
SigType TypeRo;
75+
76+
SigType TypePhys;
7577

7678
std::vector<std::string> SigToByte;
7779

0 commit comments

Comments
 (0)