What would be the C equivalent of rlwinm (PPC Instruction) - c

I was wondering if any of you would know the C equivelent of the powerpc instruction below.
rlwinm r31, r0, 0,13,13
Thanks.

Rotate left register immediate, then and with mask.
Rotate left is 0 here, so we can ignore this. The mask is all bits set from 13 to 13, which is just bit 13 (0x2000 as a bitmask; this command was probably chosen over just and to document that bit 13 is selected).
So in this case, we need to build a mask for bit 13 and then apply bitwise and with the source.
r31 = r0 & (1 << 13);
<< is the shift left operation in C, we use it here to create a mask just for bit 13. & is the and operation in C.
Documentation source: http://sametwice.com/rlwinm

Related

Mapping specific bits in input bytes to specific bits in output word

Background: Given some input bytes B0, B1, B2, B3 and B4, I want to extract selected bits from these 5 bytes and generate an output word.
For example, denoting the nth bit of Bi as Bi[n], I want to be able to write a mapping f : (B0, B1, B2, B3, B4) → B2[4] B3[5] B3[4] B3[3] B3[2] B3[1] B0[5] B0[3] B0[1]. So f(0b11001, 0b01100, 0b10101, 0b10011, 0b11111) would return 0b010011101.
An expression in C that might do this exact example would be
(B2 & 4 << 5) | (B3 << 3) | (B0 & 16 << 2) | (B0 & 4 << 1) | (B0 & 1)
using naive bitmasks and bitshifts.
Question: Is there any way to simplify such an expression to minimize the number of operations that need to be carried out?
For example, I note that B3 is copied in its entirety to some of the bits of the output, so I put it in place using B3 << 3 instead of masking and shifting individual bits. The first thing I thought of were Karnaugh maps since they came in handy in simplifying Boolean expressions, but I realised that since I am extracting and placing individual bits in different parts of a byte there is no simplification possible using Boolean algebra.
Reasoning: The reason why I want to do this is to be able to light the LEDs in a programmer-friendly manner on the BBC micro:bit. I want B0 to B4 to represent which LEDs are on in the physical 5x5 arrangement, but electronically these LEDs are wired in a complex 3x9 configuration. More information on the LEDs can be found here.
Typically a pattern would be stored in memory according to the physical 3x9 arrangement so as to be able to output this pattern to the LEDs in a single instruction, but I want to be able to map a 5x5 pattern to the 3x9 pattern programmatically. However an expression as shown above would require 5 load instructions, 9 bitwise AND/OR operations and 4 logical shifts, which is at least 9 times more inefficient that the normal method.
First consider how much each bit needs to be shifted (rather than merely its final position). You can then execute the required shift amount with one command for multiple bits for those groups of input bits where the shift in the same. For example, (B3 & 31) << 3). You might also be able to eliminate the "masking" (done with the bitwise AND, &) if the masked out bits get shifted out.

pick pair of bit (0b11) in memory array

my embedded system has 128KB memory array structure for specific purpose
and each 2bit represents 4state( state 0 ,state 1, state 2, state 3)
I'd like to count total state 3 (0b11) in memory array
for example 0xFF001234 = 1111 1111 0000 0000 0001 0010 0011 0100
It counts 5 (0b11)
I searched algorithm but it only counts single bit
- https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
I hope to avoid greedy algorithm like compare 0b11 every 2bit
anyone has good idea?
ps : I'm using LEON3 Sparc V8 32bit processor, using C language
You have an array uint32_t states[] where each state[i] represents 16 states?
To count the number of 0b11 states in the variable uint32_t s you can use the following approach:
First, pre-process s such that every state 0b11 leads to exactly one 1 bit and all other states lead to 0 bits. Then count the numbers of 1 bits.
Pre-Processing
Split s into the left bits l and right bits r of each state.
s AB CD EF GH IJ LM NO PQ RS TU VW XY ZΓ ΔΠ ΦΨ ДЖ
l = s & 0xAAAAAAAA = A0 C0 E0 G0 I0 L0 N0 P0 R0 T0 V0 X0 Z0 Δ0 Φ0 Д0
r = s & 0x55555555 = 0B 0D 0F 0H 0J 0M 0O 0Q 0S 0U 0W 0Y 0Γ 0Π 0Ψ 0Ж
Then align the bits of l and r.
(l >>> 1) = 0A 0C 0E 0G 0I 0L 0N 0P 0R 0T 0V 0X 0Z 0Δ 0Φ 0Д
r = 0B 0D 0F 0H 0J 0M 0O 0Q 0S 0U 0W 0Y 0Γ 0Π 0Ψ 0Ж
Finally, use & to get a 1-bit if and only if the state was 0b11.
(l >>> 1) & r = 0? 0? 0? 0? 0? 0? 0? 0? 0? 0? 0? 0? 0? 0? 0? 0?
Here ? is 1 if the corresponding state was 0b11 and 0 otherwise.
The same result can be achived by the simplified formula (s >>> 1) & s & 0x55555555.
Bit-Counting
To count the 0b11 states in s we only have to count the 1-bits in
(s >>> 1) & s & 0x55555555.
Bit-counting can be done without a loop as explained in the book Hacker's Delight, chapter 5 or in this Stackoverflow answer.
The methods shown here only apply to a single array element. To count the states in your whole array loop over its elements.
Optimization
As pointed out by Lundin in the comments, the operation (s >>> 1) might me expensive if your processors cannot fit uint32_t into its registers. In this case it would be sensible to declare your array states[] not as uint32_t but whatever works best on your processor – the procedure stays the same, you only have to use more or less 555…. If, for some reason you cannot change the type of your array, you can still access it as if it had another type, see how to cast an int array to a byte array in C.

