Unexpected Xs in the result of addition of two logic signals - system-verilog

I have a code where two signals are added. Only top bits of the signals are X. Bottom bits are 0s.
But, the result has all Xs. I would expect the bottom bits to be 0s in the result.
logic [3:0] a; // 4'bxx00
logic [3:0] b; // 4'bxx00
logic [4:0] c;
assign c = a + b; // Results in c = 4'bxxxxx
I am trying to understand why the bottom 2 bits are x in the result.

This behavior is specified in IEEE Std 1800-2017, section 11.4.3 Arithmetic operators:
For the arithmetic operators, if any operand bit value is the unknown
value x or the high-impedance value z , then the entire result value
shall be x .
The expression a + b contains an arithmetic operator: +

Related

Does Murmurhash have collisions on 32-bit inputs?

Consider the standard Murmurhash, giving 32-bit output values.
Suppose that we apply it on 32-bit inputs -- are there collisions?
In other words, does Murmurmash basically encodes a permutation when applied to 32-bit inputs?
If collisions exist, can anyone give an example (scanning random inputs didn't yield any)?
I assume you mean MurmurHash3, 32 bit, and specially the 32-bit fmix method:
FORCE_INLINE uint32_t fmix32 ( uint32_t h )
{
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
If not, then you need to better specify what you mean.
For the above, there are no collisions (two distinct inputs won't result in the same output). There is only one entry that returns the input value: 0.
As there are not "that many" 32-bit values, you can actually iterate over all of them to verify, in a couple of minutes. This will require some memory for a bit field, but that's it.
Btw, there is also a way to reverse the function (get the input from the output).

Boolean expression in linear program

I have to express an AND condition in linear program. The Boolean variable z takes a value 1 if both Boolean variables x and y takes a value 1. Otherwise, z takes a value 0. How do I write it in linear program?
In a pure linear program boolean expressions are not possible.
If you are in an (mixed-)integer program and x,y,z are all binary variables then you can implement the AND by the following.
z >= x+y-1
z <= x
z <= y
Here the first ensures z=1 if x=y=1 and the last two forces z=0 if any of the two is not 1.
As #Erwin Kalvelagen pointed out in the comments this is better relaxed than the formulation using 2z <= x+y.

Sum of integers using XOR, AND gates

How to compute sum of two integers using bit wise NOT, XOR, AND,and OR operators, without using SHIFT operator (not using arithmetic operators as well)? Is it possible ?
Example C code:
int a = 5;
int b = 11;
int c = a ^ b;
int d = a & b;
int sum = ...
Let's take a = b = 1. The result of this sum is sum = 2. The binary representation of the inputs are 00000001 (let's have 8-bits for simplicity). The output is 00000010. You can easily get the least significant bit, LSB = a0 XOR b0, but to modify the bit number 1 you need a shift.

Is there an equivalent MATLAB dec2bin function in Fortran?

I would like to find a similar function as dec2bin(d,n) in MATLAB, which produces a binary representation with at least n bits, for Fortran. Online I found this code, which converts decimal to binary but without a mechanism to decide the number of bits, and it asks for the user to decide whether its a positive integer or a positive real number...
Here is a solution based on the B edit operator (as per High Performance Mark's comment), that requires a fairly recent compiler.
!> Returns a binary representation of d as a string with at least n bits.
!> abs(d) < 2^52
function dec2bin(d, n)
implicit none
integer,intent(in) :: d
integer,intent(in),optional :: n
character(len=:),allocatable :: dec2bin
character(len=53) :: tmp
integer :: n_
character(len=8) :: f
if (present(n)) then
n_ = min(n, 53)
write(f,'(i2)') n_
f = '(B' // trim(adjustl(f)) // '.' // trim(adjustl(f)) // ')'
else
f = '(B53)'
endif
write(tmp,f) d
dec2bin = trim(adjustl(tmp))
end function
Note that this function does not check the sign (as the sign is correctly handled). If you want to limit this to positive integers, you need to do it outside the function.
Just because I had to look for the correct syntax myself, here are the relevant sections from the Fortran 2008 Standard, Cl. 10.7.2.4 "B, O, and Z editing":
1 The Bw , Bw .m, Ow , Ow .m, Zw , and Zw .m edit descriptors indicate that the field to be edited occupies w
positions, except when w is zero. When w is zero, the processor selects the field width. On input, w shall not be
zero. The corresponding input/output list item shall be of type integer, real, or complex.
[...]
6 The output field for the Bw .m, Ow .m, and Zw .m edit descriptor is the same as for the Bw, Ow, and Zw edit descriptor, except that the digit-string or hex-digit-string consists of at least m digits. If necessary, sufficient
leading zeros are included to achieve the minimum of m digits. The value of m shall not exceed the value of w ,
except when w is zero. [...]

Generating Unique ID Number from Two Integers

Given 2 integers a and b (positive or negative). Is there any formula / method for generating unique ID number?
note: 1. result from f(a,b) and f(b,a) should be different. 2. calculating f(a,b) for x times (x > 1), the result should be same.
To make clear about the question, this function f(n) = (n * p) % q (where n=input sequence value, p=step size, q=maximum result size, n=non-negative integer, n < q, p < q, p ⊥ q (coprime)) will give unique ID number.
But, in my requirement, input are two numbers, a and b can be negative or positive integer.
any reference is appreciable
You could generate a long (64 bit) from 2 integers (32 bit) by just right bit shifting the first integer with 32 and then add the second integer.
private long uniqueId(int left, int right) {
long uniqueId = (long) left;
uniqueId = uniqueId <<< 32;
uniqueId += (long) right;
return uniqueId;
}
Say your integers have a range in [MIN_INT,MAX_INT]. Then, given an integer n from this range, the function
f(n) = n - MIN_INT
attributes a unique positive integer f(n) in the range [0, MAX_INT - MIN_INT], which is often called a rank.
Denote M = MAX_INT - MIN_INT + 1. Then, to find a unique id g(n,m) of two concatenated integers n and m, you can use the common access style also used for two-dimensional arrays:
g(n,m) = f(n)*M + f(m)
That is, you simply offset the second integer by the largest possible value and count on.
Practically, of course, you have to be careful in order to avoid overflows -- that is, you should use some suited data types.
Here is an example: say your integers come from the range [-1,4], thus M=6. Then, for two integers n=3 and m=-1 out of this range, g(n,m) = 3*6 + 0 = 18 can be used as id.