Skip to content

Commit 490bddc

Browse files
committed
Fix problem in wrap type optimisation
1 parent 388d7a9 commit 490bddc

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

modules/compiler/src/transformations/cmaj_ReplaceWrapTypes.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ struct AddWrapFunctions : public AST::NonParameterisedObjectVisitor
174174
if (auto constIndex = createConstantWrappedIndex (index, isClamp, size))
175175
return *constIndex;
176176

177-
if (! isClamp && choc::math::isPowerOf2 (size))
178-
return AST::createBinaryOp (index.context, AST::BinaryOpTypeEnum::Enum::bitwiseAnd,
179-
AST::createCastIfNeeded (index.context.allocator.int32Type, AST::castToRef<AST::ValueBase> (index)),
180-
index.context.allocator.createConstantInt32 (static_cast<int32_t> (size - 1)));
177+
// if (! isClamp && choc::math::isPowerOf2 (size))
178+
// return AST::createBinaryOp (index.context, AST::BinaryOpTypeEnum::Enum::bitwiseAnd,
179+
// AST::createCastIfNeeded (index.context.allocator.int32Type, AST::castToRef<AST::ValueBase> (index)),
180+
// index.context.allocator.createConstantInt32 (static_cast<int32_t> (size - 1)));
181181

182182
auto& function = getOrCreateWrapOrClampFunction (isClamp, size);
183183
return AST::createFunctionCall (index.context, function, index);
@@ -218,6 +218,17 @@ struct AddWrapFunctions : public AST::NonParameterisedObjectVisitor
218218
void createWrapFunction (AST::ScopeBlock& block, AST::VariableReference& param, AST::ConstantValueBase& size)
219219
{
220220
auto& context = block.context;
221+
222+
if (choc::math::isPowerOf2 (*size.getAsInt32()))
223+
{
224+
auto& value = AST::createBinaryOp (context, AST::BinaryOpTypeEnum::Enum::bitwiseAnd,
225+
param,
226+
allocator.createConstantInt32 (*size.getAsInt32() - 1));
227+
228+
AST::addReturnStatement (block, value);
229+
return;
230+
}
231+
221232
auto& nModSize = AST::createBinaryOp (context, AST::BinaryOpTypeEnum::Enum::modulo, param, size);
222233
auto& x = AST::createLocalVariableRef (block, "x", nModSize);
223234

tests/language_tests/cmaj_test_arithmetic_ops.cmajtest

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,3 +1529,25 @@ processor Test [[ main ]]
15291529
advance();
15301530
}
15311531
}
1532+
1533+
1534+
## testConsole ("15")
1535+
1536+
processor Test [[ main ]]
1537+
{
1538+
output stream float out;
1539+
1540+
void main()
1541+
{
1542+
const int bufferLength = 16;
1543+
1544+
wrap<bufferLength> writeIndex;
1545+
wrap<bufferLength> readIndex;
1546+
int32 offset = 1;
1547+
1548+
readIndex = writeIndex - wrap<bufferLength>(offset);
1549+
1550+
console <- readIndex;
1551+
advance();
1552+
}
1553+
}

0 commit comments

Comments
 (0)