Better seed support for (random).
[bpt/emacs.git] / src / fns.c
index 739ffca..2dee851 100644 (file)
--- a/src/fns.c
+++ b/src/fns.c
@@ -74,32 +74,16 @@ Other values of LIMIT are ignored.  */)
   (Lisp_Object limit)
 {
   EMACS_INT val;
-  Lisp_Object lispy_val;
 
   if (EQ (limit, Qt))
-    {
-      EMACS_TIME t = current_emacs_time ();
-      seed_random (getpid () ^ EMACS_SECS (t) ^ EMACS_NSECS (t));
-    }
+    init_random ();
+  else if (STRINGP (limit))
+    seed_random (SSDATA (limit), SBYTES (limit));
 
+  val = get_random ();
   if (NATNUMP (limit) && XFASTINT (limit) != 0)
-    {
-      /* Try to take our random number from the higher bits of VAL,
-        not the lower, since (says Gentzel) the low bits of `random'
-        are less random than the higher ones.  We do this by using the
-        quotient rather than the remainder.  At the high end of the RNG
-        it's possible to get a quotient larger than n; discarding
-        these values eliminates the bias that would otherwise appear
-        when using a large n.  */
-      EMACS_INT denominator = (INTMASK + 1) / XFASTINT (limit);
-      do
-       val = get_random () / denominator;
-      while (val >= XFASTINT (limit));
-    }
-  else
-    val = get_random ();
-  XSETINT (lispy_val, val);
-  return lispy_val;
+    val %= XFASTINT (limit);
+  return make_number (val);
 }
 \f
 /* Heuristic on how many iterations of a tight loop can be safely done
@@ -628,7 +612,7 @@ concat (ptrdiff_t nargs, Lisp_Object *args,
          ptrdiff_t thislen_byte = SBYTES (this);
 
          memcpy (SDATA (val) + toindex_byte, SDATA (this), SBYTES (this));
-         if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
+         if (string_intervals (this))
            {
              textprops[num_textprops].argnum = argnum;
              textprops[num_textprops].from = 0;
@@ -640,7 +624,7 @@ concat (ptrdiff_t nargs, Lisp_Object *args,
       /* Copy a single-byte string to a multibyte string.  */
       else if (STRINGP (this) && STRINGP (val))
        {
-         if (! NULL_INTERVAL_P (STRING_INTERVALS (this)))
+         if (string_intervals (this))
            {
              textprops[num_textprops].argnum = argnum;
              textprops[num_textprops].from = 0;
@@ -1060,7 +1044,7 @@ If you're not sure, whether to use `string-as-multibyte' or
        str_as_multibyte (SDATA (new_string), nbytes,
                          SBYTES (string), NULL);
       string = new_string;
-      STRING_SET_INTERVALS (string, NULL_INTERVAL);
+      set_string_intervals (string, NULL);
     }
   return string;
 }
@@ -1100,7 +1084,7 @@ an error is signaled.  */)
     {
       ptrdiff_t chars = SCHARS (string);
       unsigned char *str = xmalloc (chars);
-      ptrdiff_t converted = str_to_unibyte (SDATA (string), str, chars, 0);
+      ptrdiff_t converted = str_to_unibyte (SDATA (string), str, chars);
 
       if (converted < chars)
        error ("Can't convert the %"pD"dth character to unibyte", converted);
@@ -1868,7 +1852,7 @@ This is the last value stored with `(put SYMBOL PROPNAME VALUE)'.  */)
   (Lisp_Object symbol, Lisp_Object propname)
 {
   CHECK_SYMBOL (symbol);
-  return Fplist_get (SVAR (XSYMBOL (symbol), plist), propname);
+  return Fplist_get (XSYMBOL (symbol)->plist, propname);
 }
 
 DEFUN ("plist-put", Fplist_put, Splist_put, 3, 3, 0,
@@ -1910,8 +1894,8 @@ It can be retrieved with `(get SYMBOL PROPNAME)'.  */)
   (Lisp_Object symbol, Lisp_Object propname, Lisp_Object value)
 {
   CHECK_SYMBOL (symbol);
-  SVAR (XSYMBOL (symbol), plist)
-    = Fplist_put (SVAR (XSYMBOL (symbol), plist), propname, value);
+  set_symbol_plist
+    (symbol, Fplist_put (XSYMBOL (symbol)->plist, propname, value));
   return value;
 }
 \f
@@ -2053,8 +2037,8 @@ internal_equal (register Lisp_Object o1, register Lisp_Object o2, int depth, int
              || !internal_equal (OVERLAY_END (o1), OVERLAY_END (o2),
                                  depth + 1, props))
            return 0;
-         o1 = MVAR (XOVERLAY (o1), plist);
-         o2 = MVAR (XOVERLAY (o2), plist);
+         o1 = XOVERLAY (o1)->plist;
+         o2 = XOVERLAY (o2)->plist;
          goto tail_recurse;
        }
       if (MARKERP (o1))
