Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm building a ray tracer and in many cases I need to make additions and multiplications on three floats.
In such cases I've been doing it the naive way:
class Color{
float mR, mG, mB;
...
Color operator+(const Color &color) const
{
return Color(mR + color.mR,
mG + color.mG,
mB + color.mB);
}
Color operator*(const Color &color) const
{
return Color(mR * color.mR / COLOR_MAX,
mG * color.mG / COLOR_MAX,
mB * color.mB / COLOR_MAX);
}
}
This would also happen in equivalent classes such as Point or Vect3.
Then I heard about SIMD instructions and they look like a good fit for what I'm doing. So, of course, I googled it and found this piece of code:
typedef int v4sf __attribute__((mode(V4SF))); // Vector of three single floats
union f4vector
{
v4sf v;
float f[4];
};
Which first of all uses an extra four I don't need right now. But then gcc warns me that:
specifying vector types with __attribute__ ((mode)) is deprecated
I'd like to know how to do this in C++14 (if it even makes a difference at all) and I can't seem to find any other way of doing it.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I came Across a line of code in an example that I can not understand how is it being utilized and what benefit on using this type of declaration.
following in the part of code extracted from that example
const uint16_t cycles = 8;
typedef uint8_t invariant(value < cycles) TIndex_t;
struct TData
{
uint16_t readings[cycles];
volatile uint16_t sum;
TIndex_t index;
void addReading(uint16_t arg)
writes(*this; volatile)
pre(invar())
pre(arg <= 1023)
post(invar())
{
sum = sum - readings[index] + arg;
readings[index] = arg;
index = static_cast<TIndex_t>((index + 1u) % cycles);
}
void init()
writes(*this; volatile)
post(invar());
ghost(
bool invar() const
returns((forall r in readings :- r <= 1023) && sum == + over readings);
)
This is GNU C++ the struct is a kind of class definition as I can tell
This is not pure C++.
It looks like somebody has written either a compiler extension or a series of macros to allow the specification of pre- and post-conditions for a block of code.
You're going to have to ask that person.
(As an aside, this is why just "extracting code" is often insufficient for understanding; you need to observe the full context in order to know what's going on. In this case, the context includes definitions and/or documentation found elsewhere and not included in your question.)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
One purpose of left shift operator is multiply the left Operand with 2 and right shift for integer Division. Both have also constraints like undefined behaviours Link etc. I do not understand what will be the real use. I am even not confident to use them for Division or multiplication.
Only beginners would ever use a shift for division or multiplication (including people who are writing software for decades and are still beginners).
Shift operations are for shifting bits. When you want to shift bits you use them. If you don't know what shifting bits means, you don't want to use them.
They're bitwise operators, so they're used for bitwise operations. Here's a trivial example that shows some bitwise operations that call for bit shifting.
struct color {
unsigned char r;
unsigned char g;
unsigned char b;
};
void setColor(struct color*,int);
int getColor(struct color*);
int main() {
struct color myColor;
int color;
setColor(&myColor,0x00ff00);
color = getColor(&myColor);
return 0;
}
void setColor(struct color* color,int rgb) {
color->r = (rgb>>16)&0xff;
color->g = (rgb>>8)&0xff;
color->b = rgb&0xff;
}
int getColor(struct color* color) {
return color->r<<16|color->g<<8|color->b;
}
One real-world use case is in manipulating bitmasks. For example, if I have a bitmask x and I want to set the 7th bit from the right (where I start counting at zero), I could do the following.
x = x | (1 << 7);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello' I've got a problem
there is some telemetry values packed in words (or double words) and bitmask for each telemetry channel.
for example i've got mask 0xf000 and word value 0x2499
after & operation i've got 0x2000,but real telemetry value packed in this word is 0x2 (0x2000>>12). How can i determine this offset using only mask and without cycles like:
offset = 0;
for (int i = 0;i<32 i++)
{
if ((mask>>i)&1)
{
offset = i
break;
}
}
I think you may use some built in functions to get the first non-zero bit index.For example GCC has int __builtin_ffs (int x)
— Built-in Function: int __builtin_ffs (int x)
Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.
So in your case:
offset = __builtin_ffs(mask);
MSDN has similar intrinsics _BitScanForward and _BitScanForward64
https://msdn.microsoft.com/en-us/library/wfd9z0bb.aspx
All these builtin functions use special assembly instruction which performs the required calculation on the hardware level.
Maybe you can look at intrinsics to perform a "scan". The instruction "int _bit_scan_forward(int)" should do the trick ;)
https://software.intel.com/sites/landingpage/IntrinsicsGuide/#cats=Bit%20Manipulation
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is there any difference in memory usage / execution speed between e.g.
struct test
{
int a;
float b;
char c;
};
test ar[30];
and
int arr1[30];
float arr2[30];
char arr3[30];
? Lets pretend, that we are not talking about work comfort or programmer sided things, only speed of execution / memory usage.
In terms of memory usage definitely.
When you allocate test ar[30] you are actually allocating:
int - float - char - (padding) - int - float - char - ...
While in your second example you are allocating:
int - int - int - .... - float - float - ... - char - ...
So the layout in your memory is completely different, which will have an impact on your performance (depending on what you do OFC)
In term of execution performance (speed), there is a difference because of the CPU cache; even if you ask the compiler to optimize.
If all the members of a given structure are accessed nearly together, the locality is increased, and you get less cache misses.
In terms of memory size, the compiler may add padding to the struct to align memory, so it is possible that sizeof(test) > sizeof(arr1) + sizeof(arr2) + sizeof(arr3)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was studying C++ via cplusplus.com and came across something like 75u, which seems to describe an unsigned constant.
What has got me confused is: what's the point of declaring a constant to be unsigned when there is already a provision to declare the variable to which 75 will be assigned as unsigned?
Simpler said:
Why would you specifically add a u to a number when assigning it to (for example) an unsigned int?
What's the difference between
unsigned int i = 75;
and
unsigned int i = 75u;
That's because the type of the variable (in an assignment) on the left hand side of the = has nothing to do with how an expression is evaluated (the right hand side).
This seems to surprise many new programmers, but it's still true.
Something like this:
const float two_thirds = 2 / 3; /* Bad code! */
does not assign 0.6666667 to two_thirds; since both 2 and 3 are int literals, the expression is evaluated using integer math.
You need:
const float two_thirds = 2.f / 3;
to force the expression to float. Similar reasoning applies to the use of unsigned, since it has larger range than signed variables.