You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/04/17 16:49:02 UTC
[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #5996: stdlib: generate uniformly distributed pseudo-random numbers
xiaoxiang781216 commented on code in PR #5996:
URL: https://github.com/apache/incubator-nuttx/pull/5996#discussion_r851779293
##########
libs/libc/stdlib/lib_rand48.c:
##########
@@ -0,0 +1,200 @@
+/****************************************************************************
+ * libs/libc/stdlib/lib_rand48.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdlib.h>
+
+#include <nuttx/lib/lib.h>
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static unsigned short g_seed48[7] =
+{
+ 0,
+ 0,
+ 0,
+ 0xe66d,
+ 0xdeec,
+ 0x5,
+ 0xb
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+#ifdef CONFIG_HAVE_LONG_LONG
+static uint64_t rand48_step(FAR unsigned short *xi, FAR unsigned short *lc)
+{
+ uint64_t a, x;
+
+ x = xi[0] | (xi[1] + (0ul << 16)) | (xi[2] + (0ull << 32));
+ a = lc[0] | (lc[1] + (0ul << 16)) | (lc[2] + (0ull << 32));
+ x = a * x + lc[3];
+
+ xi[0] = x;
+ xi[1] = x >> 16;
+ xi[2] = x >> 32;
+ return x & 0xffffffffffffull;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: srand48
+ ****************************************************************************/
+
+void srand48(long seed)
+{
+ unsigned short p[3];
+
+ p[0] = 0x330e;
+ p[1] = seed;
+ p[2] = seed >> 16;
+ seed48(p);
+}
+
+/****************************************************************************
+ * Name: seed48
+ ****************************************************************************/
+
+FAR unsigned short *seed48(FAR unsigned short seed16v[3])
+{
+ static unsigned short p[3];
+
+ memcpy(p, g_seed48, sizeof p);
+ memcpy(g_seed48, seed16v, sizeof p);
+ return p;
+}
+
+/****************************************************************************
+ * Name: lcong48
+ ****************************************************************************/
+
+void lcong48(FAR unsigned short p[7])
+{
+ memcpy(g_seed48, p, sizeof(g_seed48));
+}
+
+/****************************************************************************
+ * Name: jrand48
+ *
+ * Description:
+ * Return signed long integers uniformly distributed over the
+ * interval [-2^31, 2^31).
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_HAVE_LONG_LONG
Review Comment:
why need guard jrand48/mrand48/nrand48/lrand48 by CONFIG_HAVE_LONG_LONG?
##########
libs/libc/stdlib/lib_rand48.c:
##########
@@ -0,0 +1,200 @@
+/****************************************************************************
+ * libs/libc/stdlib/lib_rand48.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdlib.h>
+
+#include <nuttx/lib/lib.h>
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static unsigned short g_seed48[7] =
+{
+ 0,
+ 0,
+ 0,
+ 0xe66d,
+ 0xdeec,
+ 0x5,
+ 0xb
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+#ifdef CONFIG_HAVE_LONG_LONG
+static uint64_t rand48_step(FAR unsigned short *xi, FAR unsigned short *lc)
+{
+ uint64_t a, x;
Review Comment:
split to two lines
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org