Error observed in using bitsll and bitsra

When utilizing bitsll and bitsra, an error was noticed.
I have the following code and intend to utilize Embedded.fi [fixed point ] as double implementation show in case -1.
Can someone please show me how to code correctly and help me understand what I'm missing in cases 2 and 3?
It would be nice if I could do cases 2 and 3 in a single function/ single solution.
Thank you very much.
% case -1 : double
k = linspace(-12,12,10);
for i = 1 : length(k)
a = 2 ^ k(i)
end
a = 2.4414e-04
a = 0.0016
a = 0.0098
a = 0.0625
a = 0.3969
a = 2.5198
a = 16
a = 101.5937
a = 645.0796
a = 4096
% case -2: Embdedded fi ,fixed point - right shift
k = fi(linspace(-12,12,10),1,32,27);
a = int8(1);
for i = 1 : length(k)
% fxpOut = bitsra(a,k(i))
disp(bin(bitsra(a,k(i))))
end
Error using bitsra
K must be numeric, scalar, real, integer-valued, and greater than or equal to zero in BITSRA(A,K).

Error in embedded.fi/bitsra (line 38)
y = bitsra(x, double(kin));
% case - 3: Embdedded fi ,fixed point - left shift
k = fi(linspace(-12,12,10),1,32,27);
a = int8(1);
for i = 1 : length(k)
fxpOut = bitsll(a,k(i))
disp(bin(bitsll(a,k(i))))
end

4 件のコメント

Richard McCormack
Richard McCormack 2022 年 9 月 23 日
As th error messages indicates, the issue is that linspace(-12,12,10) results in k containing negative values. bitsll and bitsra do not support shifting with a negative value. Can you explain what you like the behavior to be in the case where k(i) is negative?
Life is Wonderful
Life is Wonderful 2022 年 9 月 25 日
編集済み: Life is Wonderful 2022 年 9 月 26 日
Thank you!!.
As you indicated, bitsll and bitsra do not support negative values; do we have an alternative way that uses I can do an in fixed point calculation?
kn = [-12]
kn = -12
kp = [12]
kp = 12
dblout = 2 ^ kn
dblout = 2.4414e-04
fpxout = 2 ^ kp
fpxout = 4096
In my head, I am carrying out the following procedure.
K = [-12,12]
K = 1×2
-12 12
for i = 1:length(K)
if sign(K(i)) < 0 % Bitsliceget to know bitposition and do a bitand for sign
% Negative
b = fi(bitsra(1,abs(K(i))));
disp(bin(b))
else
% Positive
b_i = fi(bitsra(1,abs(K(i))));
disp(bin(b_i))
end
end
say
k = -12
k = -12
fpx_neg = bitsra(1,abs(k))
fpx_neg = 2.4414e-04
k = 12
k = 12
fpx_pos = bitsll(1,abs(k))
fpx_pos = 4096
I'm confident that you'll have more ideas than I did.
Thank you very much
Richard McCormack
Richard McCormack 2022 年 9 月 26 日
I am happy that you made progress!
I made some adjustments to your code to make it work for code generation.
But maybe there is a reason that you can't use abs. If that is the case, please let me know why you can't use abs.
function scratch
coder.extrinsic('bin');
K = [-12,12];
coder.unroll;
for i = 1:length(K)
if sign(K(i)) < 0 % Bitsliceget to know bitposition and do a bitand for sign
% Negative
b = fi(bitsra(1,abs(K(i))));
disp(bin(b))
else
% Positive
b_i = fi(bitsra(1,abs(K(i))));
disp(bin(b_i))
end
end
end
Life is Wonderful
Life is Wonderful 2022 年 9 月 26 日
編集済み: Life is Wonderful 2022 年 9 月 28 日
Thanks a lot @Richard McCormack.
I am fine in using abs. I will try out your suggested code and make some test.

サインインしてコメントする。

回答 (0 件)

カテゴリ

製品

リリース

R2022a

質問済み:

2022 年 9 月 22 日

編集済み:

2022 年 9 月 28 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by