popcnt

Date:

09-12-2012

NAME

_popcnt, _popcnt32, _popcnt64, POPCNT - Count the number of set bits

SYNOPSIS

C Synopsis

#include <intrinsics.h>

long _popcnt(unsigned long i);
long _popcnt32(unsigned int i);
long _popcnt64(unsigned long i);

Fortran Synopsis

POPCNT ([I=]i)
INTEGER i

IMPLEMENTATION

Cray Linux Environment (CLE)

STANDARDS

C extensions

Fortran extension

DESCRIPTION

The population count intrinsic functions return the number of bits, in the i argument, set to 1.

C

Use the _popcnt32 function on 32-bit integers and the _popcnt and _popcnt64 functions on 64-bit integers.

The compiler generates inline code to produce the result. For further details, see the Cray C and C++ Reference Manual.

Fortran

The POPCNT intrinsic function counts the number of bits set to 1. The i argument is a boolean, integer, real, or Cray pointer value. The i argument cannot be of type REAL(KIND=16).

POPCNT is an elemental function. The name of this intrinsic cannot be passed as an argument.

NOTES

A vector version of the POPCNT intrinsic function exists.

RETURN VALUES

popcnt, popcnt32, popcnt64, and POPCNT return a 64-bit integer.

C programmers can cause the functions to return 32-bit ints instead of 64-bit longs by declaring the intrinsics manually with int as the return type.

EXAMPLES

Fortran: The following section of Fortran code shows the POPCNT function used with an argument of type integer. The bit pattern of the argument and the value of the result are also given. For simplicity, a 64-bit object is used.

INTEGER (KIND=8) I1, I2
I2 = POPCNT(I1)
-----------------------------------------------------------------
| 0 | 0 | 0 | 0 | 0 | 1 | 1 | . . . | 1 | 1 | 1 | 0 | 0 | 1 | 0 |
-----------------------------------------------------------------
                                I1

The POPCNT function returns the value 6 in variable I2.

SEE ALSO

For a complete list of C intrinsic functions, see the Cray C and C++ Reference Manual.