|
30 | 30 | #include "wincrypt.h" |
31 | 31 | #endif |
32 | 32 |
|
| 33 | +#if defined __GNUC__ && ! defined __llvm__ |
| 34 | + #define GCC_VERSION (__GNUC__ * 10000 \ |
| 35 | + + __GNUC_MINOR__ * 100 \ |
| 36 | + + __GNUC_PATCHLEVEL__) |
| 37 | + #if GCC_VERSION < 40500 |
| 38 | + #include <tr1/random> |
| 39 | + #define IMPLEMENT_TR1 |
| 40 | + #define tr1ns std::tr1 |
| 41 | + #define uniform_real_distribution uniform_real |
| 42 | + #else |
| 43 | + #include <random> |
| 44 | + #define tr1ns std |
| 45 | + #endif |
| 46 | +#else |
| 47 | + #include <random> |
| 48 | + #define tr1ns std |
| 49 | +#endif |
| 50 | + |
33 | 51 | #define ARG(argname, argtype) get_arg<argtype>(argname, env, sig, pstate, backtrace) |
34 | 52 | #define ARGR(argname, argtype, lo, hi) get_arg_r(argname, env, sig, pstate, lo, hi, backtrace) |
35 | 53 | #define ARGM(argname, argtype, ctx) get_arg_m(argname, env, sig, pstate, backtrace, ctx) |
@@ -212,9 +230,43 @@ namespace Sass { |
212 | 230 | // random_device degrades sharply once the entropy pool |
213 | 231 | // is exhausted. For practical use, random_device is |
214 | 232 | // generally only used to seed a PRNG such as mt19937. |
215 | | - static std::mt19937 rand(static_cast<unsigned int>(GetSeed())); |
| 233 | + static tr1ns::mt19937 rand(static_cast<unsigned int>(GetSeed())); |
| 234 | + |
| 235 | + tr1ns::uniform_real_distribution<> std_dist(0, 1); |
| 236 | + #ifdef IMPLEMENT_TR1 |
| 237 | + tr1ns::variate_generator < |
| 238 | + tr1ns::mt19937, |
| 239 | + tr1ns::uniform_real_distribution <double> |
| 240 | + > gen_std_dist(rand, std_dist); |
| 241 | + #endif |
| 242 | + |
| 243 | + // Using ULONG_MAX here seems to fail on Mac OSX Clang!? |
| 244 | + tr1ns::uniform_real_distribution<> full_dist(0, 4294967296); |
| 245 | + #ifdef IMPLEMENT_TR1 |
| 246 | + tr1ns::variate_generator < |
| 247 | + tr1ns::mt19937, |
| 248 | + tr1ns::uniform_real_distribution <double> |
| 249 | + > gen_full_dist(rand, full_dist); |
| 250 | + #endif |
| 251 | + |
| 252 | + // helper function to retrieve a random number in interval |
| 253 | + // works around some compiler issues with older gcc versions |
| 254 | + static double random(double min, double max) |
| 255 | + { |
| 256 | + tr1ns::uniform_real_distribution<> distributor(min, max); |
| 257 | + #ifdef IMPLEMENT_TR1 |
| 258 | + tr1ns::variate_generator < |
| 259 | + tr1ns::mt19937, |
| 260 | + tr1ns::uniform_real_distribution <> |
| 261 | + > gen(rand, distributor); |
| 262 | + distributor(rand); |
| 263 | + return gen(); |
| 264 | + #else |
| 265 | + return distributor(rand); |
| 266 | + #endif |
| 267 | + } |
216 | 268 |
|
217 | | - // features |
| 269 | + // supported features lookup table |
218 | 270 | static std::set<std::string> features { |
219 | 271 | "global-variable-shadowing", |
220 | 272 | "extend-selector-pseudoclass", |
@@ -1199,13 +1251,19 @@ namespace Sass { |
1199 | 1251 | err << "Expected $limit to be an integer but got " << v << " for `random'"; |
1200 | 1252 | error(err.str(), pstate); |
1201 | 1253 | } |
1202 | | - std::uniform_real_distribution<> distributor(1, v + 1); |
1203 | | - uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand)); |
| 1254 | + // std::uniform_real_distribution<> distributor(1, v + 1); |
| 1255 | + // uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand)); |
| 1256 | + uint_fast32_t distributed = random(1, v + 1); |
1204 | 1257 | return SASS_MEMORY_NEW(Number, pstate, (double)distributed); |
1205 | 1258 | } |
1206 | 1259 | else if (b) { |
1207 | | - std::uniform_real_distribution<> distributor(0, 1); |
1208 | | - double distributed = static_cast<double>(distributor(rand)); |
| 1260 | + // std::uniform_real_distribution<> distributor(0, 1); |
| 1261 | + // double distributed = static_cast<double>(distributor(rand)); |
| 1262 | + #ifdef IMPLEMENT_TR1 |
| 1263 | + double distributed = gen_std_dist(); |
| 1264 | + #else |
| 1265 | + double distributed = std_dist(rand); |
| 1266 | + #endif |
1209 | 1267 | return SASS_MEMORY_NEW(Number, pstate, distributed); |
1210 | 1268 | } else if (v) { |
1211 | 1269 | throw Exception::InvalidArgumentType(pstate, "random", "$limit", "number", v); |
@@ -1989,8 +2047,11 @@ namespace Sass { |
1989 | 2047 | BUILT_IN(unique_id) |
1990 | 2048 | { |
1991 | 2049 | std::stringstream ss; |
1992 | | - std::uniform_real_distribution<> distributor(0, 4294967296); // 16^8 |
1993 | | - uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand)); |
| 2050 | + #ifdef IMPLEMENT_TR1 |
| 2051 | + uint_fast32_t distributed = gen_full_dist(); |
| 2052 | + #else |
| 2053 | + uint_fast32_t distributed = full_dist(rand); |
| 2054 | + #endif |
1994 | 2055 | ss << "u" << std::setfill('0') << std::setw(8) << std::hex << distributed; |
1995 | 2056 | return SASS_MEMORY_NEW(String_Quoted, pstate, ss.str()); |
1996 | 2057 | } |
|
0 commit comments