Intel(R) Array Building Blocks (Intel(R) ArBB) provides control flow mechanisms such as if statements and while loops similar to those provided by C++.
You can use the Intel(R) ArBB control flow statements to insert control flow such as loops and conditionals into your functions that depends on the values of Intel(R) ArBB types such as scalars and dense containers.
In general the control flow statements provided mimic those of standard C++. They differ from their C++ equivalents in the following specific ways:
do
/while
loops, Intel(R) ArBB provides _do/_until loops which invert the meaning of the loop condition.switch
or goto
statements.If statements allow you to conditionally execute a block of code using the _if, _else_if, _else and _end_if. The following example illustrates how you can use if statements:
f32 x = ...; f32 result; // Start an if statement _if (x > 1) { // This code will execute if x is greater than one. result = x; } _else_if (x < 0) { // This code will execute if x is less than zero. result = x * x * x; } _else { // This code will execute if x is between zero and one. result = -x; _end_if;
While loops, for loops, and do/until loops let you iterate over some condition, repeatedly executing some block of code.
The following example shows how you can use the three different types of loops:
// Initialize i to zero, and keep incrementing until it reaches a million. _for(i32 i = 0, i < 1000000; ++i) { // This code will be execute a million times. some_function(i); } _end_for; f32 x = 100.0f; // Repeat the loop body until x is no longer greater than one. _while (x > 1.0f) { x = x * 0.5f; } _end_while; i32 y = 0; // Always execute the loop body at least once, then repeat until y is greater than ten. _do { y++; } _until (y > 10);
The _break and _continue statements work like their counterparts from C++, but apply to the innermost Intel(R) ArBB loop. They can be used inside of any type of loop, and can be nested in _if statements.
The following example shows how you can use _continue and _break:
f32 x = ...; _for(i32 i = 0; i < 100; ++i) { _if (i % 2 == 0) { // Skip even iterations _continue; } _end_if; x = x * (1.0f + 1.0f/f32(i)); _if (x > 10.0f) { // Stop the loop completely if x exceeds ten. _break; } _end_if; } _end_for;
Defines | |
#define | _if(cond) |
Starts a block of code that will only execute if cond is true . | |
#define | _else_if(cond) |
Starts a block of code that will only execute if cond is true and conditions for all previous if statements at the same nesting level are false . | |
#define | _else |
Starts a block of code that will only execute if all conditions for any preceding _if and _else statements at the same nesting level are false . | |
#define | _end_if |
Ends a chain of _if/_else_if/_else statements. | |
#define | _while(cond) |
Starts a loop that will repeat as long as cond is true . | |
#define | _end_while |
Ends the current _while loop body. | |
#define | _for(init, cond, step) |
Starts a loop that will evaluate init , then loop as long as cond is true , running step after each iteration. | |
#define | _end_for |
Ends the current _for loop body. | |
#define | _break |
Ends the current innermost loop. | |
#define | _continue |
Continues to the next iteration of the current innermost loop. | |
#define | _do |
Starts a _do/_until loop. | |
#define | _until(cond) |
Ends a _do/_until loop and causes the loop to repeat until cond is true . |
#define _if | ( | cond | ) |
{{{{ \ ARBB_CPP_NS::detail::if_stack().push(true); \ ARBB_CPP_NS::detail::if_stack().top().begin(cond); \ if (ARBB_CPP_NS::detail::capturing() || \ ARBB_CPP_NS::detail::if_stack().top().if_value()) {{
Starts a block of code that will only execute if cond
is true
.
Definition at line 30 of file control_flow.hpp.
#define _else_if | ( | cond | ) |
}} \
ARBB_CPP_NS::detail::if_stack().top().end(); \
ARBB_CPP_NS::detail::if_stack().top().begin_else_if(cond); \
if (ARBB_CPP_NS::detail::capturing() || \
ARBB_CPP_NS::detail::if_stack().top().if_value()) {{
Starts a block of code that will only execute if cond
is true
and conditions for all previous if statements at the same nesting level are false
.
Definition at line 40 of file control_flow.hpp.
#define _else |
}} \
ARBB_CPP_NS::detail::if_stack().top().add_else(); \
if (ARBB_CPP_NS::detail::capturing() || \
ARBB_CPP_NS::detail::if_stack().top().else_value()) {{
Starts a block of code that will only execute if all conditions for any preceding _if and _else statements at the same nesting level are false
.
Definition at line 50 of file control_flow.hpp.
#define _end_if |
}} \ ARBB_CPP_NS::detail::if_stack().top().end(); \ ARBB_CPP_NS::detail::if_stack().pop(); \ }}}}
Ends a chain of _if/_else_if/_else statements.
Definition at line 57 of file control_flow.hpp.
#define _while | ( | cond | ) |
{{{{ \ ARBB_CPP_NS::detail::loop_stack().push(true); \ ARBB_CPP_NS::detail::loop_stack().top().while_begin(); \ if (ARBB_CPP_NS::detail::capturing()) { \ ARBB_CPP_NS::detail::loop_stack().top().while_cond(cond); \ } \ while ((ARBB_CPP_NS::detail::loop_stack().top().first_iteration() && \ (ARBB_CPP_NS::detail::capturing() || \ ARBB_CPP_NS::value(ARBB_CPP_NS::boolean(cond)))) \ || (ARBB_CPP_NS::detail::capturing() == false && \ ARBB_CPP_NS::value(ARBB_CPP_NS::boolean(cond)))) \ {{
Starts a loop that will repeat as long as cond
is true
.
Definition at line 65 of file control_flow.hpp.
#define _end_while |
}} \ ARBB_CPP_NS::detail::loop_stack().top().end_while(); \ ARBB_CPP_NS::detail::loop_stack().pop(); \ }}}}
Ends the current _while loop body.
Definition at line 80 of file control_flow.hpp.
#define _for | ( | init, | |||
cond, | |||||
step | ) |
{{{{ \ ARBB_CPP_NS::detail::loop_stack().push(true); \ ARBB_CPP_NS::detail::loop_stack().top().for_begin(); \ init; \ if (ARBB_CPP_NS::detail::capturing()) { \ ARBB_CPP_NS::detail::loop_stack().top().for_end_init(); \ { \ ARBB_CPP_NS::boolean c = ARBB_CPP_NS::detail::accept_empty_cond(cond); \ ARBB_CPP_NS::detail::loop_stack().top().for_end_cond(c); \ } \ step; \ ARBB_CPP_NS::detail::loop_stack().top().for_end_step(); \ } \ bool forever = true; \ while (forever) {{ \ if (!ARBB_CPP_NS::detail::capturing()) { \ if (!ARBB_CPP_NS::detail::loop_stack().top().first_iteration()) { \ step; \ } \ if (!value(ARBB_CPP_NS::detail::accept_empty_cond(cond))) { \ break; \ } \ }
Starts a loop that will evaluate init
, then loop as long as cond
is true
, running step
after each iteration.
Definition at line 88 of file control_flow.hpp.
#define _end_for |
if (ARBB_CPP_NS::detail::capturing()) { \ break; \ } \ }} \ ARBB_CPP_NS::detail::loop_stack().top().for_end(); \ ARBB_CPP_NS::detail::loop_stack().pop(); \ }}}}
Ends the current _for loop body.
Definition at line 114 of file control_flow.hpp.
#define _break |
{ \ if (ARBB_CPP_NS::detail::capturing()) { \ arbb_break(ARBB_CPP_NS::detail::function::current(), \ ARBB_CPP_NS::detail::throw_on_error_details()); \ } else { \ break; \ } \ }
Ends the current innermost loop.
Definition at line 124 of file control_flow.hpp.
#define _continue |
{ \ if (ARBB_CPP_NS::detail::capturing()) { \ arbb_continue(ARBB_CPP_NS::detail::function::current(), \ ARBB_CPP_NS::detail::throw_on_error_details()); \ } else { \ continue; \ } \ }
Continues to the next iteration of the current innermost loop.
Definition at line 135 of file control_flow.hpp.
#define _do |
{{{{ \ _while (true) \ {{
Definition at line 146 of file control_flow.hpp.
#define _until | ( | cond | ) |
_if (cond) { \ _break; \ } _end_if; \ }} \ _end_while; \ }}}}
Ends a _do/_until loop and causes the loop to repeat until cond
is true
.
Definition at line 153 of file control_flow.hpp.
Copyright © 2010, Intel Corporation. All rights reserved.