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

Antonin Bas antonin at barefootnetworks.com
Mon Jun 20 14:39:08 EDT 2016


Hi,


I just noticed that modulo and divide where not supported by the P4
frontend, so I made the change. In P4v1.1, using modulo would look like
this:
action my_action(in int<32> a, in int<32> b) {
    modify_field(*h1.f1*, a % b);  // a >= 0, b > 0
    // or *h1.f1* = a % b;
}
The second syntax is supported by the p4-hlir compiler, but I don't think
it is described in the official spec.
This action can then be referenced by a table.
Really now that I have made the change in the frontend, using "modulo" is
no different from using "add" or "substract". See
https://github.com/p4lang/tutorials/blob/master/p4v1_1/simple_router/p4src/simple_router.p4#L129
for a P4v1.1 program.

Thanks,

Antonin

On Mon, Jun 20, 2016 at 4:53 AM, Christian Esteve Rothenberg <
chesteve at dca.fee.unicamp.br> wrote:

> Thanks Antonin for your comprehensive reply!
>
> Are you aware of any publicly available P4 code example using the
> modulo operation?
>
> Just a code snippet would suffice...
>
> thanks,
> Christian
>
> On Fri, Jun 17, 2016 at 5:05 PM, Antonin Bas
> <antonin at barefootnetworks.com> wrote:
> > 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
> >
> > _______________________________________________
> > 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/20160620/debbbd58/attachment-0002.html>


More information about the P4-dev mailing list