| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file ut/ut0math.cc |
| 29 |
Math functions. |
| 30 |
|
| 31 |
********************************************************************/ |
| 32 |
|
| 33 |
#include "ut0math.h" |
| 34 |
|
| 35 |
namespace ut { |
| 36 |
uint64_t find_prime(uint64_t n) { |
| 37 |
uint64_t pow2; |
| 38 |
uint64_t i; |
| 39 |
|
| 40 |
constexpr auto random1 = 1.0412321; |
| 41 |
constexpr auto random2 = 1.1131347; |
| 42 |
constexpr auto random3 = 1.0132677; |
| 43 |
|
| 44 |
n += 100; |
| 45 |
|
| 46 |
pow2 = 1; |
| 47 |
while (pow2 * 2 < n) { |
| 48 |
pow2 = 2 * pow2; |
| 49 |
} |
| 50 |
|
| 51 |
if ((double)n < 1.05 * (double)pow2) { |
| 52 |
n = (uint64_t)((double)n * random1); |
| 53 |
} |
| 54 |
|
| 55 |
pow2 = 2 * pow2; |
| 56 |
|
| 57 |
if ((double)n > 0.95 * (double)pow2) { |
| 58 |
n = (uint64_t)((double)n * random2); |
| 59 |
} |
| 60 |
|
| 61 |
if (n > pow2 - 20) { |
| 62 |
n += 30; |
| 63 |
} |
| 64 |
|
| 65 |
/* Now we have n far enough from powers of 2. To make |
| 66 |
n more random (especially, if it was not near |
| 67 |
a power of 2), we then multiply it by a random number. */ |
| 68 |
|
| 69 |
n = (uint64_t)((double)n * random3); |
| 70 |
|
| 71 |
for (;; n++) { |
| 72 |
i = 2; |
| 73 |
while (i * i <= n) { |
| 74 |
if (n % i == 0) { |
| 75 |
goto next_n; |
| 76 |
} |
| 77 |
i++; |
| 78 |
} |
| 79 |
|
| 80 |
/* Found a prime */ |
| 81 |
break; |
| 82 |
next_n:; |
| 83 |
} |
| 84 |
|
| 85 |
return (n); |
| 86 |
} |
| 87 |
} // namespace ut |