Skip to content

Commit 7300719

Browse files
committed
[matgen] Cleanup power with integer exponent
Add header. Include header instead of forward declarations. Rename function to no longer follow Fortran naming scheme. Call by value instead of reference. Move float and double functions into a single file. Add Doxygen documentation.
1 parent 326eac9 commit 7300719

File tree

11 files changed

+96
-68
lines changed

11 files changed

+96
-68
lines changed

DOC/modules.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@
5656

5757
These tests start with the letter z.
5858
*/
59+
60+
/*!
61+
\defgroup TestingMatgen Matrix generation
62+
\ingroup Testing
63+
\brief Matrix generation for tests.
64+
*/

TESTING/MATGEN/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if(enable_single)
3030
slarot.c
3131
slatm2.c
3232
slatm3.c
33-
pow_ri.c
33+
powi.c
3434
)
3535
endif()
3636

@@ -48,7 +48,7 @@ if(enable_double)
4848
dlarot.c
4949
dlatm2.c
5050
dlatm3.c
51-
pow_di.c
51+
powi.c
5252
)
5353
endif()
5454

@@ -72,7 +72,7 @@ if(enable_complex)
7272
claghe.c
7373
clarnd.c
7474
# cdotc.c
75-
pow_ri.c
75+
powi.c
7676
)
7777
endif()
7878

@@ -96,7 +96,7 @@ if(enable_complex16)
9696
zlaghe.c
9797
zlarnd.c
9898
# zdotc.c
99-
pow_di.c
99+
powi.c
100100
)
101101
endif()
102102

TESTING/MATGEN/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ include ../../make.inc
3333
SCATGEN = slatm1.o slaran.o slarnd.o slaruv.o slabad.o slarnv.o
3434
SLASRC = slatb4.o slaset.o slartg.o
3535
SMATGEN = slatms.o slagge.o slagsy.o slarge.o slaror.o slarot.o slatm2.o slatm3.o
36-
SINTRINSIC = pow_ri.o
36+
SINTRINSIC = powi.o
3737

3838
DZATGEN = dlatm1.o dlaran.o dlarnd.o dlaruv.o dlabad.o dlarnv.o
3939
DLASRC = dlatb4.o dlaset.o dlartg.o
4040
DMATGEN = dlatms.o dlagge.o dlagsy.o dlarge.o dlaror.o dlarot.o dlatm2.o dlatm3.o
41-
DINTRINSIC = pow_di.o
41+
DINTRINSIC = powi.o
4242

4343
CLASRC = clatb4.o claset.o clartg.o clarnv.o clacgv.o csymv.o
4444
CMATGEN = clatms.o clagge.o clagsy.o clarge.o claror.o clarot.o clatm2.o clatm3.o \

TESTING/MATGEN/dlartg.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#include "../../SRC/slu_ddefs.h"
2+
#include "powi.h"
3+
14
#include <stdbool.h>
25
#include <math.h>
3-
#include "../../SRC/slu_ddefs.h"
46

57
/* Subroutine */ int dlartg_slu(double *f, double *g, double *cs, double *sn, double *r)
68
{
@@ -53,8 +55,7 @@
5355
/* System generated locals */
5456
int i__1;
5557
double d__1, d__2;
56-
/* Builtin functions */
57-
double pow_di(double *, int *);
58+
5859
/* Local variables */
5960
static int i;
6061
static double scale;
@@ -71,7 +72,7 @@
7172
eps = dmach("E");
7273
d__1 = dmach("B");
7374
i__1 = (int) (log(safmin / eps) / log(dmach("B")) / 2.);
74-
safmn2 = pow_di(&d__1, &i__1);
75+
safmn2 = powi(d__1, i__1);
7576
safmx2 = 1. / safmn2;
7677
}
7778
if (*g == 0.) {

TESTING/MATGEN/dlatm1.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* -- translated by f2c (version 19940927).
22
*/
3+
#include "powi.h"
34

45
#include <math.h>
56
#include <stdlib.h>
@@ -12,9 +13,6 @@
1213
int i__1, i__2;
1314
double d__1;
1415

15-
/* Builtin functions */
16-
double pow_di(double *, int *);
17-
1816
/* Local variables */
1917
static double temp;
2018
static int i;
@@ -189,7 +187,7 @@
189187
i__1 = *n;
190188
for (i = 2; i <= i__1; ++i) {
191189
i__2 = i - 1;
192-
d[i] = pow_di(&alpha, &i__2);
190+
d[i] = powi(alpha, i__2);
193191
/* L60: */
194192
}
195193
}

