The BOOST_PP_MUL_D macro expands to the product of its second and third arguments.  It reenters BOOST_PP_WHILE with maximum efficiency.

Usage

BOOST_PP_MUL_D(d, x, y)

Arguments

d
The next available BOOST_PP_WHILE iteration. 
x
The multiplicand of the operation.  Valid values range from 0 to BOOST_PP_LIMIT_MAG.
y
The multiplier of the operation.  Valid values range from 0 to BOOST_PP_LIMIT_MAG.

Remarks

If the product of x and y is greater than BOOST_PP_LIMIT_MAG, the result is saturated to BOOST_PP_LIMIT_MAG.
This macro is the most efficient when x is greater than or equal to y.  However, the efficiency gain is not worth actually comparing the two arguments prior to invocation.  In other words, x should be the value that is most likely to be the largest of the two operands.

See Also

Requirements

Header:  <boost/preprocessor/arithmetic/mul.hpp>

Sample Code

#include <boost/preprocessor/arithmetic/dec.hpp>
#include <boost/preprocessor/arithmetic/mul.hpp>
#include <boost/preprocessor/control/while.hpp>
#include <boost/preprocessor/tuple/elem.hpp>

#define PRED(d, data) BOOST_PP_TUPLE_ELEM(3, 0, data)

#define OP(d, data) \
   ( \
      BOOST_PP_DEC( \
         BOOST_PP_TUPLE_ELEM(3, 0, data) \
      ), \
      BOOST_PP_TUPLE_ELEM(3, 1, data), \
      BOOST_PP_MUL_D( \
         d, \
         BOOST_PP_TUPLE_ELEM(3, 2, data), \
         BOOST_PP_TUPLE_ELEM(3, 1, data) \
      ) \
   ) \
   /**/

// raise 'x' to the 'n'-th power
#define EXP(x, n) BOOST_PP_TUPLE_ELEM(3, 2, BOOST_PP_WHILE(PRED, OP, (n, x, 1)))

EXP(4, 2) // expands to 16
EXP(2, 3) // expands to 8

© Copyright Housemarque Oy 2002
© Copyright Paul Mensonides 2002

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt)