[P4-dev] Where should the operations on arbitrary-precision integers be declared?

Antonin Bas antonin at barefootnetworks.com
Fri Jun 17 16:05:53 EDT 2016


Hi Javier,

In P4v1.1, the only variables you have are fields (packet fields and
metadata fields), and they have global scope. These variables all have a
type which includes their bitwidth (with the exception of the very specific
variable-length field case). For example, the ethernet header can be
declared as follows:

header_type ethernet_t {
    fields {
        bit<48> dstAddr;
        bit<48> srcAddr;
        bit<16> etherType;
    }
}


Notice that all three fields are bitstrings. One can also declare fields
has signed integers (int<*w*>), usually this is more useful for some
metadata fields.
As mentioned in the spec, you cannot declare fields a "int" (without a
width). This is reserved for evaluation of compile-time constants. The
"int" type is used by the compiler but is hidden from the P4 programmer.
For example, let's look at the following code:
ethernet.etherType = (bit<16>) (11111111 - 1);
The compiler will use the internal "int" type to represent compile-time
constants 11111111 and 1. It will also compute the difference, 11111110,
which is also of type "int". This is what we call "arbitrary-precision
arithmetic" since the compiler can handle arbitrarily large compile time
constants. Finally the compiler should give you a warning because 11111110
cannot be represented using 16 bits and the most significant bits will be
lost.

Best,

Antonin


On Thu, Jun 16, 2016 at 4:13 PM, Javier <richardqa at gmail.com> wrote:

> Hi everyone,
>
> I am trying to use the operations on "arbitrary-precison constant
> integers", however I dont get to know how to declare these constant
> integers. In the P4 spec for v1.1 tell that "the only values that can have
> the type *int* are compile-time constants" but it doesn't show how to use
> it. For example, i want to use the operator modulo to get the *modulo* of
> two variables, where and how should I declare these variables?.
>
> Thank in advance,
> Javier
>
>
> --
> Saludos Cordiales
> Javier
> Fingerprint: 7AF8 4E03 66C2 2663 3A77  7067 6B5A D8F9 EE91 47BE
>
> _______________________________________________
> P4-dev mailing list
> P4-dev at lists.p4.org
> http://lists.p4.org/mailman/listinfo/p4-dev_lists.p4.org
>



-- 
Antonin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.p4.org/pipermail/p4-dev_lists.p4.org/attachments/20160617/d8f07ddf/attachment-0002.html>


More information about the P4-dev mailing list