Using __builtin_ia32_shufps to shift a vector by 32 bits? - c

This is the prototype for the function:
v4si __builtin_ia32_shufps (v4si, v4si, int)
On some websites I found they had but hex in the int field, and it looked liked it separated high and low bits, but what I want is a logical 32 bit shift.
X3 X2 X1 X0 shifted by 32 bits to get X2 X1 X0 0
Another example using 2 v4si vectors:
X7 X6 X5 X4 | X3 X2 X1 X0, where each X is a 32 bit and what I want for a shift is the
same a logical shift, but with each vector element. So:
X7 X6 X5 X4 | X3 X2 X1 X0 << 2 = X5 X4 X3 X2 | X1 X0 0 0
Is shufps the right command to do this?

Looking at your example with two vectors I think what you're probably looking for is _mm_alignr_epi8 (PALIGNR). This works for any shift of a pair of vectors by an arbitrary number of bytes, so you would need to multiply the shift parameter by sizeof(int), e.g.
v = _mm_alignr_epi8(v0, v1, 2 * sizeof(int));
Note that this instruction is only available in SSSE3 and later, which means pretty much any Intel CPU since ~ 2005.

Related

Get first smaller timestamp in SQL Server query

I have one table from this type:
Equipment TimestampEqmt
truck1 timetruck1
truck2 timetruck2
truck3 timetruck3
truck1 timetruck4
truck2 timetruck5
truck3 timetruck6
and another with GPS coordinates for the trucks
Equipment TimestamGPS X Y Z
truck1 TimestamGPS1 X1 Y1 Z1
truck2 TimestamGPS2 X2 Y2 Z2
truck3 TimestamGPS3 X3 Y3 Z3
truck1 TimestamGPS4 X4 Y4 Z4
truck2 TimestamGPS5 X5 Y5 Z5
truck3 TimestamGPS6 X6 Y6 Z6
truck1 TimestamGPS1 X7 Y7 Z7
truck2 TimestamGPS2 X8 Y8 Z8
truck3 TimestamGPS3 X9 Y9 Z9
truck1 TimestamGPS1 X10 Y10 Z10
truck2 TimestamGPS2 X11 Y11 Z11
truck3 TimestamGPS3 X12 Y12 Z12
How to get for every TimestampEqmt and Equipment from first table, first GPS coordinates where TimestamGPS < TimestampEqmt?
The result should be like this:
Equipment TimestampEqmt TimestamGPS X Y Z
truck1 timetruck1 TimestamGPS7 X7 Y7 Z7
truck2 timetruck2 TimestamGPS11 X11 Y11 Z11
truck3 timetruck3 TimestamGPS12 X12 Y12 Z12
Which means that TimestamGPS7 is the first smaller timestamp depending on timetruck1 for truck1.

Rearranging an array

I have a row array as shown below:
X1 Y1 Z1 X2 Y2 Z2 X3 Y3 Z3 X4 Y4 Z4 ..... Xn Yn Zn
This row array always starts with the value of X and with the value of Z. I want to rearrange them as a multidimensional array:
X1 X2 X3 X4 X5 .... Xn
Y1 Y2 Y3 Y4 Y5 .... Yn
Z1 Z2 Z3 Z4 Z5 .... Zn
The size of the original array varies in each case. Any help to how to approach this problem would be helpful.
Use reshape function setting second dimension as []
reshaped_array = reshape(myarray,3,[]);

Move Columnar Data to Correlation Matrix

