1 /*
2 * %CopyrightBegin%
3 *
4 * Copyright Ericsson AB 1998-2016. All Rights Reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * %CopyrightEnd%
19 *
20
21 */
22 #include "hash.h"
23
24 /* this is a general prime factoring function
25 * we get one prime factor each time we call it
26 * we only use it here to determine if n is prime,
27 * by checking if factor(n) == n .
28 */
factor(int n)29 static int factor(int n)
30 {
31 /* FIXME problem for threaded?! */
32 static int a[] = { 0, 4, 1, 2, 0, 2 };
33 static int m = 0;
34 static int d = 0;
35
36 if (n) {
37 m = n;
38 d = 2;
39 }
40
41 while ((d*d) <= m) {
42 if (!(m%d)) {
43 m /= d;
44 return d;
45 }
46 d += a[d%6];
47 }
48 n = m;
49 m = 0;
50
51 return n;
52 }
53
54 /* true if n prime */
ei_isprime(int n)55 int ei_isprime(int n)
56 {
57 return (n == factor(n));
58 }
59