Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

840 Good

About DevFred

  • Rank
  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. DevFred

    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. DevFred

    #define vs const

    The constant will only be placed into memory if you take its address or pass it by reference. For example: #include <stdio.h> const int answer = 42; int main() { printf("%d\n", answer); } turns into: .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 Note how the constant is placed directly into the code, and the variable answer got optimized away.
  6. 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. DevFred

    C++ header only style

    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. DevFred

    C++ or another language

    1. What's difference between these two lines? Foo* p = new Foo; Foo* q = new Foo(); 2. What's the difference between these five lines? Foo a; Foo b(); Foo c = Foo(); Foo d(Foo()); Foo e((Foo())); 3. Why does the following popular include guard naming pattern invoke undefined behavior? #ifndef __MY_HEADER_GUARD__ #define __MY_HEADER_GUARD__ // ... #endif 4. In the following example, why is the first swap well-defined, but the second swap isn't? namespace std { template<> void swap(Foo& a, Foo& n); template<typename T> void swap(Bar<T>& a, Bar<T>& b); } 5. Why can I pass an lvalue to foo, but not to bar? template<typename T> void foo(T&& x); void bar(Bar&& x);
  10. There is no language called "C/C++", and C certainly does not have classes.
  11. That is a common misbelief. Here is how you allocate a 3D array dynamically: int (*p)[8][9] = new int[7][8][9]; p[0][0][0] = 42; // ... delete[] p; See my FAQ on using arrays in C++
  12. DevFred

    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. DevFred

    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. DevFred

    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
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!