Closed KIZI-01 closed 2 years ago
You can do modulo by powers of 2 pretty easily, since those can be accomplished by bit-wise AND operations, e.g. (a mod 16)
is the same as (a & 0xf)
.
If you are using simple_switch_grpc
or simple_switch
to execute your P4 programs, and the v1model architecture (i.e. you have #include "v1model.p4" near the beginning of your P4 programs), there is a hash
extern function that can be called in such a way that it can calculate (a mod b)
. I haven't tested this, but something close to this should work:
bit<16> a; bit<16> b; bit<16> mod_result;
hash(mod_result, HashAlgorithm.identity, 0, {a}, b);
See here for some documentation on the parameters and the behavior: https://github.com/p4lang/p4c/blob/master/p4include/v1model.p4#L437-L453
HashAlgorithm.identity
means NOT to calculate a hash function on the parameter {a}
, but simply to use the value of {a}
unchanged.
You can do modulo by powers of 2 pretty easily, since those can be accomplished by bit-wise AND operations, e.g.
(a mod 16)
is the same as(a & 0xf)
.If you are using
simple_switch_grpc
orsimple_switch
to execute your P4 programs, and the v1model architecture (i.e. you have #include "v1model.p4" near the beginning of your P4 programs), there is ahash
extern function that can be called in such a way that it can calculate(a mod b)
. I haven't tested this, but something close to this should work:bit<16> a; bit<16> b; bit<16> mod_result;
hash(mod_result, HashAlgorithm.identity, 0, {a}, b);
See here for some documentation on the parameters and the behavior: https://github.com/p4lang/p4c/blob/master/p4include/v1model.p4#L437-L453
HashAlgorithm.identity
means NOT to calculate a hash function on the parameter{a}
, but simply to use the value of{a}
unchanged.
Thank you for your help !I gained knowledge that the P4 tutorial didn't. I will eventually implement my P4 program on the tofino architecture(https://github.com/barefootnetworks/Open-Tofino/blob/master/share/p4c/p4include/tofino.p4#L500-L514). However, the two use hash extern function quite differently. How do I take the modulus in the tofino architecture?
I know you are asking about integer modulo, not floating point operations, but the section "Other operations" in the article linked below might of interest to you. It isn't cheap if you want large operands for a modulo operation, but it can be made to work for pretty much any P4-programmable device, Tofino or otherwise, if you can make tables large enough for your desired purposes: https://github.com/jafingerhut/p4-guide/blob/master/docs/floating-point-operations.md
There may be a cheaper way in Tofino. If you have access to Tofino customer engineering support forums, I would recommend asking there. I can check through other channels, but I suppose one question is: do you actually need an integer modulo operation? Or could you get by with whatever an ActionSelector extern is doing internally to pick one member of.a group with equal weight?
need an integer modulo operation
我知道您在问整数模,而不是浮点运算,但是您可能会对下面链接的文章中的“其他运算”部分感兴趣。如果您想对模运算进行大操作数运算,这并不便宜,但是如果您可以使表足够大以实现所需的目的,则可以使其适用于几乎所有的P4可编程设备(Tofino或其他设备): /github.com/jafingerhut/p4-guide/blob/master/docs/floating-point-operations.md
在托菲诺,也许有一种更便宜的方式。如果您可以访问Tofino客户工程支持论坛,建议在此进行询问。我可以检查其他渠道,但我想一个问题是:您实际上是否需要整数模运算?还是您可以通过ActionSelector extern内部所做的任何事情来选择一个权重相等的组中的一个成员?
I'm sure I need an integer modulo operation. In your articles,I think I've found the answer:use a table,but the code is very repetitive.If only there was an easier way. Thanks again for your help!Hope you enjoy your life!
The code is very repetitive, but it is also repetitive in a way that is easy to write a tiny C, Python, etc. program (whatever your most familiar programming language is), and use that to "print out" the repetitive part of the P4 program. See this article for a short example using Python to generate repetitive P4 code: https://github.com/jafingerhut/p4-guide/blob/master/code-generation/README.md
Closing this issue, as it seems to have been addressed to the issue creator's satisfaction. I would recommend the forum.p4.org web site for similar questions in the future.
In the P416 Language Specification,only int types can be modulo between positive values.But I want to take the modulus of int\<w> or bit\<w> types,how do I do that?