[P4-dev] Help to writing functor using available C++ functions/library

Antonin Bas antonin at barefootnetworks.com
Wed Feb 10 02:09:05 EST 2016


Hi Swaroop,

The return type of your functor is uin16_t, so trying to return -1 is
probably not a good idea. Note that according to bmv2 design you cannot
define a hash function with a signed return type.
Now I am pretty sure that if you return 1 or 2 based on a random output,
you will observe the correct behavior.

Antonin

On Tue, Feb 9, 2016 at 10:33 AM, Swaroop Thool <swaroopthool1991 at outlook.com
> wrote:

> Hi Everyone,
>
> I want to write functor for generating random values of +1/-1 to implement
> one of my interested algorithm in p4. So referring Antonin's post on
> writing own hash functions I have written something like this:
>
> *struct my_hash {*
> *     uint16_t operator()(const char *buf, size_t s) const {   *
> * return (random() & 1 ? 1 : -1);*
> *       }*
> *  };*
>
> *REGISTER_HASH(my_hash);  *
>
> I can get access for it by writing:
> *field_list_calculation myhash  {*
> *    input {*
> *        hashvalue_list;                               * // *I dont
> require this , is this necessary?*
> *    }*
> *    algorithm : my_hash; *
> *    output_width : 16;*
> *}*
>
> Then calling in some action :
> * modify_field_with_hash_based_offset(mymetadata.val,myhash,0,1000);*
>
> When I add this in my previous program which implements copy_to_cpu , I
> can see only 00 00 for both +1 and -1? Is this because negative value
> support explanation of my previous post applies here? If yes, then for both
> +1 and -1  it should have come as 01 01.
> OR I am writing above functor in wrong way? Please help.
>
> Thanks,
> Swaroop
>
> _______________________________________________
> 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/20160209/8a7c538a/attachment-0002.html>


More information about the P4-dev mailing list