areusch commented on a change in pull request #7266:
URL: https://github.com/apache/tvm/pull/7266#discussion_r561139856



##########
File path: src/runtime/crt/host/main.cc
##########
@@ -93,6 +94,20 @@ tvm_crt_error_t TVMPlatformTimerStop(double* 
elapsed_time_seconds) {
   g_utvm_timer_running = 0;
   return kTvmErrorNoError;
 }
+
+static_assert(RAND_MAX >= (1 << 8), "RAND_MAX is smaller than acceptable");
+unsigned int random_seed = 0;
+tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) {
+  if (random_seed == 0) {
+    random_seed = (unsigned int)time(NULL);
+  }
+  for (size_t i = 0; i < num_bytes; ++i) {
+    int random = rand_r(&random_seed);
+    buffer[i] = (uint8_t)random;

Review comment:
       ah i see, yeah that would work. hmm, I think if we had the ability to 
template the random block type (I.e. uint32_t right now) without a C macro i'd 
do it, but maybe it's okay to leave it as is for now.
   
   the main difference between the zephyr one and the host one is that the 
underlying rng function is different. the host uses `rand_r` and zephyr uses 
`sys_rand32_get`. note that this main.cc is not compiled using zephyr but 
rather just directly against the system headers, and it uses posix pipes to 
communicate.

##########
File path: tests/micro/qemu/zephyr-runtime/src/main.c
##########
@@ -161,6 +162,27 @@ tvm_crt_error_t TVMPlatformTimerStop(double* 
elapsed_time_seconds) {
   return kTvmErrorNoError;
 }
 
+tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) {
+  uint32_t random;  // one unit of random data.
+
+  // Fill parts of `buffer` which are as large as `random`.
+  size_t num_full_blocks = num_bytes / sizeof(random);
+  for (int i = 0; i < num_full_blocks; ++i) {
+    random = sys_rand32_get();
+    memcpy(&buffer[i * sizeof(random)], &random, sizeof(random));
+  }
+
+  // Fill any leftover tail which is smaller than `random.

Review comment:
       done

##########
File path: tests/micro/qemu/zephyr-runtime/src/main.c
##########
@@ -161,6 +162,27 @@ tvm_crt_error_t TVMPlatformTimerStop(double* 
elapsed_time_seconds) {
   return kTvmErrorNoError;
 }
 
+tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) {
+  uint32_t random;  // one unit of random data.

Review comment:
       simplified the tail block computation at the end of mine using modulo 
operator. thanks for the suggestion!




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to