/[jscoverage]/trunk/js/jsmath.cpp
ViewVC logotype

Diff of /trunk/js/jsmath.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 584 by siliconforks, Sun Jan 10 07:23:34 2010 UTC revision 585 by siliconforks, Sun Sep 12 15:13:23 2010 UTC
# Line 436  Line 436 
436   * Math.random() support, lifted from java.util.Random.java.   * Math.random() support, lifted from java.util.Random.java.
437   */   */
438  static inline void  static inline void
439  random_setSeed(JSThreadData *data, int64 seed)  random_setSeed(JSContext *cx, int64 seed)
440  {  {
441      data->rngSeed = (seed ^ RNG_MULTIPLIER) & RNG_MASK;      cx->rngSeed = (seed ^ RNG_MULTIPLIER) & RNG_MASK;
442  }  }
443    
444  void  void
445  js_InitRandom(JSThreadData *data)  js_InitRandom(JSContext *cx)
446  {  {
447      /* Finally, set the seed from current time. */      /*
448      random_setSeed(data, PRMJ_Now() / 1000);       * Set the seed from current time. Since we have a RNG per context and we often bring
449         * up several contexts at the same time, we xor in some additional values, namely
450         * the context and its successor. We don't just use the context because it might be
451         * possible to reverse engineer the context pointer if one guesses the time right.
452         */
453        random_setSeed(cx,
454                       (PRMJ_Now() / 1000) ^
455                       int64(cx) ^
456                       int64(cx->link.next));
457  }  }
458    
459  static inline uint64  static inline uint64
460  random_next(JSThreadData *data, int bits)  random_next(JSContext *cx, int bits)
461  {  {
462      uint64 nextseed = data->rngSeed * RNG_MULTIPLIER;      uint64 nextseed = cx->rngSeed * RNG_MULTIPLIER;
463      nextseed += RNG_ADDEND;      nextseed += RNG_ADDEND;
464      nextseed &= RNG_MASK;      nextseed &= RNG_MASK;
465      data->rngSeed = nextseed;      cx->rngSeed = nextseed;
466      return nextseed >> (48 - bits);      return nextseed >> (48 - bits);
467  }  }
468    
469  static inline jsdouble  static inline jsdouble
470  random_nextDouble(JSThreadData *data)  random_nextDouble(JSContext *cx)
471  {  {
472      return jsdouble((random_next(data, 26) << 27) + random_next(data, 27)) / RNG_DSCALE;      return jsdouble((random_next(cx, 26) << 27) + random_next(cx, 27)) / RNG_DSCALE;
473  }  }
474    
475  static JSBool  static JSBool
476  math_random(JSContext *cx, uintN argc, jsval *vp)  math_random(JSContext *cx, uintN argc, jsval *vp)
477  {  {
478      jsdouble z = random_nextDouble(JS_THREAD_DATA(cx));      jsdouble z = random_nextDouble(cx);
479      return js_NewNumberInRootedValue(cx, z, vp);      return js_NewNumberInRootedValue(cx, z, vp);
480  }  }
481    
# Line 674  Line 682 
682  static jsdouble FASTCALL  static jsdouble FASTCALL
683  math_random_tn(JSContext *cx)  math_random_tn(JSContext *cx)
684  {  {
685      return random_nextDouble(JS_THREAD_DATA(cx));      return random_nextDouble(cx);
686  }  }
687    
688  static jsdouble FASTCALL  static jsdouble FASTCALL

Legend:
Removed from v.584  
changed lines
  Added in v.585

  ViewVC Help
Powered by ViewVC 1.1.24