TESTING/MATGEN/pow_di.c

Lines changed: 0 additions & 22 deletions
This file was deleted.

TESTING/MATGEN/pow_ri.c

Lines changed: 0 additions & 25 deletions
This file was deleted.

TESTING/MATGEN/powi.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*! \file
2+
* \ingroup TestingMatgen
3+
* \brief Power with integer exponent
4+
*/
5+
6+
/*!
7+
* Power with integer exponent
8+
*
9+
* Implemented as Exponentiation by Squaring for speed.
10+
*
11+
* \param[in] base base as double value
12+
* \param[in] exp exponent as integer value
13+
* \return Computes the value of \p base raised to the power \p exp
14+
*/
15+
double powi(double base, int exp)
16+
{
17+
double x = base;
18+
int n = exp;
19+
double pow = 1.0;
20+
21+
if (n != 0) {
22+
if(n < 0) {
23+
n = -n;
24+
x = 1 / x;
25+
}
26+
for( ; ; ) {
27+
if(n & 01)
28+
pow *= x;
29+
if(n >>= 1)
30+
x *= x;
31+
else
32+
break;
33+
}
34+
}
35+
return pow;
36+
}
37+
38+
/*!
39+
* Power with integer exponent
40+
*
41+
* Implemented as Exponentiation by Squaring for speed.
42+
*
43+
* \param[in] base base as float value
44+
* \param[in] exp exponent as integer value
45+
* \return Computes the value of \p base raised to the power \p exp
46+
*/
47+
float powif(float base, int exp)
48+
{
49+
float x = base;
50+
int n = exp;
51+
float pow = 1.0;
52+
53+
if (n != 0) {
54+
if (n < 0) {
55+
n = -n;
56+
x = 1 / x;
57+
}
58+
for ( ; ; ) {
59+
if (n & 01)
60+
pow *= x;
61+
if (n >>= 1)
62+
x *= x;
63+
else
64+
break;
65+
}
66+
}
67+
return pow;
68+
}

TESTING/MATGEN/powi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
double powi(double base, int exp);
2+
3+
float powif(float base, int exp);

TESTING/MATGEN/slartg.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "powi.h"
2+
13
#include <math.h>
24
#include <stdbool.h>
35

@@ -52,8 +54,7 @@
5254
/* System generated locals */
5355
int i__1;
5456
float r__1, r__2;
55-
/* Builtin functions */
56-
double pow_ri(float *, int *);
57+
5758
/* Local variables */
5859
static int i;
5960
static float scale;
@@ -69,7 +70,7 @@
6970
eps = smach("E");
7071
r__1 = smach("B");
7172
i__1 = (int) (log(safmin / eps) / log(smach("B")) / 2.f);
72-
safmn2 = pow_ri(&r__1, &i__1);
73+
safmn2 = powif(r__1, i__1);
7374
safmx2 = 1.f / safmn2;
7475
}
7576
if (*g == 0.f) {

TESTING/MATGEN/slatm1.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* -- translated by f2c (version 19940927).
22
*/
3+
#include "powi.h"
34

45
#include <math.h>
56
#include <stdlib.h>
@@ -11,9 +12,6 @@
1112
int i__1, i__2;
1213
double d__1, d__2;
1314

14-
/* Builtin functions */
15-
double pow_ri(float *, int *);
16-
1715
/* Local variables */
1816
static float temp;
1917
static int i;
@@ -190,7 +188,7 @@
190188
i__1 = *n;
191189
for (i = 2; i <= i__1; ++i) {
192190
i__2 = i - 1;
193-
d[i] = pow_ri(&alpha, &i__2);
191+
d[i] = powif(alpha, i__2);
194192
/* L60: */
195193
}
196194
}

0 commit comments

Comments
 (0)