os/ossrv/genericopenlibs/liboil/src/liboilrandom.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2  * LIBOIL - Library of Optimized Inner Loops
     3  * Copyright (c) 2005 David A. Schleef <ds@schleef.org>
     4  * All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions
     8  * are met:
     9  * 1. Redistributions of source code must retain the above copyright
    10  *    notice, this list of conditions and the following disclaimer.
    11  * 2. Redistributions in binary form must reproduce the above copyright
    12  *    notice, this list of conditions and the following disclaimer in the
    13  *    documentation and/or other materials provided with the distribution.
    14  * 
    15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
    19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    25  * POSSIBILITY OF SUCH DAMAGE.
    26  */
    27 
    28 #ifdef HAVE_CONFIG_H
    29 #include "config.h"
    30 #endif
    31 
    32 #include <liboil/liboilrandom.h>
    33 #include "liboil/liboilcolorspace.h"
    34 #include <stdlib.h>
    35 
    36 
    37 /**
    38  * SECTION:liboilrandom
    39  * @title: Random Number Generation
    40  * @short_description: Random number generation
    41  */
    42 
    43 static void
    44 _oil_random_bits (void *dest, int n)
    45 {
    46   int i;
    47   uint8_t *d = dest;
    48   for(i=0;i<n;i++){
    49     d[i] = (rand()>>16);
    50   }
    51 }
    52 
    53 /**
    54  * oil_random_s32:
    55  * @dest:
    56  * @n:
    57  *
    58  * Writes random values in the range [-(1<<31), (1<<31)-1] to the
    59  * destination array.
    60  */
    61 #ifdef __SYMBIAN32__
    62 EXPORT_C
    63 #endif
    64 void
    65 oil_random_s32(oil_type_s32 *dest, int n)
    66 {
    67   _oil_random_bits (dest, n*4);
    68 }
    69 
    70 /**
    71  * oil_random_s64:
    72  *
    73  * Writes random values in the range [-(1<<63), (1<<63)-1] to the
    74  * destination array.
    75  */
    76 #ifdef __SYMBIAN32__
    77 EXPORT_C
    78 #endif
    79 void
    80 oil_random_s64 (oil_type_s64 *dest, int n)
    81 {
    82   _oil_random_bits (dest, n*8);
    83 }
    84 
    85 /**
    86  * oil_random_s16:
    87  *
    88  * Writes random values in the range [-(1<<15), (1<<15)-1] to the
    89  * destination array.
    90  */
    91 #ifdef __SYMBIAN32__
    92 EXPORT_C
    93 #endif
    94 void
    95 oil_random_s16 (oil_type_s16 *dest, int n)
    96 {
    97   _oil_random_bits (dest, n*2);
    98 }
    99 
   100 /**
   101  * oil_random_s8:
   102  *
   103  * Writes random values in the range [-(1<<7), (1<<7)-1] to the
   104  * destination array.
   105  */
   106 #ifdef __SYMBIAN32__
   107 EXPORT_C
   108 #endif
   109 void
   110 oil_random_s8 (oil_type_s8 *dest, int n)
   111 {
   112   _oil_random_bits (dest, n);
   113 }
   114 
   115 /**
   116  * oil_random_u32:
   117  *
   118  * Writes random values in the range [0, (1<<32)-1] to the
   119  * destination array.
   120  */
   121 #ifdef __SYMBIAN32__
   122 EXPORT_C
   123 #endif
   124 void
   125 oil_random_u32 (oil_type_u32 *dest, int n)
   126 {
   127   _oil_random_bits (dest, n*4);
   128 }
   129 
   130 /**
   131  * oil_random_u64:
   132  *
   133  * Writes random values in the range [0, (1<<64)-1] to the
   134  * destination array.
   135  */
   136 #ifdef __SYMBIAN32__
   137 EXPORT_C
   138 #endif
   139 void
   140 oil_random_u64 (oil_type_u64 *dest, int n)
   141 {
   142   _oil_random_bits (dest, n*8);
   143 }
   144 
   145 /**
   146  * oil_random_u16:
   147  *
   148  * Writes random values in the range [0, (1<<16)-1] to the
   149  * destination array.
   150  */
   151 #ifdef __SYMBIAN32__
   152 EXPORT_C
   153 #endif
   154 void
   155 oil_random_u16 (oil_type_u16 *dest, int n)
   156 {
   157   _oil_random_bits (dest, n*2);
   158 }
   159 
   160 /**
   161  * oil_random_u8:
   162  *
   163  * Writes random values in the range [0, (1<<8)-1] to the
   164  * destination array.
   165  */
   166 #ifdef __SYMBIAN32__
   167 EXPORT_C
   168 #endif
   169 void
   170 oil_random_u8 (oil_type_u8 *dest, int n)
   171 {
   172   _oil_random_bits (dest, n);
   173 }
   174 
   175 /**
   176  * oil_random_f64:
   177  *
   178  * Writes random double-precision floating point values in the
   179  * range [0, 1.0) to the destination array.
   180  */
   181 #ifdef __SYMBIAN32__
   182 EXPORT_C
   183 #endif
   184 void
   185 oil_random_f64 (oil_type_f64 *dest, int n)
   186 {
   187   int i;
   188   for(i=0;i<n;i++){
   189     dest[i] = (((rand()/(RAND_MAX+1.0))+rand())/(RAND_MAX+1.0));
   190   }
   191 }
   192 
   193 /**
   194  * oil_random_f32:
   195  *
   196  * Writes random single-precision floating point values in the
   197  * range [0, 1.0) to the destination array.
   198  */
   199 #ifdef __SYMBIAN32__
   200 EXPORT_C
   201 #endif
   202 void
   203 oil_random_f32 (oil_type_f32 *dest, int n)
   204 {
   205   int i;
   206   for(i=0;i<n;i++){
   207     dest[i] = (rand()/(RAND_MAX+1.0));
   208   }
   209 }
   210 
   211 /**
   212  * oil_random_alpha:
   213  *
   214  * Writes random values in the range [0, 255] to the destination
   215  * array suitable for alpha values.  This is similar to oil_random_u8(),
   216  * except the values 0 and 255 are strongly favored.
   217  */
   218 #ifdef __SYMBIAN32__
   219 EXPORT_C
   220 #endif
   221 void
   222 oil_random_alpha(uint8_t *dest, int n)
   223 {
   224   int i;
   225   int x;
   226   for(i=0;i<n;i++){
   227     x = ((rand()>>8) & 0x1ff) - 0x80;
   228     if (x<0) x = 0;
   229     if (x>255) x = 255;
   230     dest[i] = x;
   231   }
   232 }
   233 
   234 /**
   235  * oil_random_argb:
   236  * @dest: destination array.
   237  * @n: number of values to write.
   238  *
   239  * Creates valid random RGBA values and places them in the destination
   240  * array.
   241  */
   242 #ifdef __SYMBIAN32__
   243 EXPORT_C
   244 #endif
   245 void
   246 oil_random_argb(uint32_t *dest, int n)
   247 {
   248   int i;
   249   int x;
   250   for(i=0;i<n;i++){
   251     x = ((rand()>>8) & 0x1ff) - 0x80;
   252     if (x<0) x = 0;
   253     if (x>255) x = 255;
   254     dest[i] = oil_argb_noclamp(x,
   255         oil_muldiv_255(x,oil_rand_u8()),
   256         oil_muldiv_255(x,oil_rand_u8()),
   257         oil_muldiv_255(x,oil_rand_u8()));
   258   }
   259 
   260 }
   261 
   262