Skip to content

Commit 6f38439

Browse files
committed
Simplify bitcontainer calculation
1 parent 9a81e44 commit 6f38439

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/core/IronPython.Modules/_ctypes/StructType.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,15 @@ private int UpdateSizeAndAlignment(INativeType cdata, int? bitCount, ref INative
451451
// as long as they fit and match the alignment
452452
int containerOffset = AlignBack(size, cdata.Alignment); // TODO: _pack
453453
int containerBitCount = (totalBitCount ?? 0) + (size - containerOffset) * 8;
454-
while (containerBitCount + bitCount > cdata.Size * 8) {
455-
// the bitfield does not fit into the container unit at this offset, try the next allowed offset
454+
if (containerBitCount + bitCount > cdata.Size * 8) {
455+
// the bitfield does not fit into the container unit at this offset, find the nearest allowed offset
456456
int deltaOffset = cdata.Alignment; // TODO: _pack
457-
containerOffset += deltaOffset;
458-
containerBitCount = Math.Max(0, containerBitCount - deltaOffset * 8);
457+
int numOffsets = Math.Max(1, (containerBitCount + bitCount.Value - 1) / (deltaOffset * 8));
458+
containerOffset += numOffsets * deltaOffset;
459+
containerBitCount = Math.Max(0, containerBitCount - numOffsets * deltaOffset * 8);
459460
}
460461
// the bitfield now fits into the container unit at this offset
462+
Debug.Assert(containerBitCount + bitCount <= cdata.Size * 8);
461463
fieldOffset = size = containerOffset;
462464
totalBitCount = containerBitCount + bitCount;
463465
lastType = cdata;

0 commit comments

Comments
 (0)