0

I am running Tcl 8.4.9. I'm dealing with 64-bit address and need to perform some arithmetic operations on 64-bit address. I used the expr command but it returns a negative result. I do not want to upgrade Tcl version is there any other alternative for it??

set addr 0xffff00001000000
set offset 0x01

set newaddr [expr {$addr + $offset}]

if {$newaddr < 0} {
    puts "Less than ZERO"
}

how to overcome such issues is there any other command to do arithmetic operations?

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • 1
    Canonically, the fix for this is to upgrade to a supported version where this was all fixed. 8.4 was weird in this area (8.3 simply didn't work at all with 64 but values), and 8.5 cleared it up a lot. You might try using the `wide()` function around arguments to operators to see if that helps. On the other hand, that value *is* negative in 8.4 as that uses *signed* 64 bit math... – Donal Fellows Jan 15 '20 at 06:38
  • i observed tcl handle 64 bit signed integer well but many address are beyond this so i looking some way to work with 64 bit unsigned integer. – satveer singh Jan 15 '20 at 08:47
  • 2
    Maybe use `math::bignum` from tcllib? I think it works on tcl versions that old. – Shawn Jan 15 '20 at 15:01

1 Answers1

1

The math::bignum library from tcllib is listed as having a minimum version requirement of 8.4. So you should be able to use it (Though updating to 8.6 gives you a lot more bonuses than being able to use large integers).

Example (Using an interactive tclsh repl session):

% package require math::bignum
3.1.1
% set addr [::math::bignum::fromstr 0xffff00001000000]
bignum 0 0 256 61440 4095
% set offset [::math::bignum::fromstr 0x01]
bignum 0 1
% set newaddr [::math::bignum::add $addr $offset]
bignum 0 1 256 61440 4095
% puts [::math::bignum::tostr $newaddr 16]
ffff00001000001

Compared to tcl 8.6 native math:

% set addr 0xffff00001000000
0xffff00001000000
% set offset 0x01
0x01
% set newaddr [expr {$addr + $offset}]
1152903912437579777
% puts [format %x $newaddr]
ffff00001000001

Same non-negative result.

Shawn
  • 47,241
  • 3
  • 26
  • 60