00001 /* 00002 * Author : Defour David 00003 * Contact : David.Defour@ens-lyon.fr 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU Lesser General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 #include <stdlib.h> 00020 #include "scs.h" 00021 #include "scs_private.h" 00022 00023 /* 00024 * Return 'sizeof(int)' random bits 00025 */ 00026 00027 00028 int rand_val(void){ 00029 int val; 00030 int i; 00031 00032 val = (rand() & 0x000000ff); 00033 for(i=0; i<(sizeof(int)); i++){ 00034 val = val << 8; 00035 val += (rand() & 0x000000ff ); /* we keep only 8 bits */ 00036 } 00037 return val; 00038 } 00039 00040 00041 /* 00042 * Put into 'result' a scs random number with the index field set 00043 * with a value between -expo_max and +expo_max. 00044 * 00045 * Rem. : 00046 * 1) If you want an scs number belonging to double precision floating 00047 * point number you must call scs_rand with an expo_max less than 39. 00048 * 2) expo_max must be less than RAND_MAX that is usually set a 00049 * value greater than 32767 00050 */ 00051 void scs_rand(scs_ptr result, int expo_max){ 00052 int i; 00053 00054 R_EXP = 1; 00055 R_IND = (rand() % (2*expo_max)) - expo_max; 00056 R_SGN = ((2*rand()- RAND_MAX) > 0) ? (-1) : (1); 00057 00058 00059 for(i=0; i<SCS_NB_WORDS; i++){ 00060 /* We keep the first SCS_NB_BITS bits of a random value */ 00061 R_HW[i] = rand_val() & SCS_MASK_RADIX; 00062 } 00063 }