Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Floating-Point Guide: HP 9000 Computers > Chapter 4 HP-UX Math Libraries on HP 9000 Systems

Contents of the HP-UX Math Libraries

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

This section describes in some detail the contents of the math libraries. These libraries include:

  • Scalar math libraries (libm and libcl)

  • The BLAS library libblas (provided with the HP Fortran 90 and HP FORTRAN/9000 products only)

  • The vector library libvec (provided with the HP FORTRAN/9000 product only) (obsolete)

The math libraries run well on both PA-RISC 1.1 and PA-RISC 2.0 systems. HP does not provide PA2.0 versions of the math libraries, although it does provide PA2.0 versions of millicode functions. See “Millicode Versions of Math Library Functions” for details.

Scalar Math Libraries (libm and libcl)

The scalar math libraries are implemented using the PA-RISC 1.1 instruction set. The most important functions are carefully optimized.

For even faster performance, a number of frequently used math functions are implemented in the millicode library (/usr/lib/milli.a) as well as in the standard math library. See “Millicode Versions of Math Library Functions” for details.

Millicode Versions of Math Library Functions

Several of the most frequently used math functions are implemented in the millicode library as well as in the math library. The millicode versions have a streamlined calling sequence and are usually faster than their counterparts in the math library.

Millicode versions exist for the following Fortran and C functions. They have double-precision versions only, unless otherwise specified.

acos
asin
atan
atan2
cos (single-precision also)
exp
log (single-precision also)
log10
pow
sin (single-precision also)
tan (single-precision also)

Millicode versions exist for the following Pascal functions:

arctan
cos
exp
ln
sin

To get the millicode versions of any of these functions, compile your program with

  • Any optimization level (0 through 4)

  • The +Olibcalls or the +Oaggressive optimization option

With the f90 and f77 compiler commands, the +Olibcalls option is the default at optimization level 2 and above.

The +Olibcalls option is invoked by default when you specify the optimization type ­+Oaggressive; use +Oaggressive +Onolibcalls if you want aggressive optimization without using millicode routines. The millicode versions are implemented in the library /usr/lib/milli.a.

The millicode versions of functions do not provide standard-conforming error handling. This has different implications for different languages. In C programs, if an error occurs, the millicode versions return the same values as their standard library counterparts, but they do not set errno. Because the C and Pascal standards specify error handling for library functions, you should use millicode versions in C and Pascal programs only if your program does not require standard-conforming error handling. The Fortran standards do not specify error handling for math intrinsic functions, so using the millicode versions has no effect on standards compliance. See “Optimizing Your Program” for more information about optimization options.

The C Math Library (libm)

NOTE: The libM math library is obsolete. See “Overview of HP-UX Math Libraries” for details.

The C math library, libm, also supports

  • float versions of many mathematical functions

  • Degree-valued trigonometric functions

  • A group of functions and macros recommended by the IEEE standard (see Table 2-12 “HP-UX Support for IEEE Recommended Functions”), including floating-point classification macros, and some additional classification macros approved by the ISO/ANSI C committee for inclusion in the C9X draft standard

  • A group of functions required by the COSE Common API Specification (Spec 1170) and by the XPG4.2 specification

  • The fenv(5) suite, a collection of functions (approved by the ISO/ANSI C committee for inclusion in the C9X draft standard) that allow an application to manipulate the ­floating-point status register

All of these functions are defined in math.h. However, all of these functions are outside the ANSI C specification, and many of them are outside the XPG4.2 specification. If you compile in strict ANSI mode (with the -Aa option), only the declarations of functions specified by the ANSI C standard are ordinarily visible to your program. Therefore, to make the additional functions visible, do one of the following:

  • Compile with the extended ANSI option -Ae, which makes both ANSI and non-ANSI function declarations visible. This option is the default, so you need not specify it explicitly:

    cc program_name.c -lm
  • If your program does not use ANSI C features, compile in compatibility mode (with the -Ac option), which also makes both ANSI and non-ANSI function declarations visible:

    cc -Ac program_name.c -lm

    Do not use this option to compile programs that call float type math functions, because they depend on ANSI C features. See “float Type Math Functions”.

  • Compile with the basic ANSI option -Aa and use the -D option to define the macro _HPUX_SOURCE on the command line. If this macro is defined, all the function declarations are visible. (If you compile with -Ae or -Ac, this macro is defined automatically.)

    cc -Aa -D_HPUX_SOURCE program_name.c -lm
