DevFred

Members
  • Content count

    2418
  • Joined

  • Last visited

Community Reputation

840 Good

About DevFred

  • Rank
    Contributor
  1. Just for completeness, here is an O(n) recursive solution: int fib(int n, int a, int b) { return (n <= 0) ? a : fib(n - 1, b, a + b); } int fib(int n) { return fib(n, 0, 1); } It is possible to convert the recursive solution into an iterative one: int fib(int n) { int a = 0; int b = 1; while (n --> 0) { int s = a + b; a = b; b = s; } return a; } But I find the recursive solution aesthetically more pleasing :)
  2. SSE has built-in instructions for determining the minimum and maximum, which allows you to get rid of jumps entirely: #include <x86intrin.h> // returns the minimum in *pa and the maximum in *pb void minmax(int* pa, int* pb, int* pc) { __m128 a = _mm_cvtsi32_ss(a, *pa); __m128 b = _mm_cvtsi32_ss(b, *pb); __m128 c = _mm_cvtsi32_ss(c, *pc); __m128 minimum = _mm_min_ss(_mm_min_ss(a, b), c); __m128 maximum = _mm_max_ss(_mm_max_ss(a, b), c); *pa = _mm_cvttss_si32(minimum); *pb = _mm_cvttss_si32(maximum); } Note that due to the conversion from int to float, only numbers up to 16777216 will be treated correctly.   If your processor supports SSE4, which is roughly six years old, you can use the full int range: void minmax(int* pa, int* pb, int* pc) { __m128i a = _mm_cvtsi32_si128(*pa); __m128i b = _mm_cvtsi32_si128(*pb); __m128i c = _mm_cvtsi32_si128(*pc); __m128i minimum = _mm_min_epi32(_mm_min_epi32(a, b), c); __m128i maximum = _mm_max_epi32(_mm_max_epi32(a, b), c); *pa = _mm_cvtsi128_si32(minimum); *pb = _mm_cvtsi128_si32(maximum); } On g++, you need to use -msse4 to enable SSE4.
  3. That won't help at all, because auto mi=0; means int i=0; because 0 is of type int.
  4. Is this bad?

    If you want to round an int down to a multiple of 64, you can use bitmasking:   x = x & ~63;   Note that this only works for powers of two.
  5. #define vs const

    [quote name='3333360' timestamp='1339221313' post='4947577'] However it uses memory to declare this variable, and it works like any other variable (if somewhere in your program there's a reference to this const variable, the program will fetch it's value from memory. [/quote] The constant will only be placed into memory if you take its address or pass it by reference. For example: [code] #include <stdio.h> const int answer = 42; int main() { printf("%d\n", answer); } [/code] turns into: [code] .file "floating.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "%d\n" .text .globl main .type main, @function main: .LFB31: .cfi_startproc subq $8, %rsp .cfi_def_cfa_offset 16 movl $42, %edx movl $.LC0, %esi movl $1, %edi movl $0, %eax call __printf_chk movl $0, %eax addq $8, %rsp .cfi_def_cfa_offset 8 ret .cfi_endproc .LFE31: .size main, .-main .ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3" .section .note.GNU-stack,"",@progbits [/code] Note how the constant is placed directly into the code, and the variable answer got optimized away.
  6. [quote name='Red Ant' timestamp='1324419662' post='4895906'] I'm aware of that, but I've made a habit of doing it just because it makes it impossible to write silly stuff like is_NaN( 6.3433 ) = false; [/quote] That assignment is always impossible, no matter if you return a bool or a const bool. Again, the const is simply ignored here, it doesn't make any difference whatsoever. The language simply does not allow assigning to scalar rvalues.
  7. C++ header only style

    [quote name='freeworld' timestamp='1323125323' post='4890877'] why not just switch to a language like C# then? Seems like a lot of work just to do something that is already common place in another almost identical language. [/quote] In what universe is C# "almost identical" to C++?
  8. Since you're using Actor as a polymorphic base class, it needs a virtual destructor. Deleting an Asteroid object through an Actor pointer without a virtual destructor yields undefined behavior.
  9. C++ or another language

    [quote name='Hannesnisula' timestamp='1313501761' post='4849837'] I'm kind of curious about what's not straight forward, mind giving me some examples? [/quote] 1. What's difference between these two lines? [code] Foo* p = new Foo; Foo* q = new Foo(); [/code] 2. What's the difference between these five lines? [code] Foo a; Foo b(); Foo c = Foo(); Foo d(Foo()); Foo e((Foo())); [/code] 3. Why does the following popular include guard naming pattern invoke undefined behavior? [code] #ifndef __MY_HEADER_GUARD__ #define __MY_HEADER_GUARD__ // ... #endif [/code] 4. In the following example, why is the first swap well-defined, but the second swap isn't? [code] namespace std { template<> void swap(Foo& a, Foo& n); template<typename T> void swap(Bar<T>& a, Bar<T>& b); } [/code] 5. Why can I pass an lvalue to foo, but not to bar? [code] template<typename T> void foo(T&& x); void bar(Bar&& x); [/code]
  10. [quote name='tom_mai78101' timestamp='1304050013' post='4804279'] My preferred languages are C/C++ and Java. Since they both use classes [/quote] There is no language called "C/C++", and C certainly does not have classes.
  11. [quote name='taz0010' timestamp='1295515720' post='4761748'] Only static multidimensional arrays are supported. [/quote] That is a common misbelief. Here is how you allocate a 3D array dynamically: [code] int (*p)[8][9] = new int[7][8][9]; p[0][0][0] = 42; // ... delete[] p; [/code] See my [url="http://stackoverflow.com/questions/4810664/"]FAQ on using arrays in C++[/url]
  12. Problems with Perlin Noise

    1 / 2^(OCTAVES - i) This does not do what you think it does. ^ is the bitwise exclusive-or operator. It has nothing to do with exponentiation, what you appear to want. In that case: 1.0 / (1<<(OCTAVES - i))
  13. Constant array declaration

    In the olden days, processors were slow, and memory was (comparatively) fast. Nowadays, memory is the bottleneck, so lookup tables simply are not a good idea anymore. In fact, memory is so hideously slow these days we need several layers of caches. If you buy a $100 processor, $99 are spent on the clever cache parts, and only $1 on the actual computing part. Herb Sutter's video Machine Architecture: Things Your Programming Language Never Told You has all the details.
  14. C++'s floats not very good?

    Quote:Original post by Evil Steve Write down 1 divided by 3 exactly, as a single real number. That's easy in ternary: 0.1