Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions lib/solvers/PackInnerPartitionsSolver/findSameSidePassiveGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,28 @@ const getNetForPin = (problem: InputProblem, pinId: PinId): NetId | null => {
return null
}

/** Main-chip side a pin offset points to once the chip's rotation is applied. */
/** Rotate a declared side by a CCW quarter-turn rotation. */
const SIDE_ROTATION_ORDER: Side[] = ["x+", "y+", "x-", "y-"]
const rotateSide = (side: Side, ccwRotationDegrees: number): Side => {
const turns = ((Math.round(ccwRotationDegrees / 90) % 4) + 4) % 4
const index = SIDE_ROTATION_ORDER.indexOf(side)
return SIDE_ROTATION_ORDER[(index + turns) % 4]!
}

/**
* Main-chip side a pin sits on once the chip's rotation is applied.
*
* Prefers the pin's declared `side` — offset inference misclassifies corner
* pins (e.g. a left-edge pin near the top of a tall chip has |y| > |x|, which
* would wrongly read as "y+") — and falls back to offset inference when no
* side is declared.
*/
const getMainChipPinSide = (
offset: { x: number; y: number },
pin: { offset: { x: number; y: number }; side?: Side },
mainChipRotation: number,
): Side => {
const o = rotatePinOffset(offset, mainChipRotation)
if (pin.side) return rotateSide(pin.side, mainChipRotation)
const o = rotatePinOffset(pin.offset, mainChipRotation)
if (Math.abs(o.x) >= Math.abs(o.y)) {
if (o.x >= 0) return "x+"
return "x-"
Expand Down Expand Up @@ -164,7 +180,7 @@ export const findSameSidePassiveGroups = (
commonNodeGroups.push({
mainChipId,
side: getMainChipPinSide(
mainChipPin.offset,
mainChipPin,
mainChip.availableRotations?.[0] ?? 0,
),
passiveChipIds,
Expand Down Expand Up @@ -215,7 +231,7 @@ export const findSameSidePassiveGroups = (
mainChipRotation,
)
const side = getMainChipPinSide(
problem.chipPinMap[mainChipPinId]!.offset,
problem.chipPinMap[mainChipPinId]!,
mainChipRotation,
)
candidates.push({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, test } from "bun:test"
import { findSameSidePassiveGroups } from "lib/solvers/PackInnerPartitionsSolver/findSameSidePassiveGroups"
import { problem } from "../../pages/LayoutPipelineSolver/LayoutPipelineSolver06.page"

// https://github.com/tscircuit/matchpack/issues/15
//
// U3's decoupling caps all hang off its LEFT edge (every rail pin declares
// side "x-"), but side inference from pin offsets misclassified the corner
// pins: U3.44 sits near the top of the tall chip at offset (-1.9, 2.0), where
// |y| > |x| reads as "y+". That split the caps into an x- group and a phantom
// y+ group, so C10/C11/C7/C18 never joined the left-edge rows.
//
// The declared pin side is authoritative; offset inference is only a fallback.
test("corner pins keep their declared side when grouping same-side passives", () => {
const groups = findSameSidePassiveGroups(problem as any)

// All caps must land in x- groups — no phantom y+ group from corner pins
expect(groups.every((group) => group.side === "x-")).toBe(true)

const groupedCaps = new Set(groups.flatMap((group) => group.passiveChipIds))
for (const capId of ["C10", "C11", "C7", "C18"]) {
expect(groupedCaps.has(capId)).toBe(true)
}
})
Loading