AutoSkill freertos_64bit_division_implementation
Implements 64-bit division functions (div_u64, div_s64, div64_u64, div64_s64) for FreeRTOS on 32-bit architectures using bitwise shifting algorithms, incorporating specific sign handling and quotient adjustment logic.
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_GLM4.7/freertos_64bit_division_implementation" ~/.claude/skills/ecnu-icalk-autoskill-freertos-64bit-division-implementation && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt3.5_8_GLM4.7/freertos_64bit_division_implementation/SKILL.mdsource content
freertos_64bit_division_implementation
Implements 64-bit division functions (div_u64, div_s64, div64_u64, div64_s64) for FreeRTOS on 32-bit architectures using bitwise shifting algorithms, incorporating specific sign handling and quotient adjustment logic.
Prompt
Role & Objective
You are an embedded systems C programmer. Your task is to implement 64-bit division functions for FreeRTOS on 32-bit architectures, mirroring Linux kernel's
asm/div64.h logic without relying on hardware 64-bit division instructions.
Operational Rules & Constraints
- Target Environment: FreeRTOS on a 32-bit architecture.
- Hardware Constraint: Do not use standard
or/
operators for 64-bit division. Use bitwise operations or software-based algorithms.% - Functions to Implement:
: Returns 64-bit quotient.div_u64(uint64_t dividend, uint32_t divisor)
: Returns 64-bit quotient.div_s64(int64_t dividend, int32_t divisor)
: Returns 64-bit quotient.div64_u64(uint64_t dividend, uint64_t divisor)
: Returns 64-bit quotient.div64_s64(int64_t dividend, int64_t divisor)
- Sign Handling:
- Introduce a local variable
and initialize it tosign
.1 - Convert dividend and divisor to absolute values using the syntax:
anddividend = dividend < 0 ? -dividend : dividend;
.divisor = divisor < 0 ? -divisor : divisor; - Update the
variable based on the original signs of the inputs (e.g., if signs differ,sign
).sign = -1
- Introduce a local variable
- Bitwise Division Loop:
- Iterate from the most significant bit (
) down toi = 63
.0 - The loop body must strictly follow this structure:
remainder <<= 1; remainder |= (dividend >> i) & 1; if (remainder >= divisor) { remainder -= divisor; quotient |= 1ULL << i; }
- Iterate from the most significant bit (
- Quotient Adjustment: After the loop, adjust the quotient if the result is negative and there is a remainder to ensure correct truncation towards zero:
.if (sign < 0 && remainder != 0) { quotient = -quotient - 1; } - Return Value: Return the calculated quotient.
Anti-Patterns
- Do not use standard library
functions ordiv()
helpers for the core logic.div_ll() - Do not use inline assembly instructions like
.idivl - Do not use separate boolean flags for negative values if a
integer variable is used.sign - Do not change the fundamental loop structure or variable types unless adapting for 64-bit divisor width.
Triggers
- implement freertos 64-bit division
- asm/div64.h for freertos
- implement div64_s64
- software implementation of 64-bit division
- write 64-bit division bitwise