@@ -2139,19 +2123,15 @@ ARRAY is a vector, string, char-table, or bool-vector.  */)
   register ptrdiff_t size, idx;
 
   if (VECTORP (array))
-    {
-      register Lisp_Object *p = XVECTOR (array)->contents;
-      size = ASIZE (array);
-      for (idx = 0; idx < size; idx++)
-       p[idx] = item;
-    }
+    for (idx = 0, size = ASIZE (array); idx < size; idx++)
+      ASET (array, idx, item);
   else if (CHAR_TABLE_P (array))
     {
       int i;
 
       for (i = 0; i < (1 << CHARTAB_SIZE_BITS_0); i++)
-       XCHAR_TABLE (array)->contents[i] = item;
-      XCHAR_TABLE (array)->defalt = item;
+       set_char_table_contents (array, i, item);
+      set_char_table_defalt (array, item);
     }
   else if (STRINGP (array))
     {
@@ -3663,7 +3643,7 @@ make_hash_table (Lisp_Object test, Lisp_Object size, Lisp_Object rehash_size,
 
   /* Set up the free list.  */
   for (i = 0; i < sz - 1; ++i)
-    set_hash_next (h, i, make_number (i + 1));
+    set_hash_next_slot (h, i, make_number (i + 1));
   h->next_free = make_number (0);
 
   XSET_HASH_TABLE (table, h);
@@ -3760,17 +3740,17 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
        }
 #endif
 
-      h->key_and_value = larger_vector (h->key_and_value,
-                                       2 * (new_size - old_size), -1);
-      h->next = larger_vector (h->next, new_size - old_size, -1);
-      h->hash = larger_vector (h->hash, new_size - old_size, -1);
-      h->index = Fmake_vector (make_number (index_size), Qnil);
+      set_hash_key_and_value (h, larger_vector (h->key_and_value,
+                                               2 * (new_size - old_size), -1));
+      set_hash_next (h, larger_vector (h->next, new_size - old_size, -1));
+      set_hash_hash (h, larger_vector (h->hash, new_size - old_size, -1));
+      set_hash_index (h, Fmake_vector (make_number (index_size), Qnil));
 
       /* Update the free list.  Do it so that new entries are added at
          the end of the free list.  This makes some operations like
          maphash faster.  */
       for (i = old_size; i < new_size - 1; ++i)
-       set_hash_next (h, i, make_number (i + 1));
+       set_hash_next_slot (h, i, make_number (i + 1));
 
       if (!NILP (h->next_free))
        {
@@ -3781,7 +3761,7 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
                 !NILP (next))
            last = next;
 
-         set_hash_next (h, XFASTINT (last), make_number (old_size));
+         set_hash_next_slot (h, XFASTINT (last), make_number (old_size));
        }
       else
        XSETFASTINT (h->next_free, old_size);
@@ -3792,8 +3772,8 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *h)
          {
            EMACS_UINT hash_code = XUINT (HASH_HASH (h, i));
            ptrdiff_t start_of_bucket = hash_code % ASIZE (h->index);
-           set_hash_next (h, i, HASH_INDEX (h, start_of_bucket));
-           set_hash_index (h, start_of_bucket, make_number (i));
+           set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
+           set_hash_index_slot (h, start_of_bucket, make_number (i));
          }
     }
 }
@@ -3852,16 +3832,16 @@ hash_put (struct Lisp_Hash_Table *h, Lisp_Object key, Lisp_Object value,
   /* Store key/value in the key_and_value vector.  */
   i = XFASTINT (h->next_free);
   h->next_free = HASH_NEXT (h, i);
-  set_hash_key (h, i, key);
-  set_hash_value (h, i, value);
+  set_hash_key_slot (h, i, key);
+  set_hash_value_slot (h, i, value);
 
   /* Remember its hash code.  */
-  set_hash_hash (h, i, make_number (hash));
+  set_hash_hash_slot (h, i, make_number (hash));
 
   /* Add new entry to its collision chain.  */
   start_of_bucket = hash % ASIZE (h->index);
-  set_hash_next (h, i, HASH_INDEX (h, start_of_bucket));
-  set_hash_index (h, start_of_bucket, make_number (i));
+  set_hash_next_slot (h, i, HASH_INDEX (h, start_of_bucket));
+  set_hash_index_slot (h, start_of_bucket, make_number (i));
   return i;
 }
 
