Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 8089a3f95c60cad63425b5bfdf44ecaf51cdfcffe65d8da1b2f0ae4f7493edc2
Ticket: fd1585e2a1a8f8900ea63bc2c723717f5871962b
Continued improvements to TclLog2() and friends and callers
User & Date: dgp on 2025-05-05 19:37:41
Changes

  1. icomment:
    
    Answering question about the non-support for an input of 0 to TclMSB().
    
    Since the binary representation of 0 has no set bits, it is unclear
    what the correct answer should be to the question what is the index
    of the most significant set bit.
    
    A return value of 0 is effectively an assertion that the input value 0
    is no different from the input value 1.  Far from clear that's an
    acceptable equivalence to all callers.
    
    A return value of -1 succeeds in treating an input value 0 as something
    unlike any other input.  However, most known callers of TclMSB() have the
    intent of using the return value as an index, and a negative index is not
    usually welcome.  Such callers would need to post-process the return value
    to treat -1 differently.  This is no better than expecting the caller to
    treat an input value of 0 differently before making the TclMSB() call. 
    Either way, there's a testing branch needed if the caller knows a value
    of 0 is a possibility.
    
    FWIW, it is also true that this unsupported input value reflects the
    same unsupported input value in the most efficient implementations.  The
    limitations of clzll(), etc. are just showing through.
    
  2. login: "dgp"
  3. mimetype: "text/plain"