"long long" division
"long long" division
Hi all,
When I try to divide a "long long" variable by some number, I get the linker error "undefined reference to ___divdi3". So, I understand that the division has to be carried out by 4 bytes at a time. Is there any optimized way of implementing the above opearation?
Thanks in advance.
When I try to divide a "long long" variable by some number, I get the linker error "undefined reference to ___divdi3". So, I understand that the division has to be carried out by 4 bytes at a time. Is there any optimized way of implementing the above opearation?
Thanks in advance.
Re:"long long" division
Huh?
No, you shouldn't have to do anything special to do long long divisions.
One, check that your compiler is running in C99 mode. For GCC, the option is [tt]-std=c99[/tt].
Two, make sure your compiler doesn't use any builtins ([tt]-nostdlib -nodefaultlibs[/tt], possibly [tt]-fno-builtin[/tt]).
No, you shouldn't have to do anything special to do long long divisions.
One, check that your compiler is running in C99 mode. For GCC, the option is [tt]-std=c99[/tt].
Two, make sure your compiler doesn't use any builtins ([tt]-nostdlib -nodefaultlibs[/tt], possibly [tt]-fno-builtin[/tt]).
Every good solution is obvious once you've found it.
Re:"long long" division
Yea as What Solar said,gcc has provided a pretty good built-in support for long long variables.
Re:"long long" division
Thanks Solar.
I use DJGPP, gcc version 4.0.0. Also, all builtins have been suppressed. I tried to use the option -std=c99. But, still, it gives the same error mesage.
I use DJGPP, gcc version 4.0.0. Also, all builtins have been suppressed. I tried to use the option -std=c99. But, still, it gives the same error mesage.
Re:"long long" division
Perhaps the error is because builtins have been disabled. If GCC has such great builtin support for long longs, what happens when you disable builtins?? I'm thinking its a builtin that usually handles these kinds of things.
All I can suggest is google, or grep thrugh the gcc & glibc sources. Have fun!
All I can suggest is google, or grep thrugh the gcc & glibc sources. Have fun!
Re:"long long" division
Uh? I'm surprised to see that DJGPP seems to be not half as outdated a GCC version than I thought.
Anyway... it would take some testing Re GCC long long support, and I don't have a GCC at hand ATM. I'd be interested in any findings, though (so that I can rub them into the FAQ).
Anyway... it would take some testing Re GCC long long support, and I don't have a GCC at hand ATM. I'd be interested in any findings, though (so that I can rub them into the FAQ).
Every good solution is obvious once you've found it.
Re:"long long" division
I've just found the OpenBSD version of this function, and its under a modified BSD license (ie. no advertising clause).
I could email it you or you could try and find it. Its in 'src/sys/lib/libkern' in the CVS repository, which you can get by anonymous cvs or webcvs. Its named 'divdi3.c'. Theres also files for add, and, left & right shift, etc. Theres quite a few actually.
I could email it you or you could try and find it. Its in 'src/sys/lib/libkern' in the CVS repository, which you can get by anonymous cvs or webcvs. Its named 'divdi3.c'. Theres also files for add, and, left & right shift, etc. Theres quite a few actually.
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:"long long" division
iirc, functions for long long division require some library code (that should be in the "language support library" libgcc.a or something. Asking "no builtin" just prevented the linker to use that library when generating your file. IIrc too, that specific library should be safe for inclusion even in a kernel, and you could still have the benefits of "avoid anything that is host-related" through "-ffreestanding", yet i have to toy with that in real programming condition before i could validate the stuff.
Re:"long long" division
I've had this discussion before, and '-ffreestanding -fbuiltin' should be safe for use. I'm using them and havent had any problems yet.
Re:"long long" division
Hey, I tried using '-ffreestanding -fbuiltin'. It's not working. When I removed all the flags and compiled my kernel, still it throws the error, while linking. Does gcc have support for "long long" division? ???paulbarker wrote: I've had this discussion before, and '-ffreestanding -fbuiltin' should be safe for use. I'm using them and havent had any problems yet.
Re:"long long" division
Of course it does, you might need to provide the library functions though. See my earlier post.Does gcc have support for "long long" division?
Re:"long long" division
Every good solution is obvious once you've found it.
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:"long long" division
Hmmm... does that work for memcpy() as well? I'm not that interesting in trying to write my own optimized version...paulbarker wrote: I've had this discussion before, and '-ffreestanding -fbuiltin' should be safe for use. I'm using them and havent had any problems yet.
@Solar: What problems do you forsee with using builtins in a kernel? Are they licensing or technical concerns that you have?
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
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:"long long" division
afaik (in gcc3.x), the language support library (that is, libgcc.a, which you can find in /usr/lib/gcc/<version>/, but also libsupc++ afaik.) is license-friendly, linking against it doesn't taint your program to the library's license in any way. This is actually mandatory since it is linked to _any_ program GCC generates (that would mean any commercial project couldn't use GCC -- and FSF isn't *that* nasty)
Note: not _all_ parts of libgcc.a are that osdevers-friendly. Among others, _eh.o do contain pthread_xxx calls, but as long as you don't use language's exceptions handlers, that specific part shouldn't be needed (that's the nice part of static libraries: you only get what you need, not the rest )
In your case, "_divdi3.o" offers __divdi3 and requires no other part of libgcc.a ... if it still doesn't work with "-ffreestanding", try to locate your compiler's libgcc.a and link explicitly against it. That should do the trick.
Note: not _all_ parts of libgcc.a are that osdevers-friendly. Among others, _eh.o do contain pthread_xxx calls, but as long as you don't use language's exceptions handlers, that specific part shouldn't be needed (that's the nice part of static libraries: you only get what you need, not the rest )
In your case, "_divdi3.o" offers __divdi3 and requires no other part of libgcc.a ... if it still doesn't work with "-ffreestanding", try to locate your compiler's libgcc.a and link explicitly against it. That should do the trick.
Re:"long long" division
Colonel Kernel: I foresee nothing. I just mix a bag of "I think I have seen this somewhere" with a shot of xenophobia ("not invented here") and a bucket of paranoia.
Every good solution is obvious once you've found it.