@@ -3892,16 +3872,16 @@ hash_remove_from_table (struct Lisp_Hash_Table *h, Lisp_Object key)
        {
          /* Take entry out of collision chain.  */
          if (NILP (prev))
-           set_hash_index (h, start_of_bucket, HASH_NEXT (h, i));
+           set_hash_index_slot (h, start_of_bucket, HASH_NEXT (h, i));
          else
-           set_hash_next (h, XFASTINT (prev), HASH_NEXT (h, i));
+           set_hash_next_slot (h, XFASTINT (prev), HASH_NEXT (h, i));
 
          /* Clear slots in key_and_value and add the slots to
             the free list.  */
-         set_hash_key (h, i, Qnil);
-         set_hash_value (h, i, Qnil);
-         set_hash_hash (h, i, Qnil);
-         set_hash_next (h, i, h->next_free);
+         set_hash_key_slot (h, i, Qnil);
+         set_hash_value_slot (h, i, Qnil);
+         set_hash_hash_slot (h, i, Qnil);
+         set_hash_next_slot (h, i, h->next_free);
          h->next_free = make_number (i);
          h->count--;
          eassert (h->count >= 0);
@@ -3927,10 +3907,10 @@ hash_clear (struct Lisp_Hash_Table *h)
 
       for (i = 0; i < size; ++i)
        {
-         set_hash_next (h, i, i < size - 1 ? make_number (i + 1) : Qnil);
-         set_hash_key (h, i, Qnil);
-         set_hash_value (h, i, Qnil);
-         set_hash_hash (h, i, Qnil);
+         set_hash_next_slot (h, i, i < size - 1 ? make_number (i + 1) : Qnil);
+         set_hash_key_slot (h, i, Qnil);
+         set_hash_value_slot (h, i, Qnil);
+         set_hash_hash_slot (h, i, Qnil);
        }
 
       for (i = 0; i < ASIZE (h->index); ++i)
@@ -3971,8 +3951,8 @@ sweep_weak_table (struct Lisp_Hash_Table *h, int remove_entries_p)
       for (idx = HASH_INDEX (h, bucket); !NILP (idx); idx = next)
        {
          ptrdiff_t i = XFASTINT (idx);
-         int key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
-         int value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
+         bool key_known_to_survive_p = survives_gc_p (HASH_KEY (h, i));
+         bool value_known_to_survive_p = survives_gc_p (HASH_VALUE (h, i));
          int remove_p;
 
          if (EQ (h->weak, Qkey))
@@ -3994,18 +3974,18 @@ sweep_weak_table (struct Lisp_Hash_Table *h, int remove_entries_p)
                {
                  /* Take out of collision chain.  */
                  if (NILP (prev))
-                   set_hash_index (h, bucket, next);
+                   set_hash_index_slot (h, bucket, next);
                  else
-                   set_hash_next (h, XFASTINT (prev), next);
+                   set_hash_next_slot (h, XFASTINT (prev), next);
 
                  /* Add to free list.  */
-                 set_hash_next (h, i, h->next_free);
+                 set_hash_next_slot (h, i, h->next_free);
                  h->next_free = idx;
 
                  /* Clear key, value, and hash.  */
-                 set_hash_key (h, i, Qnil);
-                 set_hash_value (h, i, Qnil);
-                 set_hash_hash (h, i, Qnil);
+                 set_hash_key_slot (h, i, Qnil);
+                 set_hash_value_slot (h, i, Qnil);
+                 set_hash_hash_slot (h, i, Qnil);
 
                  h->count--;
                }
@@ -4512,7 +4492,7 @@ VALUE.  In any case, return VALUE.  */)
 
   i = hash_lookup (h, key, &hash);
   if (i >= 0)
-    set_hash_value (h, i, value);
+    set_hash_value_slot (h, i, value);
   else
     hash_put (h, key, value, hash);
 
@@ -4660,13 +4640,12 @@ secure_hash (Lisp_Object algorithm, Lisp_Object object, Lisp_Object start, Lisp_
     {
       struct buffer *prev = current_buffer;
 
-      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
+      record_unwind_current_buffer ();
 
       CHECK_BUFFER (object);
 
       bp = XBUFFER (object);
-      if (bp != current_buffer)
-       set_buffer_internal (bp);
+      set_buffer_internal (bp);
 
       if (NILP (start))
        b = BEGV;
@@ -4753,8 +4732,7 @@ secure_hash (Lisp_Object algorithm, Lisp_Object object, Lisp_Object start, Lisp_
        }
 
       object = make_buffer_string (b, e, 0);
-      if (prev != current_buffer)
-       set_buffer_internal (prev);
+      set_buffer_internal (prev);
       /* Discard the unwind protect for recovering the current
         buffer.  */
       specpdl_ptr--;