Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add missing source files, added in 1.42

  • Loading branch information...
commit 3bb730874f76b43827e579a9c97150fd3468d54d 1 parent 0dbd966
@jkivilin authored
Showing with 107 additions and 0 deletions.
  1. +40 −0 new_baseclass.h
  2. +67 −0 random_num.cpp
View
40 new_baseclass.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2004-2006 Jussi Kivilinna
+ */
+#ifndef NEW_BASECLASS_H
+#define NEW_BASECLASS_H
+
+#include <malloc.h>
+
+//new/delete operators with malloc/free to remove need for libstdc++
+
+class class_new_baseclass {
+public:
+ // Construction
+ class_new_baseclass(void) { };
+
+ // Operators
+ inline void * operator new(size_t size) {
+ if(size==0)
+ return(calloc(1, 1));
+ return(calloc(1, size));
+ }
+
+ inline void * operator new[](size_t size) {
+ if(size==0)
+ return(calloc(1, 1));
+ return(calloc(1, size));
+ }
+
+ inline void operator delete(void *ptr) {
+ if(ptr)
+ free(ptr);
+ }
+
+ inline void operator delete[](void *ptr) {
+ if(ptr)
+ free(ptr);
+ }
+};
+
+#endif /*NEW_BASECLASS_H*/
View
67 random_num.cpp
@@ -0,0 +1,67 @@
+// Manual branch optimization for GCC 3.0.0 and newer
+#if !defined(__GNUC__) || __GNUC__ < 3
+ #define likely(x) (x)
+ #define unlikely(x) (x)
+#else
+ #define likely(x) __builtin_expect((long int)!!(x), true)
+ #define unlikely(x) __builtin_expect((long int)!!(x), false)
+#endif
+
+static unsigned int rnd_idnum[2] = {1, 1};
+
+/* generates a random 32bit integer */
+static unsigned int fast_generate_random(void)
+{
+ rnd_idnum[0] ^= rnd_idnum[1] << 5;
+
+ rnd_idnum[0] *= 1664525L;
+ rnd_idnum[0] += 1013904223L;
+
+ rnd_idnum[1] *= 1664525L;
+ rnd_idnum[1] += 1013904223L;
+
+ rnd_idnum[1] ^= rnd_idnum[0] << 3;
+
+ return rnd_idnum[0];
+}
+
+
+void fast_random_seed(unsigned int seed)
+{
+ rnd_idnum[0] = seed;
+ rnd_idnum[1] = ~(seed + 6);
+ rnd_idnum[1] = fast_generate_random();
+}
+
+
+/* supports range INT_MIN, INT_MAX */
+int RANDOM_LONG2(int lLow, int lHigh)
+{
+ const double c_divider = ((unsigned long long)1) << 32; // div by (1<<32)
+ double rnd;
+
+ if(unlikely(lLow >= lHigh))
+ return(lLow);
+
+ rnd = fast_generate_random();
+ rnd *= (double)lHigh - (double)lLow + 1.0;
+ rnd /= c_divider; // div by (1<<32)
+
+ return (int)(rnd + (double)lLow);
+}
+
+
+float RANDOM_FLOAT2(float flLow, float flHigh)
+{
+ const double c_divider = (((unsigned long long)1) << 32) - 1; // div by (1<<32)-1
+ double rnd;
+
+ if(unlikely(flLow >= flHigh))
+ return(flLow);
+
+ rnd = fast_generate_random();
+ rnd *= (double)flHigh - (double)flLow;
+ rnd /= c_divider; // div by (1<<32)-1
+
+ return (float)(rnd + (double)flLow);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.