There is some code in my kernel that uses the % and / C operators on integer types (uint64_t, with some integer literals). I realize that on x86 this likely gets compiled to non-FP instructions and is therefore ok... My concern is for other processor architectures where there may not be integer divide and modulus instructions (there are some RISC architectures with this trait... I don't remember which ones offhand).
Basically, I'm trying to avoid any use of FP code in the kernel. Does anyone see a potential problem with % and / in this regard? BTW, the code in question is my kernel's equivalent to atoi()...
/ and %: Dangerous in a portable kernel?
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
/ and %: Dangerous in a portable kernel?
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:/ and %: Dangerous in a portable kernel?
Aha! The linker has answered my question for me:
I guess I'll have to whip up some asm magic to make this work. Anybody else have this problem and some cleaner solutions to it?
Code: Select all
../../Lib/libKLib.a(TextWriter.o)(.text+0x107): In function `TextWriter_writeDecimal':
: undefined reference to `__udivdi3'
../../Lib/libKLib.a(TextWriter.o)(.text+0x15b): In function `TextWriter_writeDecimal':
: undefined reference to `__umoddi3'
../../Lib/libKLib.a(TextWriter.o)(.text+0x188): In function `TextWriter_writeDecimal':
: undefined reference to `__udivdi3'
collect2: ld returned 1 exit status
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re:/ and %: Dangerous in a portable kernel?
GCC implements 64-bit arithmetic on 32-bit architectures (via the long long type or a typedef thereof) with the helper functions the linker is complaining it can't find.
So, you either need to link in libgcc, or implement these yourself. Reimplementing these shouldn't be too hard. You could even look at a disassembly of libgcc.
So, you either need to link in libgcc, or implement these yourself. Reimplementing these shouldn't be too hard. You could even look at a disassembly of libgcc.
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:/ and %: Dangerous in a portable kernel?
I ended up solving my immediate problem by replacing writeInt64() with writeIntPtr()... in other words, no outputting 64-bit ints on 32-bit machines. I don't think this is a big deal, since the kernel will be outputting mostly machine-specific registers and pointers anyway, for diagnostic purposes.
Anyway, the bigger question still remains -- will the use of integer / and % get me into trouble on other processor architectures...? I guess the short answer is that if it will, GCC will probably be using some builtins to implement these operators and the linker will kindly let me know. Nevertheless, if anyone has any experience porting this kind of code to a non-x86 machine, I'd like to hear about your experiences...
Anyway, the bigger question still remains -- will the use of integer / and % get me into trouble on other processor architectures...? I guess the short answer is that if it will, GCC will probably be using some builtins to implement these operators and the linker will kindly let me know. Nevertheless, if anyone has any experience porting this kind of code to a non-x86 machine, I'd like to hear about your experiences...
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re:/ and %: Dangerous in a portable kernel?
Only for types that the processor cannot handle natively and thus require helper functions. You can always provide these functions if required.
In general, no.
In general, no.