def divide_unsigned_clocks(dividend: int, divisor: int) -> int: """DIVU's internal clocks, from the microcode's shift-and-subtract loop. The manual gives only a maximum (UM Table 8-4). The loop: overflow is detected first; otherwise 15 steps each shift the dividend left, and a step whose shifted-out bit was 0 costs 2 more clocks, less 1 if the trial subtraction succeeded. Jorge Cwik's analysis (2005, carried in WinUAE, T2) states the same count; SST DIVU checks it on every case (T3). """ if dividend >> 16 >= divisor: return 10 half_clocks = 38 shifted_divisor = divisor << 16 for _ in range(15): carry = dividend & 0x80000000 dividend = (dividend << 1) & 0xFFFFFFFF if carry: dividend = (dividend - shifted_divisor) & 0xFFFFFFFF else: half_clocks += 2 if dividend >= shifted_divisor: dividend -= shifted_divisor half_clocks -= 1 return half_clocks * 2 def divide_signed_clocks(dividend: int, divisor: int) -> int: """DIVS's internal clocks: sign handling, then one step per quotient bit. ``dividend`` and ``divisor`` are signed. Absolute overflow (|dividend| >> 16 >= |divisor|) is found early; otherwise the unsigned loop runs on the magnitudes and each of the 15 high quotient bits that is 0 costs 2 clocks (Cwik 2005, T2; checked against the corpus, T3). """ half_clocks = 6 if dividend < 0: half_clocks += 1 if abs(dividend) >> 16 >= abs(divisor): return (half_clocks + 2) * 2 quotient = abs(dividend) // abs(divisor) half_clocks += 55 if divisor >= 0: half_clocks += -1 if dividend >= 0 else 1 for bit in range(15): if not quotient & (0x8000 >> bit): half_clocks += 1 return half_clocks * 2