Rotating bits in RISC-V - bit-manipulation

Hey so I am kinda new here and to RISC-V.
One of my exercise questions is:
Rotate right by 4 bits the value of 0x0000000000000123. The expected result is 0x3000000000000012, i.e. all hexadecimal digits move right by one position while the rightmost one moves to the front*
So far, I learned a little bit about the Logical Operations: andi, or, and xori. In my previous exercises I learned add, addi, sub, slli, srli, srai.
I started off with:
addi x6, 0x, 0x123
However, I am stuck here. My textbook doesn't really describe things properly so any assistance is much appreciated!

Here's what I ended up doing:
addi x30, x0, 0x123
andi x29, x30, 0x00f //masking all, but the right most hexadecimal digit
slli x6, x29, 60 //shifting this digit all the way to the left (rotating)
srli x7, x30, 4 //shifting the original value 4 bits right
add x5, x6, x7 //adding the two regs

Related

How to select level of detalization of point cloud(xyz points)

What opengl parameters i must use for LOD selection?
I use glortho
glOrtho(boxC.x-maxLen/1 - dL, boxC.x + maxLen/1 + dL, boxC.y-maxLen/1 - dL, boxC.y + maxLen/1 + dL, -(f_scale)*max_z, f_scale*max_z);
You can't. OpenGL has no feature called LOD.
If you want to render less points when the camera is a certain distance away, it is up to your program to do that. Unless you just want to ignore points that are farther than a certain distance away - the far clipping plane (the last parameter to glOrtho) controls the maximum distance at which things can be displayed.

LabVIEW Complicated If Statements

Background: I am trying to configure a DMX turntable in LabVIEW, it has two settings for rotating: coarse (360 degrees in 255 points) and fine (1 degree in 255 points). I need to be able to firstly execute a command to move to the closest available DMX position in coarse mode, then make up the difference in fine mode.
e.g. I want to turn to 90 degrees, this is equivalent to a DMX value of 63.75 however this is rounded down to 63. The real value in degrees is now 88.94 so I need to make up the extra 1.06 degrees by using the fine setting (I can only make up 1 degree but 89.94 is close enough to 90).
I can execute the coarse setting just fine however I need some kind of "if" statement to say "if real degree value is less than input value, make up the difference". Case Structures do not provide enough control to use this complicated "if" statement, what can I use instead?
255 coarse steps * 255 fine steps per coarse step = 65025 possible steps.
360 degrees / 65025 = ~ 0.00536 degrees per step.
Divide your desired angle by this constant, then use this as the X input to quotient and remainder. Y would be 255. The Quotient will represent the coarse value to adjust and the Remainder represents the fine value.
63 coarse steps and 191 fine steps.
You don't need any condition. Use the Quotient and Remainder function with 255/4 to get 63 and .75. Do the 63 coarse movement, then take the .75 and multiply it by 360. This will tell you many fine steps you need to take (270, which is 255 + 15. You can use Q&R again to know how many whole turns to make and how much you have left in the last turn).

C++ Unicode for Color16 Values

This has been one huge headache. Ive googled everything and found very little, and have little knowledge on unicode, learned a bit from the searching. What I am needing is really simple, right? A struct I am using requires COLOR16.
So I know 0x0000 and 0x00FF is 0 to 255, which for COLOR16 is useless.
The four zeros each can represent 0 to 15 Ive seen.
I know COLOR16 represents all 16^4 colors.
But I cannot for the life of me figure out how to convert say, (R:100; G:35; B:42) to a unicode value.
I could really use some info on this, or a tutorial or anything.
Thanks.
I know what you're looking for. You're just asking the wrong way. You mean a short value, not a Unicode value. The common name for this is RGB565. That means 5-bits for red, 6 for green and 5 for blue.
That adds up to 16 bits. You pack the bits in like this:
unsigned short val = ((r<<8) & 0xf800) | ((g<<3) & 0x07e0) | (b>>3);
The bits are like this:
R 00000000 12345xxx -> 12345000 00000000 (shift left by 8 and masked)
G 00000000 123456xx -> 00000123 45600000 (shift left by 3 and masked)
B 00000000 12345xxx -> 00000000 00012345 (shift right by 3, no mask required)
Obviously information is lost in this process. You are just taking the most significant bits of the colour and using that. It's like lossy compression, but pretty good for video when you don't notice the loss of colour definition as much. The reason green gets the extra bit is because human eyes are more sensitive to colours in that spectrum.
Finally found a random example that had the solution in it. This takes a COLORREF and extracts 16bit colors for the TRIVERTEX struct:
vertex[1].Red = GetRValue(clrStart)<<8;
vertex[1].Green = GetGValue(clrStart)<<8;
vertex[1].Blue = GetBValue(clrStart)<<8;
It is misleading in the Windows API if that is what you are asking, RGBA all "COLOR16" references should be stored as byte [0-255] for each channel in the TRIVERTEX structure. though I have seen most use short values which are 8 bits
Red [0-255]; <= byte
Green [0-255]; <= byte
Blue [0-255]; <= byte
Alpha [0-255]; <= byte
Microsoft: what can you say

My neural net learns sin x but not cos x