NOTE: To compile C++ programs that call nonstandard math library functions, use the +a1 option.

For a complete list of the contents of libm, see Appendix A “The C Math Library”. The functions are described in online man pages.

float Type Math Functions

Certain libm math functions are implemented as single-precision functions, accepting float arguments and returning a float result. On HP 9000 systems, the performance of single-precision and double-­precision functions is similar. If your application uses single-­precision data types heavily, you may find it convenient to use the float versions of the math functions the application calls.

The names of the float functions are the same as those of their double-­precision counterparts but with the letter f appended. (For float versions of degree-valued trigonometric functions, the f follows the d.) This naming convention accords with that specified in Section 4.13 of the ANSI C standard, "Future Library Directions."

The supplied functions are as follows:

  • cosf, sinf, tanf, cosdf, sindf, tandf

  • acosf, asinf, atanf, atan2f, acosdf, asindf, atandf, atan2df

  • coshf, sinhf, tanhf

  • expf, fabsf, fmodf, logf, log10f, log2f, powf, sqrtf, cbrtf

  • copysignf

You must compile in ANSI mode (with -Ae, the default, or with -Aa -D_HPUX_SOURCE) to use these functions. If you compile in ANSI mode, ANSI C rules are followed, and the argument is treated as a float throughout the function call. If you do not compile in ANSI mode, traditional K&R C rules are followed, the compiler generates code to promote the argument to double, and the function call either generates a linker error or produces incorrect results. Therefore, use a command line like the following:

cc program_name.c -lm

CC +a1 program_name.C -lm

Degree-Valued Trigonometric Functions

The Fortran math library defines a set of trigonometric functions whose arguments or results are specified in degrees rather than radians. These functions are also implemented in libm. The names of these functions are the same as the names of the standard versions but with the letter d appended: sind, cosd, and so on. (For float versions of degree-valued trigonometric functions, the f follows the d.)

To use these functions, compile them in any mode except strict ANSI mode (-Aa). Use either extended ANSI mode (-Ae, the default), non-ANSI mode (-Ac), or -Aa -D_HPUX_SOURCE.

Floating-Point Classification Macros

The fpclassify macro is the HP version of the class function recommended by the IEEE standard. The ANSI/ISO C committee has also approved this macro for inclusion in the C9X standard. The fpclassify macro accepts either a double or a float argument. It returns an integer value that describes the class of the argument—that is, what kind of floating-point value it is. Table 4-1 “fpclassify Values” shows the values and their meanings, which are defined in math.h.

Table 4-1 fpclassify Values

Class

Name of Macro

Normalized

FP_NORMAL

Zero

FP_ZERO

Infinity

FP_INFINITE

Denormalized

FP_SUBNORMAL

NaN

FP_NAN

 

The list of classes is exhaustive: all IEEE floating-point values fall into one of these classes. The fpclassify macro never causes an exception, regardless of the operand. It is useful, therefore, for classifying an operand without risking an exception trap.

Use the signbit macro to determine whether a value is negative or positive.

To use the fpclassify macro, compile your program in any mode except strict ANSI mode (-Aa). Use either extended ANSI mode (-Ae, the default), non-ANSI mode (-Ac), or -Aa -D_HPUX_SOURCE.

The following C function shows how you might use fpclassify and signbit to display the class and value of a double-precision number:

Example 4-3 Sample Function: print_class.c

#include <math.h>
#include <stdio.h>

typedef union {
double y;
struct {
unsigned int ym, yl;
} i;
} DBL_INT;

