1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CORE_LIB_RANDOM_SIMPLE_PHILOX_H_ 17 #define TENSORFLOW_CORE_LIB_RANDOM_SIMPLE_PHILOX_H_ 18 19 #include <math.h> 20 #include <string.h> 21 #include <algorithm> 22 23 #include "philox_random.h" 24 #include "random_distributions.h" 25 26 namespace tensorflow { 27 namespace random { 28 29 // A simple imperative interface to Philox 30 class SimplePhilox { 31 public: 32 PHILOX_DEVICE_INLINE SimplePhilox(PhiloxRandom * gen)33 explicit SimplePhilox(PhiloxRandom* gen) : single_(gen) {} 34 35 // 32 random bits Rand32()36 PHILOX_DEVICE_INLINE uint32 Rand32() { return single_(); } 37 38 // 64 random bits Rand64()39 PHILOX_DEVICE_INLINE uint64 Rand64() { 40 const uint32 lo = single_(), hi = single_(); 41 return lo | static_cast<uint64>(hi) << 32; 42 } 43 44 // Uniform float in [0, 1) RandFloat()45 PHILOX_DEVICE_INLINE float RandFloat() { return Uint32ToFloat(single_()); } 46 47 // Uniform double in [0, 1) RandDouble()48 PHILOX_DEVICE_INLINE double RandDouble() { 49 const uint32 x0 = single_(), x1 = single_(); 50 return Uint64ToDouble(x0, x1); 51 } 52 53 // Skewed: pick "base" uniformly from range [0,max_log] and then 54 // return "base" random bits. The effect is to pick a number in the 55 // range [0,2^max_log-1] with bias towards smaller numbers. 56 uint32 Skewed(int max_log); 57 58 private: 59 SingleSampleAdapter<PhiloxRandom> single_; 60 }; 61 62 } // namespace random 63 } // namespace tensorflow 64 65 #endif // TENSORFLOW_CORE_LIB_RANDOM_SIMPLE_PHILOX_H_ 66