I have build my own neural net and I have a weird problem with it.
The net is quite a simple feed-forward 1-N-1 net with back propagation learning. Sigmoid is used as activation function.
My training set is generated with random values between [-PI, PI] and their [0,1]-scaled sine values (This is because the "Sigmoid-net" produces only values between [0,1] and unscaled sine -function produces values between [-1,1]).
With that training-set, and the net set to 1-10-1 with learning rate of 0.5, everything works great and the net learns sin-function as it should. BUT.. if I do everything exately the same way for COSINE -function, the net won't learn it. Not with any setup of hidden layer size or learning rate.
Any ideas? Am I missing something?
EDIT: My problem seems to be similar than can be seen with this applet. It won't seem to learn sine-function unless something "easier" is taught for the weights first (like 1400 cycles of quadratic function). All the other settings in the applet can be left as they initially are. So in the case of sine or cosine it seems that the weights need some boosting to atleast partially right direction before a solution can be found. Why is this?
I'm struggling to see how this could work.
You have, as far as I can see, 1 input, N nodes in 1 layer, then 1 output. So there is no difference between any of the nodes in the hidden layer of the net. Suppose you have an input x, and a set of weights wi. Then the output node y will have the value:
y = Σi w_i x
= x . Σi w_i
So this is always linear.
In order for the nodes to be able to learn differently, they must be wired differently and/or have access to different inputs. So you could supply inputs of the value, the square root of the value (giving some effect of scale), etc and wire different hidden layer nodes to different inputs, and I suspect you'll need at least one more hidden layer anyway.
The neural net is not magic. It produces a set of specific weights for a weighted sum. Since you can derive a set weights to approximate a sine or cosine function, that must inform your idea of what inputs the neural net will need in order to have some chance of succeeding.
An explicit example: the Taylor series of the exponential function is:
exp(x) = 1 + x/1! + x^2/2! + x^3/3! + x^4/4! ...
So if you supplied 6 input notes with 1, x1, x2 etc, then a neural net that just received each input to one corresponding node, and multiplied it by its weight then fed all those outputs to the output node would be capable of the 6-term taylor expansion of the exponential:
in hid out
1 ---- h0 -\
x -- h1 --\
x^2 -- h2 ---\
x^3 -- h3 ----- y
x^4 -- h4 ---/
x^5 -- h5 --/
Not much of a neural net, but you get the point.
Further down the wikipedia page on Taylor series, there are expansions for sin and cos, which are given in terms of odd powers of x and even powers of x respectively (think about it, sin is odd, cos is even, and yes it is that straightforward), so if you supply all the powers of x I would guess that the sin and cos versions will look pretty similar with alternating zero weights. (sin: 0, 1, 0, -1/6..., cos: 1, 0, -1/2...)
I think you can always compute sine and then compute cosine externally. I think your concern here is why the neural net is not learning the cosine function when it can learn the sine function. Assuming that this artifact if not because of your code; I would suggest the following:
It definitely looks like an error in the learning algorithm. Could be because of your starting point. Try starting with weights that gives the correct result for the first input and then march forward.
Check if there is heavy bias in your learning - more +ve than -ve
Since cosine can be computed by sine 90 minus angle, you could find the weights and then recompute the weights in 1 step for cosine.

Move in direction of rotation in opengl

I'm really new to OpenGL but have quite a good grasp of basic trigonometry (forgotten quite a bit since school!) but I'm having trouble with this.
I have a character that moves forwards and backwards on the Z axis. To move left and right, rather than strafing I want them to rotate (when pressing left and right arrow keys respectively) and then when they press forwards/backwards again they move in the direction they're facing.
So what I did was have the left/right functions add/subtract small amounts onto an angle variable that is used to draw the character's rotation. Then the forward/backward functions add/subtract small amounts to the x and z axes variables. They are as follows (for the backwards):
z -= 0.005f * Math.cos(heading);
x -= 0.005f * Math.sin(heading)
The heading variable is the angle which is manipulated by the left and right arrow keys.
I thought this would work because when the player is going straight forward the heading is 0 and so cos(0) = 1 and sin(0) = 0 which means they move nowhere on the X but forwards the full 0.005 amount on the Z. I guess my rudimentary trigonometry knowledge wasn't completely sound because if I turn a bit then move forwards they go in that direction but if I move a bit more then move forwards they go in that same line if it was rotated 90 degrees, and continues then again as if it was 180 degrees then 270 degrees etc.
EDIT: I'll try and explain that better, basically if I press forwards after turning to the left it will go in the direction I want, but if I let go of forward, turn a bit more and then press forward again, the angle has increased as it should have but the direction is like 90 degrees around from the direction it should be going. Sorry I can't really explain this well.
EDIT: Okay, I'm getting some weird problems that I think might be causing the strange "90 degrees" problem, When I get the character to look 90 degrees (through hard-coding heading=90) left/right, cos(heading) should be 0 right? But for some reason it is coming out as -0.44, and If I cos-1(-0.44) I get 116.1, is it something to do with Math.cos() wanting the angle in radians or something? I'm completely lost here.
Is this the right way of going about this problem? I'm completely stuck just trial and erroring around with minus signs...
Any reply is appreciated,
Thanks
InfinitiFizz
(Also, I know I should use a deltaTime for the speed/rotation values of the character not a hard-coded 0.005f but I want to get this problem out of the way first before I sort that).
It should be * not +
based on:
x = r cos (theta)
y = r sin (theta)
you will want:
z -= 0.005f * Math.cos(heading);
x -= 0.005f * Math.sin(heading);
If that's what you're after? I must admit, I didn't fully understand your description of what actually happens.
EDIT: you'll probably want to use a larger "radius" than 0.005, depending on how your coordinate system is working.