void print_classd(DBL_INT di)
{
int class, sign;
char *posneg[] = {"positive", "negative"};

class = fpclassify(di.y);
sign = signbit(di.y);

if (class == FP_NORMAL)
printf("%19.17g is %s normalized (%08x%08x)\n", di.y,
posneg[sign], di.i.ym, di.i.yl);
else if (class == FP_ZERO)
printf("%19.17g is %s zero (%08x%08x)\n", di.y,
posneg[sign], di.i.ym, di.i.yl);
else if (class == FP_INFINITE)
printf("%19.17g is %s infinity (%08x%08x)\n", di.y,
posneg[sign], di.i.ym, di.i.yl);
else if (class == FP_SUBNORMAL)
printf("%19.17g is %s denormalized (%08x%08x)\n", di.y,
posneg[sign], di.i.ym, di.i.yl);
else if (class == FP_NAN)
printf("%19.17g is NaN (%08x%08x), sign bit is %d\n",
di.y, di.i.ym, di.i.yl, sign);
}

Other macros that test the class of a floating-point value are

  • isinf, which tests whether a value is an infinity

  • isnan, which tests whether a value is a NaN

  • isfinite, which tests whether a value is neither infinity nor NaN

  • isnormal, which tests whether a value is normalized

See the online man pages for information about these macros.

COSE Common API Functions

The C math library provides several functions that are not specified by the ANSI C standard but that are required by the COSE Common API Specification (Spec 1170) and the XPG4.2 specification. These functions are

acosh(x)

Returns inverse hyperbolic cosine of x

asinh(x)

Returns inverse hyperbolic sine of x

atanh(x)

Returns inverse hyperbolic tangent of x

cbrt(x)

Returns cube root of x

expm1(x)

Returns exp(x) - 1

ilogb(x)

Returns the integer form of the binary exponent of the floating-point value x

log1p(x)

Returns log(1 + x)

logb(x)

Returns the exponent of x as an integer-valued ­double-precision number

nextafter(x, y)

Returns the next representable neighbor of x in the direction of y

remainder(x, y)

Returns exact floating-point remainder as defined by IEEE standard

rint(x)

Rounds x to integer-valued double-precision number, in the direction of the current rounding mode

scalb(x, n)

Returns x*(2**n), computed efficiently

To use these functions, compile your program in any mode except strict ANSI mode (-Aa). Use either extended ANSI mode (-Ae, the default), non-ANSI mode (-Ac), or -Aa -D_HPUX_SOURCE.

The fenv(5) Suite

The fenv(5) suite is a collection of functions in the C math library that allow an application to manipulate various modifiable control modes and status flags in the ­floating-point status register. Most of the functions in the fenv(5) suite have been approved by the ISO/ANSI C committee for inclusion in the C9X draft standard.

For details on how to use these functions, see Chapter 5 “Manipulating the Floating-Point Status Register” and the online man pages. The tables in Appendix A “The C Math Library” also list all of the functions.

The BLAS Library (libblas)

The Basic Linear Algebra Subroutine (BLAS) library routines perform low-level vector and matrix operations. They have been tuned for maximum performance.

The BLAS library is provided with the HP Fortran 90 and HP FORTRAN/9000 products only, but the routines in this library are callable from other languages than Fortran.

To call BLAS library routines, use the -l compile-line option to link in the libblas library. For example, the following command line links a Fortran program with the BLAS library:

f90 prog.f -lblas

To link with the library from C, you must also specify the library path name on the command line.

For more information about the BLAS library routines, see “Matrix Operations” and the HP Fortran 90 Programmer's Reference or the HP FORTRAN/9000 Programmer's Reference. In addition, online man pages for these routines are available.

NOTE: To obtain the man pages for the BLAS library, you must have /opt/fortran90/share/man in your MANPATH (or /opt/fortran/share/man). Type man blas for an overview.

The Vector Library (libvec) (Obsolete)

The vector library (provided with the HP FORTRAN/9000 product only) performs vector and matrix operations.

NOTE: This library is obsolete. It was formerly used by the FORTRAN Optimizing Preprocessor (FTNOPP). The performance benefits provided by FTNOPP are now supplied by the compiler when you use the +Ovectorize option with either C or Fortran programs at optimization level 3 or above. (See “Optimizing Your Program” for details.) The vector library is provided for compatibility reasons only, in /opt/fortran/old/lib/libvec.a.

For more information about the libvec routines, see the HP FORTRAN/9000 Programmer's Reference.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1997 Hewlett-Packard Development Company, L.P.