RE: [sv-bc] Equality operators on unpacked arrays

From: Bresticker, Shalom <shalom.bresticker@intel.com>
Date: Thu Nov 25 2010 - 05:06:45 PST

Hi,

I was recently asked a more general question: what are the operand compatibility rules for comparison operators?
 
It seems that this information is spread out over the LRM, and cannot be found just by looking at the operator description sections.
This is what I found.

(Also see Mantis 3229 (http://www.eda-stds.org/mantis/view.php?id=3229).)

Note that there are different types of comparison: logical equality, wildcard equality, case equality, relational (>, >=, <, <=).
One might consider "if (condition)" to also be a comparison operator.

First, descriptions of the operators:

"11.3 Operators, Table 11-1—Operators and data types

Operator token Name Operand data types

== != binary logical equality operators any
=== !== binary case equality operators any except real and shortreal (Note: not virtual interfaces, see 25.9)
==? !=? binary wildcard equality operators integral
< <= > >= binary relational operators integral, real, shortreal (Note: need to add string type, see 6.16)"

"11.4.4 Relational operators

If either operand is a real operand, then the other operand shall be converted to an equivalent real value and the expression shall be interpreted as a comparison between real values.

11.4.5 Equality operators

The logical equality (or case equality) operator is a legal operation if either operand is a class object or the literal null, and one of the operands is assignment compatible with the other. The logical equality (or case equality) operator is a legal operation if either operand is a chandle or the literal null. In both cases, the operator compares the values of the class objects or chandles.

If either operand is a real operand, then the other operand shall be converted to an equivalent real value, and the expression shall be interpreted as a comparison between real values.

11.4.6 Wildcard equality operators

The wildcard equality operator is equivalent to the logical equality operator if its operands are class objects, chandles or the literal null."

(Note that Table 11-1 says that this operator may only have integral operands. See Mantis 3229.)

And here are descriptions of the data types:

"8.4 Objects (class instance)

An uninitialized object can be detected by comparing its handle with null.

Only the following operators are valid on object handles:

— Equality (==), inequality (!=) with another class object or with null. One of the objects being compared must be assignment compatible with the other.
— Case equality (===), case inequality (!==) with another class object or with null (same semantics as == and !=)."

"6.14 Chandle data type

Only the following operators are valid on chandle variables:

— Equality (==), inequality (!=) with another chandle or with null
— Case equality (===), case inequality (!==) with another chandle or with null (same semantics as == and !=)
— Chandles can be tested for a Boolean value, which shall be 0 if the chandle is null and 1 otherwise."

"6.16 String data type

Str1 == Str2 Equality. Checks whether the two string operands are equal. Result is 1 if they are equal and 0 if they are not. Both operands can be expressions of string type, or one can be an expression of string type and the other can be a string literal, which shall be implicitly converted to string type for the comparison. If both operands are string literals, the operator is the same equality operator as for integral types.

Str1 != Str2 Inequality. Logical negation of ==

Str1 < Str2
Str1 <= Str2
Str1 > Str2
Str1 >= Str2
Comparison: Relational operators return 1 if the corresponding condition is true using the lexicographic ordering of the two strings Str1 and Str2. The comparison uses the compare string method. Both operands can be expressions of string type, or one can be an expression of string type and the other can be a string literal, which shall be implicitly converted to string type for the comparison. If both operands are string literals, the operator is the same comparison operator as for integral types."

"6.17 Event data type

An event variable can be assigned or compared to another event variable or assigned the special value null.

15.5.5.3 Events comparison
Event variables can be compared against other event variables or the special value null. Only the following operators are allowed for comparing event variables:

— Equality (==) with another event or with null
— Inequality (!=) with another event or with null
— Case equality (===) with another event or with null (same semantics as ==)
— Case inequality (!==) with another event or with null (same semantics as !=)
— Test for a Boolean value that shall be 0 if the event is null and 1 otherwise"

"6.23 Type operator

The type operator … can also be used in equality/inequality and case equality/inequality comparisons with other type references.

When a type reference is used in an equality/inequality or case equality/inequality comparison, it shall only be compared with another type reference. Two type references shall be considered equal in such comparisons if, and only if, the types to which they refer match (see 6.22.1)."

"25.9 Virtual interfaces

Only the following operations are directly allowed on virtual interface variables:

— Equality ( == ) and inequality ( != ) with the following:
— Another virtual interface of the same type
— An interface instance of the same type
— The special constant null"

"7.4.3 Operations on arrays

The following operations can be performed on all arrays, packed or unpacked. The examples provided with these rules assume that A and B are arrays of the same shape and type.

— Equality operations on the array or slice of the array, e.g., A==B, A[i:j] != B[i:j]

11.2.2 Aggregate expressions

Unpacked structure and array data objects, as well as unpacked structure and array constructors, can all be used as aggregate expressions. A multi-element slice of an unpacked array can also be used as an aggregate expression.

Aggregate expressions can also be compared with equality or inequality operators.

If the two operands of a comparison operator are aggregate expressions, they shall be of equivalent type as defined in 6.22.2."

It may be instructive also to look at 11.4.11, on the conditional operator. There is discussion of various types of operators, and also the sentence,

"For all other cases, the type of the first expression and second expression shall be equivalent (see 6.22.2)."

Hope this helps.

Regards,
Shalom

Shalom Bresticker
Intel LAD DA, Jerusalem, Israel
+972 2 589 6582 (office)
+972 54 721 1033 (cell)
http://www.linkedin.com/in/shalombresticker

> -----Original Message-----
> From: owner-sv-bc@eda.org [mailto:owner-sv-bc@eda.org] On Behalf Of
> Paul Graham
> Sent: Wednesday, November 24, 2010 11:38 PM
> To: sv-bc
> Subject: [sv-bc] Equality operators on unpacked arrays
>
> The lrm says that two unpacked arrays may be compared with == or !=
> provided the arrays have "the same shape and type". Is this to say
> that the arrays must be assignment compatible?

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Thu Nov 25 05:08:11 2010

This archive was generated by hypermail 2.1.8 : Thu Nov 25 2010 - 05:10:45 PST