kaldi-math.cc (1554B)
1 // kaldi-native-fbank/csrc/kaldi-math.cc 2 // 3 // Copyright (c) 2024 Brno University of Technology (authors: Karel Vesely) 4 5 // This file is an excerpt from kaldi/src/base/kaldi-math.cc 6 7 #include "kaldi-native-fbank/csrc/kaldi-math.h" 8 9 #if defined(_POSIX_THREAD_SAFE_FUNCTIONS) 10 #include <mutex> // NOLINT 11 #endif 12 13 #include <cmath> 14 15 namespace knf { 16 17 int Rand(struct RandomState *state) { 18 #if !defined(_POSIX_THREAD_SAFE_FUNCTIONS) 19 // On Windows and Cygwin, just call Rand() 20 return rand(); 21 #else 22 if (state) { 23 return rand_r(&(state->seed)); 24 } else { 25 static std::mutex _RandMutex; 26 std::lock_guard<std::mutex> lock(_RandMutex); 27 return rand(); 28 } 29 #endif 30 } 31 32 RandomState::RandomState() { 33 // we initialize it as Rand() + 27437 instead of just Rand(), because on some 34 // systems, e.g. at the very least Mac OSX Yosemite and later, it seems to be 35 // the case that rand_r when initialized with rand() will give you the exact 36 // same sequence of numbers that rand() will give if you keep calling rand() 37 // after that initial call. This can cause problems with repeated sequences. 38 // For example if you initialize two RandomState structs one after the other 39 // without calling rand() in between, they would give you the same sequence 40 // offset by one (if we didn't have the "+ 27437" in the code). 27437 is just 41 // a randomly chosen prime number. 42 seed = unsigned(Rand()) + 27437; 43 } 44 45 void Sqrt(float *in_out, int32_t n) { 46 for (int32_t i = 0; i != n; ++i) { 47 in_out[i] = std::sqrt(in_out[i]); 48 } 49 } 50 51 } // namespace knf