How to set last three bit of a byte efficiently?

I want to set the last three bit of a byte to a specific vaue. What’s the best way to archive this goal?
I can think of at least two solutions… Let’s assume I have the following Byte: 1101 0110 and want to set the three last bits to 011.
Solution 1:
1101 0110
&1111 1000 //and with mask to clear last three bits
|0000 0011 //add the target bits
Solution 2:
1101 0110 >> 3 //shift right to remove last three
0001 1010 << 3 //shift left to clear last three
|0000 0011 //add target bits
Is there a better/shorter/more efficient way?
The best way is to say
b = (b & ~7u) | 3
because 3=0...011 and 7u=0..111 in binary, and the complement of 7u is ~7u=11...1000, so the operation does what you want. It first clears the last three bits (by doing b & ~7u) and then sets the first and the second bits (by doing bitwise-OR with 3).
If the C source code has a >> in it, that does not mean the generated code will have shift instructions.
((x>>3)<<3) | 3 may generate the exact same code as (x & ~7) | 3. Compilers are very sophisticated in their optimization.
Use what is simplest #Martin James.
Recommend #blazs solution, as that is simple to understand and well copes with signed integer issues.
(x & ~7u) | 3
I would recommend you to do your second solution, sure it depends on your hardware architecture but almost always SHIFT operations are faster than ADD.

What does this condition written in bitwise operators really do?

What does the following condition effectively check in C :
if(a & (1<<b))
I have been wracking my brains but I can't find a pattern.
Any help?
Also I have seen this used a lot in competitive programming, could anyone explain when and why this is used?
It is checking whether the bth bit of a is set.
1<<b will shift over a single set bit b times so that only one bit in the bth position is set.
Then the & will perform a bitwise and. Since we already know the only bit that is set in 1<<b, either it is set in a, in which case we get 1<<b, or it isn't, in which case we get 0.
In mathematical terms, this condition verifies if a's binary representation contains 2b. In terms of bits, this checks if b's bit of a is set to 1 (the number of the least significant bit is zero).
Recall that shifting 1 to the left by b positions produces a mask consisting of all zeros and a single 1 in position b counting from the right. A value of this mask is 2b.
When you perform a bitwise "AND" with such a mask, the result would be non-zero if, and only if, a's binary representation contains 2b.
Lets say for example a = 12 (binary: 1100) and you want to check that the third bit (binaries are read from right to left) is set to 1, to do that you can use & bitwise operator which work as following:
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
1 & 1 = 1
To check if the third bit in a is set to 1 we can do:
1100
0100 &
------
0100 (4 in decimal) True
if a = 8 (binary: l000) on the other hand:
1000
0100 &
------
0000 (0 in decimal) False
Now to get the 0100 value we can right shift 1 by 2 (1 << 2) wich will append two zeros from the right and we'll get 100, in binaries left trailing zeros doesn't change the value so 100 is the same as 0100.

Converting 8 bits to a scaled 12 bits equivalent

I need to convert an 8 bit number (0 - 255 or #0 - #FF) to its 12 bit equivalent (0 - 4095 or #0 - #FFF)
I am not wanting to do just a straight conversion of the same number. I am wanting to represent the same scale, but in 12 bits.
For example:-
0xFF in 8 bits should convert to 0xFFF in 12 bits
0x0 in 8 bits should convert to 0x0 in 12 bits
0x7F in 8 bits should convert to 0x7FF in 12 bits
0x24 in 8 bit should convert to 0x249 in 12 bits
Are there any specific algorithms or techniques that I should be using?
I am coding in C
Try x << 4 | x >> 4.
This has been updated by the OP, changed from x << 4 + x >> 4
If you are able to go through a larger domain then this may help:
b = a * ((1 << 12) - 1) / ((1 << 8) - 1)
It is ugly but preserves scaling almost as requested. Of course you can put constants.
What about:
x = x ?((x + 1) << 4) - 1 :0
I use mathematical equation y=mx+c
Assuming low range of values is zero.
You can scale your data by a factor of m (Multiple for increasing range and divide for decreasing)
Ex.
My ADC data was 12 bit. Range in integer =0 to 4095
I want to shrink this data in range 0 to 255.
m=(y2-y1/x2-x1)
m=(4095-0/255-0)
m=16.05 = 16
So data received in 12 bits is divided by 16 to convert to 8 bits.
This conversion is linear in nature.
Hope this is also a good idea.
Image Link

Resources