Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small quality of life enhancement proposals #585

Closed
rmiguelc opened this issue May 9, 2017 · 3 comments
Closed

Small quality of life enhancement proposals #585

rmiguelc opened this issue May 9, 2017 · 3 comments

Comments

@rmiguelc
Copy link

rmiguelc commented May 9, 2017

1.1 - Constant conjunction

It would be interesting to be able to do the following:

#define HASH_INSTANCES 4
#define HASH_LENGTH 16 //in bits

#define TOTAL_OUTPUT_LENGTH ( HASH_INSTANCES * HASH_LENGTH )

The compiler accepts this, but if I write bit<TOTAL_OUTPUT_LENGTH> var it forbids it:

../../f/skeleton.p4(52):syntax error, unexpected (, expecting INTEGER
bit<(

1.2 - Logarithms and powers

And to use power or logarithm base 2 functions.

#define NUMBER_OF_SWITCH_PORTS 128
#define NUMBER_OF_PORTID_BITS ( log(NUMBER_OF_SWITCH_PORTS) + 1 )

typedef PortId bit<NUMBER_OF_PORTID_BITS>

2.1 - Shift operator

bit<5> b = 1;
bit<5> var_one = 1 << 1; //this will compile just fine
bit<5> var_two = 1 << b; //this will not compile

../../f/skeleton.p4(81): error: << left operand of shift must be a numeric type, not int
bit<2> var_two = 1 << b;

@mihaibudiu
Copy link
Contributor

mihaibudiu commented May 9, 2017

I would suggest filing separate issues for each problem; this makes it much simpler to solve them one by one. The first one is a duplicate of #47.

The second one is trickier, since you would have to define what the log is for non-powers of two and for negative numbers. In general you should define a macro for the log and compute the number of ports:

#define LOG_PORTS 7
#define PORTS (1 << LOG_PORTS)

For the third one the spec requires that the size of the shifted operand to be specified, unless both operands are infinite-precision. Remember that shift will discard bits, and it is not clear how wide is your 1. If you replace 1 with 5w1 it will work. In your example b evaluates to a constant, but in general you need to know how wide the 1 if you evaluate this at run-time. Another possibility is to declare b as const.

@mihaibudiu
Copy link
Contributor

So I don't really know if there is anything to fix for this issue.
Maybe we can close it.

@rmiguelc
Copy link
Author

Thank you for responding, Mihai :-)
Looking forward to seeing #47 resolved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants