Main Content

Logic and Bit Operations

hisl_0016: Usage of blocks that compute relational operators

ID: Titlehisl_0016: Usage of blocks that compute relational operators
DescriptionTo support the robustness of the operations, avoid using the equality and inequality operators on floating-point data types.
NotesDue to floating-point precision issues, do not test floating-point expressions for equality (==) or inequality (~=, !=).
RationaleImprove model robustness and prevent unexpected results.
Model Advisor ChecksCheck relational comparisons on floating-point signals (Simulink Check)
References
  • IEC 61508-3, Table A.3 (2) 'Strongly typed programming language’

    IEC 61508-3, Table A.3 (3) 'Language subset’

    IEC 61508-3, Table A.4 (3) 'Defensive programming'

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1 (1b) 'Use of language subsets'

    ISO 26262-6, Table 1 (1c) 'Enforcement of strong typing'

  • EN 50128, Table A.4 (11) 'Language Subset'

    EN 50128, Table A.4 (8) 'Strongly Typed Programming Language'

    EN 50128, Table A.3 (1) 'Defensive Programming'

  • DO-331, Section MB.6.3.1.g 'Algorithms are accurate'

    DO-331, Section MB.6.3.2.g 'Algorithms are accurate'

  • MISRA C:2012, Dir 1.1

See AlsoRelational Operations
Last ChangedR2021b
Examples

Ex: 1

Example — Correct

  • myDouble > 0.99 && myDouble < 1.01; % test range

Example — Incorrect

  • myDouble == 1.0

  • mySingle ~= 15.0

Ex: 2

Example — Correct

Equality comparison operators are not used in floating-point operands.

Example — Incorrect

Equality comparison operator == is used in floating-point operands.

Example — Correct

To test whether two floating-point variables or expressions are equal, compare the difference of the two variables against a threshold that takes into account the floating-point relative accuracy (eps) and the magnitude of the numbers.

The following pattern shows how to test two double-precision input signals, In1 and In2, for equality.

Example — InCorrect

Equality comparison operator == is used in floating-point operands

hisl_0017: Usage of blocks that compute relational operators (2)

ID: Titlehisl_0017: Usage of blocks that compute relational operators (2)
Description

To support unambiguous behavior in the generated code, when using blocks that compute relational operators, including Relational Operator, Compare To Constant, Compare to Zero, and Detect Change

ASet block parameter Output data type to Boolean.
BFor Relational Operator blocks, verify that input signals are of the same data type.
RationaleA, BSupport generation of code that produces unambiguous behavior.
Model Advisor ChecksCheck usage of Relational Operator blocks (Simulink Check)
References
  • IEC 61508-3, Table A.3 (2) 'Strongly typed programming language’
    IEC 61508-3, Table A.3 (3) 'Language subset’
    IEC 61508-3, Table A.4 (3) 'Defensive programming'

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1 (1b) 'Use of language subsets'
    ISO 26262-6, Table 1 (1c) 'Enforcement of strong typing'

  • EN 50128, Table A.4 (11) 'Language Subset'
    EN 50128, Table A.4 (8) 'Strongly Typed Programming Language'
    EN 50128, Table A.3 (1) 'Defensive Programming'

  • DO-331, Section MB.6.3.2.g 'Algorithms are accurate'

  • MISRA C:2012, Rule 10.1

See Alsohisl_0016: Usage of blocks that compute relational operators
Last ChangedR2018a

hisl_0018: Usage of Logical Operator block

ID: Titlehisl_0018: Usage of Logical Operator block
Description

To support unambiguous behavior of generated code, when using the Logical Operator block,

A

Set block parameter Output data type to Boolean.

B

Ensure input signals are of type Boolean.

Prerequisites

hisl_0045: Configuration Parameters > Math and Data Types > Implement logic signals as Boolean data (vs. double)

RationaleA, BAvoid ambiguous behavior of generated code.
Model Advisor ChecksCheck usage of Logical Operator blocks (Simulink Check)
References
  • DO-331, Section MB.6.3.2.g 'Algorithms are accurate'

  • IEC 61508-3, Table A.3 (2) 'Strongly typed programming language’
    IEC 61508-3, Table A.3 (3) 'Language subset’
    IEC 61508-3, Table A.4 (3) 'Defensive programming'

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1 (1b) 'Use of language subsets'
    ISO 26262-6, Table 1 (1c) 'Enforcement of strong typing'

  • EN 50128, Table A.4 (11) 'Language Subset'
    EN 50128, Table A.4 (8) 'Strongly Typed Programming Language'
    EN 50128, Table A.3 (1) 'Defensive Programming'

  • MISRA C:2012, Directive 10.1

Last ChangedR2017b

hisl_0019: Usage of bitwise operations

ID: Titlehisl_0019: Usage of bitwise operations
Description

To support unambiguous behaviour, when using bitwise operations,

A

Avoid bitwise operations on signed integer data types.

Notes

Bitwise operations are not meaningful on signed integers due to unpredictable behaviour. For example, a shift operation might move the sign bit into the number, or a numeric bit into the sign bit.

RationaleASupport unambiguous behavior of generated code.
Model Advisor ChecksCheck usage of bit operation blocks (Simulink Check)
References
  • DO-331, Section MB.6.3.2.g 'Algorithms are accurate'

  • IEC 61508-3, Table A.3 (3) 'Language subset’
    IEC 61508-3, Table A.3 (2) 'Strongly typed programming language’

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1 (1b) 'Use of language subsets'
    ISO 26262-6, Table 1 (1c) 'Enforcement of strong typing'
    ISO 26262-6, Table 1 (1d) 'Use of defensive implementation techniques'

  • EN 50128, Table A.4 (11) 'Language Subset'
    EN 50128, Table A.3 (1) 'Defensive Programming'
    EN 50128, Table A.4 (8) 'Strongly Typed Programming Language'

  • EN 50128, Table A.3 (1) 'Defensive Programming'

  • EN 50128, Table A.4 (8) 'Strongly Typed Programming Language'

  • MISRA C:2012, Rule 10.1

See Alsohisl_0073: Usage of bit-shift operations
Last ChangedR2024a