1 | // |
---|

2 | // Lol Engine |
---|

3 | // |
---|

4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|

5 | // This program is free software; you can redistribute it and/or |
---|

6 | // modify it under the terms of the Do What The Fuck You Want To |
---|

7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|

8 | // http://www.wtfpl.net/ for more details. |
---|

9 | // |
---|

10 | |
---|

11 | // |
---|

12 | // The Matrix classes |
---|

13 | // ------------------ |
---|

14 | // |
---|

15 | |
---|

16 | #if !defined __LOL_NUMERIC_H__ |
---|

17 | #define __LOL_NUMERIC_H__ |
---|

18 | |
---|

19 | #include <cstdlib> |
---|

20 | #include <stdint.h> |
---|

21 | |
---|

22 | namespace lol |
---|

23 | { |
---|

24 | |
---|

25 | /* Random float value */ |
---|

26 | static inline float RandF() |
---|

27 | { |
---|

28 | using namespace std; |
---|

29 | return (float)rand() / RAND_MAX; |
---|

30 | } |
---|

31 | |
---|

32 | static inline float RandF(float val) |
---|

33 | { |
---|

34 | return RandF() * val; |
---|

35 | } |
---|

36 | |
---|

37 | static inline float RandF(float min, float max) |
---|

38 | { |
---|

39 | return min + RandF() * (max - min); |
---|

40 | } |
---|

41 | |
---|

42 | /* Next power of two. */ |
---|

43 | template <typename T> static inline T PotUp(T val) |
---|

44 | { |
---|

45 | val = val - 1; |
---|

46 | if (sizeof(val) > 4) val = val | ((uint64_t)val >> 32); |
---|

47 | if (sizeof(val) > 2) val = val | ((uint64_t)val >> 16); |
---|

48 | if (sizeof(val) > 1) val = val | ((uint64_t)val >> 8); |
---|

49 | val = val | ((uint64_t)val >> 4); |
---|

50 | val = val | ((uint64_t)val >> 2); |
---|

51 | val = val | ((uint64_t)val >> 1); |
---|

52 | return val + 1; |
---|

53 | } |
---|

54 | |
---|

55 | //Damp for float |
---|

56 | template <typename T1, typename T2, typename Tf> static inline T1 damp(const T1 &a, const T2 &b, const Tf &x, const Tf &dt) |
---|

57 | { |
---|

58 | if (dt <= .0f) |
---|

59 | return a; |
---|

60 | return lol::lerp(a, b, dt / (dt + x)); |
---|

61 | } |
---|

62 | |
---|

63 | } /* namespace lol */ |
---|

64 | |
---|

65 | #endif // __LOL_NUMERIC_H__ |
---|

66 | |
---|