Overview
| Artifact ID: | b83626df505ae3a654557f63f93f307cc0708f18 |
|---|---|
| Ticket: | 699060ffffffffffffffffffffffffffffffffff
format %08x 32/64bit confusion |
| User & Date: | srwarren on 2003-04-19 01:49:18 |
Changes
- icomment:
Logged In: YES user_id=728162 Donal, Some more feedback on this issue now I've investigated things a little further. I assume that your most recent response indicates that you've updated the format command to use GET_WIDE_OR_INT internally to convert any strings to integers, so that it uses the "appropriate" internal representation. I say this after looking at how TclExecuteByteCode does the same thing for e.g. INST_ADD. Then, the format code will look at what type that macro created, and format based on that, without changing the internal representation based on the format specification? If so, will the useWide flag be used for %d/%x etc. any more? I'm not sure what it would do except change default precision, which wouldn't make sense, since the default would just be to print the entire number? More general comments: I'm assuming GET_WIDE_OR_INT is intended to be used throughout the Tcl code where conversions from string to integer are required, and that the intent of that function is to store the converted result into the "appropriate" internal rep. On architectures with just long, it'll use that. On architectures where long and wide are different, it'll use the smallest type that can represent the converted value correctly. I'm still unclear why having both TclIntType and TclWideIntType is a good idea. Your previous responses state that updating format to always convert to wides would cause problems. Can you expand on this? I would have thought the best implementation would have been something like: #if long_is_native_64_bit_type typedef long TclIntType; #else if something_else_provides_64_bits typedef something_else TclIntType #else typedef int TclIntType #endif That way, there's only ever one internal representation on integers, that always provides the maximal "capacity" that the underlying CPU/toolchain/OS supports. There appears to be a problem with the current design where multiple types are used. If I have two variables setup as shown below: set a 0x7fffffff set b 0x80000000 then if I interpret these as integers somewhere, the internal rep for a becomes TclIntType, whereas the internal rep for b becomes TclWideIntType. Now, there's no problem with this from the point of view of a Tcl script writing provided this is completely transparent to them. However, there are subtle side-effects that are visible from Tcl scripts. For example, see the following script: -------------------- set a 0x7fffffff set b [expr $a + 1] set c 0x80000000 puts "b: [format %d $b] [format %ld $b] [format %x $b] [format 0x%lx $b]" puts "c: [format %d $c] [format %ld $c] [format %x $c] [format 0x%lx $c]" -------------------- On my system (same as platform info in a previous response below), this produces the output below (using a copy of Tcl without the hack I mentioned in a previous response to this bug report) -------------------- b: -2147483648 -2147483648 80000000 0xffffffff80000000 c: -2147483648 2147483648 80000000 0x80000000 -------------------- Given that I didn't tell Tcl which storage size to use (I don't think there's any way to do that right?) and given I know Tcl is capable of storing 64bit integers on my platform, I would expect both b and c to contain the exact same value, or at least appear that way in any Tcl operation, such as formatting or equality tests. I see three possible fixes for this integer overflow problem.: a) Update INST_ADD (and much other code) to cover all the corner cases requiring promotion of the internal type. This would probably be a lot of work - complicated and error-prone. b) Update all Tcl internal math to use wides, then back-convert to int/long if it'll fit on architectures where they're different. I'm not sure what the point of having two types would be then. c) Get rid of the two types and just have one. On top of this, the Tcl code has a huge amount of conditional code to support the two different internal reps of integer values. The code could be *drastically* simpler with a single maximal-size representation - fewer bugs and easier maintenance! From a general philosphical perspective on how/why Tcl deals with platforms where long isn't 64bit but something else is, can you comment on all this so I understand why everything is the way it is, and if there's anything I can do to write my Tcl code so it behaves more like I expect it to in the code sample above. Having just found TIP72, I see there is some supposed need for a 32-bit type to remain for backwards compatibility. However, I don't know that anything ever stated that Tcl integers were categorically 32-bit before this TIP was implemented. Indeed, the code looks like integers were 32-bit or 64-bit depending on what the underlying CPU/toolchain/OS supported as "long". As such, I contend that any extensions etc. that rely on int being 32-bit are broken and I would rather see *that* code be fixed, instead of massively complicate the Tcl code to continue supporting this incorrect extension coding. I don't honestly see how Tcl can be easily 100% fixed with the current dual-type structure - and even if it is fixed now, it's just too easy to accidentally introduce more problems of this exact same type in the future. Thanks for any comments!
- login: "srwarren"
- mimetype: "text/plain"
- username: "srwarren"