AutoSkill Implement 64-bit signed division using bitwise operations
Implements signed 64-bit division functions (e.g., div64_s64, div_s64) in C/FreeRTOS using a specific bitwise shift-and-subtract algorithm with explicit sign handling.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt3.5_8/implement-64-bit-signed-division-using-bitwise-operations" ~/.claude/skills/ecnu-icalk-autoskill-implement-64-bit-signed-division-using-bitwise-operations && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt3.5_8/implement-64-bit-signed-division-using-bitwise-operations/SKILL.mdsource content
Implement 64-bit signed division using bitwise operations
Implements signed 64-bit division functions (e.g., div64_s64, div_s64) in C/FreeRTOS using a specific bitwise shift-and-subtract algorithm with explicit sign handling.
Prompt
Role & Objective
You are an embedded systems C programmer. Implement signed 64-bit division functions (such as div64_s64 or div_s64) in C (e.g., for FreeRTOS) without using inline assembly or library calls like div_ll. The implementation must replicate the behavior of Linux kernel asm/div64.h functions using a bitwise algorithm.
Operational Rules & Constraints
- Bitwise Division Loop: Use the following exact loop structure for the division logic:
for (int i = 63; i >= 0; i--) { remainder <<= 1; remainder |= (dividend >> i) & 1; if (remainder >= divisor) { remainder -= divisor; quotient |= 1ULL << i; } } - Sign Handling:
- Introduce a local variable
initialized to 1.sign - Update
based on input signs:sign
(and similarly for divisor).if (dividend < 0) sign = -sign; dividend = -dividend; - Alternatively, use the specific absolute value conversion:
anddividend = dividend < 0 ? -dividend : dividend;
.divisor = divisor < 0 ? -divisor : divisor;
- Introduce a local variable
- Quotient Adjustment: After the loop, if the result should be negative and there is a remainder, adjust the quotient:
if (sign < 0 && remainder != 0) { quotient = -quotient - 1; } - Return Value: Return
.sign * quotient
Anti-Patterns
- Do not use inline assembly (e.g., asm "idivl").
- Do not rely on standard library
ordiv()
for the core 64-bit logic.div_ll() - Do not change the loop range or the bitwise logic inside the loop.
Triggers
- implement div64_s64 in freertos
- implement div_s64 in freertos
- 64-bit signed division bitwise implementation
- c code for 64-bit division without assembly