gst_plugins_base/gst/audioconvert/gstfastrandom.h
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
equal deleted inserted replaced
29:567bb019e3e3 30:7e817e7e631c
     1 /* GStreamer
       
     2  * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
       
     3  *
       
     4  * gstfastrandom.h: Fast, bad PNRG
       
     5  *
       
     6  * This library is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU Library General Public
       
     8  * License as published by the Free Software Foundation; either
       
     9  * version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This library is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * Library General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Library General Public
       
    17  * License along with this library; if not, write to the
       
    18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    19  * Boston, MA 02111-1307, USA.
       
    20  */
       
    21 
       
    22 #include <glib.h>
       
    23 
       
    24 #ifndef __GST_FAST_RANDOM__
       
    25 #define __GST_FAST_RANDOM__
       
    26 
       
    27 /* transform [0..2^32] -> [0..1] */
       
    28 #define GST_RAND_DOUBLE_TRANSFORM 2.3283064365386962890625e-10
       
    29 
       
    30 /* This is the base function, implementing a linear congruential generator
       
    31  * and returning a pseudo random number between 0 and 2^32 - 1.
       
    32  */
       
    33 static inline guint32
       
    34 gst_fast_random_uint32 ()
       
    35 {
       
    36   static guint32 state = 0xdeadbeef;
       
    37 
       
    38   return (state = state * 1103515245 + 12345);
       
    39 }
       
    40 
       
    41 static inline guint32
       
    42 gst_fast_random_uint32_range (gint32 start, gint32 end)
       
    43 {
       
    44   guint64 tmp = gst_fast_random_uint32 ();
       
    45 
       
    46   tmp = (tmp * (end - start)) / G_MAXUINT32 + start;
       
    47 
       
    48   return (guint32) tmp;
       
    49 }
       
    50 
       
    51 static inline gint32
       
    52 gst_fast_random_int32 (void)
       
    53 {
       
    54   return (gint32) gst_fast_random_uint32 ();
       
    55 }
       
    56 
       
    57 static inline gint32
       
    58 gst_fast_random_int32_range (gint32 start, gint32 end)
       
    59 {
       
    60   gint64 tmp = gst_fast_random_uint32 ();
       
    61 
       
    62   tmp = (tmp * (end - start)) / G_MAXUINT32 + start;
       
    63 
       
    64   return (gint32) tmp;
       
    65 }
       
    66 
       
    67 static inline gdouble
       
    68 gst_fast_random_double (void)
       
    69 {
       
    70   gdouble ret;
       
    71 
       
    72   ret = gst_fast_random_uint32 () * GST_RAND_DOUBLE_TRANSFORM;
       
    73   ret = (ret + gst_fast_random_uint32 ()) * GST_RAND_DOUBLE_TRANSFORM;
       
    74 
       
    75   if (ret >= 1.0)
       
    76     return gst_fast_random_double ();
       
    77 
       
    78   return ret;
       
    79 }
       
    80 
       
    81 static inline gdouble
       
    82 gst_fast_random_double_range (gdouble start, gdouble end)
       
    83 {
       
    84   return gst_fast_random_double () * (end - start) + start;
       
    85 }
       
    86 
       
    87 #undef GST_RAND_DOUBLE_TRANSFORM
       
    88 
       
    89 #endif /* __GST_FAST_RANDOM__ */
       
    90