source: trunk/src/numeric.h @ 296

Last change on this file since 296 was 296, checked in by sam, 10 years ago

Fix Linux build.

File size: 1.2 KB
Line 
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://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11//
12// The Matrix classes
13// ------------------
14//
15
16#if !defined __DH_NUMERIC_H__
17#define __DH_NUMERIC_H__
18
19#include <cmath>
20#include <cstdlib>
21#include <stdint.h>
22
23/* Random float value */
24static inline float RandF()
25{
26    return (float)rand() / RAND_MAX;
27}
28
29static inline float RandF(float val)
30{
31    return RandF() * val;
32}
33
34static inline float RandF(float min, float max)
35{
36    return min + RandF() * (max - min);
37}
38
39/* Next power of two. */
40template <typename T> static inline T PotUp(T val)
41{
42    val = val - 1;
43    if (sizeof(val) > 4) val = val | ((uint64_t)val >> 32);
44    if (sizeof(val) > 2) val = val | ((uint64_t)val >> 16);
45    if (sizeof(val) > 1) val = val | ((uint64_t)val >> 8);
46    val = val | ((uint64_t)val >> 4);
47    val = val | ((uint64_t)val >> 2);
48    val = val | ((uint64_t)val >> 1);
49    return val + 1;
50}
51
52#endif // __DH_NUMERIC_H__
53
Note: See TracBrowser for help on using the repository browser.