I was looking for a solution (avoiding vba) to reorganize 3 columns representing data1, data2, and f(data1,data2) into a 2d array.
The data is as follows:
X1 X2 Z_1_2
X1 X3 Z_1_3
X1 X4 Z_1_4
X1 X5 Z_1_5
X2 X3 Z_2_3
X2 X4 Z_2_4
X2 X5 Z_2_5
X3 X4 Z_3_4
X3 X5 Z_3_5
X4 X5 Z_4_5
Edit: I will give some more details...
X are strings and Z are numerics.
The results should be as following:
X1 X2 X3 X4 X5
X1 1 Z_1_2 Z_1_3 Z_1_4 Z_1_5
X2 Z_1_2 1 Z_2_3 Z_2_4 Z_2_5
X3 Z_1_3 Z_2_3 1 Z_3_4 Z_3_5
X4 Z_1_4 Z_2_4 Z_3_4 1 Z_4_5
X5 Z_1_5 Z_2_5 Z_3_5 Z_4_5 1
Thanks !!!
So you need to set up the headers, correctly both the row and column headers.
Then if the Upper left cell put this formula:
=IFERROR(INDEX($C:$C,MATCH($G2&"|"&H$1,$A:$A&"|"&$B:$B,0)),IFERROR(INDEX($C:$C,MATCH(H$1&"|"&$G2,$A:$A&"|"&$B:$B,0)),1))
This is an array formula and needs to be confirmed with Ctrl-Shift-Enter instead of just enter. Copy and fill the rest of the array.
One warning this will take time to compute, It can be sped up by limting the full column references to only those with data; $C:$C in this instance would be $C$1:$C$10.
something like that?
=IF(F$1=$E2,1,INDEX($C$1:$C$10,IFERROR(MATCH(F$1&"_"&$E2,$A$1:$A$10&"_"&$B$1:$B$10,),MATCH($E2&"_"&F$1,$A$1:$A$10&"_"&$B$1:$B$10,))))
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
if you get worried by the & you can avoid it like this:
=IF(F$1=$E2,1,INDEX($C$1:$C$10,MAX(ROW($1:$10)*((F$1=$A$1:$A$10)*($E2=$B$1:$B$10)+($E2=$A$1:$A$10)*(F$1=$B$1:$B$10)))))
This is an array formula and must be confirmed with Ctrl+Shift+Enter.

single line from multidimensional array, using awk

I have a file which contains:
x1 x2 x3 x4
x5 x6 x7 x8
x9
Which I want:
x1
x2
x3
.
.
How you do that using awk or in another ways?
With awk:
$ awk '1' RS=' ' ORS='\n' file
x1
x2
x3
x4
x5
x6
x7
x8
x9
However personally I would do:
$ xargs -n1 < file
x1
x2
x3
x4
x5
x6
x7
x8
x9
or:
$ grep -o '\S*' file
x1
x2
x3
x4
x5
x6
x7
x8
x9

How do you compute the XOR Remainder used in CRC?

I'm trying to remember how the math is worked out to compute the remainder of an XOR algorithm in Cyclical Redundancy Checks to verify the remainder bits of a network message.
I shouldn't have tossed that text book.
This is easily done in code, but how is it worked out by hand?
I know it looks something like a standard division algorithm, but I can't remember where to go from there to get the remainder.
___________
1010 | 101101000
Note: I did google it, but wasn't able to find a place where they mapped the steps in figuring the remainder.
1010 | 101101000
1010
0001 this result is 1011 XOR 1010 = 0001
1010
1010
0000 thus no remainder.
Thus 101101000 is perfect and no error has occurred in transmission/reception
In my experience it's easier to convert it to a polynomial when calculating by hand, especially when there're a lot of zeroes.
1010 = 1*x^3 + 0*x^2 + 1*x^1 + 0*x^0 = x^3 + x = x3 + x
101101000 = x8 + x6 + x5 + x3
-------------------
x3 + x ) x8 + x6 + x5 + x3
Then you divide the largest term in the dividend (x^8) with the first term in the divisor (x^3), resulting in x^5. You put that number on top and then multiply it with each term in the divisor. This yields the following for the first iteration:
x5
-------------------
x3 + x ) x8 + x6 + x5 + x3
x8 + x6
Doing XOR for each term then yields the new dividend: x5 + x3:
x5
-------------------
x3 + x ) x8 + x6 + x5 + x3
x8 + x6
-------------------
x5 + x3
Follow the same pattern until the dividend's largest term is smaller then the divisor's largest term. After the calculations are complete it will look like this:
x5 + x2
-------------------
x3 + x ) x8 + x6 + x5 + x3
x8 + x6
-------------------
x5 + x3
x5 + x3
-------------------
0
The reminder in this case is 0, which would indicate that most likely no errors has occurred during the transmission.
Note: I've shortened x^y as xy in the example above to reduce the clutter in the answer, since SO doesn't support math equation formatting.
Note2: Adding/subtracting a multiple of the divisor from the dividend will also give the reminder 0, since (P(x) + a*C(x)) / C(x) = P(x)/C(x) + a*C(x)/C(x) gives the same reminder as P(x)/C(x) since the reminder of a*C(x)/C(x) is 0.
It is long division by binary 11. There is an example on Wikipedia.
Let's assume that we want to divide 101110000 to 1001.
101110000
1001
--------- XOR the 1011 and 1001
0010
Now we will remove the zeros at the beginning of our XOR result which is 0010 and slide numbers from the top.
101110000
1001
---------
1010
Continue the XOR 1001 with the result.
101110000
1001
---------
1010
1001
---------
0011
--------- Remove zeros at the beginning
1100
1001
---------
0101
--------- Remove zeros at the beginning
1010
1001
---------
0011
Answer is 0011.

Resources