Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tohil tclobj integer math always at 64 bits; Faster Python-to-Tcl numeric conversions; TclProcs now return tclobj by default #75

Merged
merged 9 commits into from
Nov 23, 2021

Conversation

lehenbauer
Copy link
Collaborator

  • Perform all integer operations using Tohil tclobj objects using 64-bit integers, even on 32-bit machines. (It was already 64-bits on 64-bit machines.)
  • When creating a new Tcl numeric object from a numeric Python object, we now do so much more efficiently (see below).
  • Make TclProcs default to returning tclobj data type instead of string. (Fixes issue TclProcs default to returning str but call defaults to tclobj #72)
  • Expanded number ranges for hypothesis framework testing of tclobj math.

When creating a new Tcl object from a numeric Python object, previously tohil always converted it to a string, which thereby supported both Tcl and Python's arbitrary precision arithmetic, but would be slower than using access functions that worked on machine-native numeric types when those types were large enough to handle the numbers being represented.

Tohil, now, if the Python object is an integer, tries PyLong_AsLongAndOverflow to obtain it as a C long, and if it works without overflow, uses Tcl_NewLongObj to create the Tcl equivalent. If it overflowed, indicating the number is too large for a long, Tohil tries PyLong_AsLongLongAndOverflow, and if that doesn't overflow, tohil creates and returns a new wide Tcl integer through Tcl_NewWideIntObj.

Likewise if the Python object being converted is floating point, it is brought into Tcl using PyFloat_AsDouble and Tcl_NewDoubleObj.

Now, only if the number is wider than a long long, or complex, is it converted to a string for use by Tcl.

...on 32-bit machines like current raspberry pi
When creating a new Tcl object from a Python object,  and the Python object was numeric, tohil always converted it to a string, which thereby supported both Tcl and Python's arbitrary precision arithmetic, but would be slower than using access functions that worked on machine-native numeric types.

Tohil, now, if the python object is a number, tries PyLong_AsLongAndOverflow to convert to long and if it works without overflow, returns a Tcl_NewLongObj.

If it overflowed, tohil tries PyLong_AsLongLongAndOverflow, and if that doesn't overflow, tohil returns a new wide integer through Tcl_NewWideIntObj.

Likewise if the number is floating point, it is converted through PyFLoat_AsDouble and Tcl_NewDoubleObj.

Now, only if the number is wider than a long long, or complex, is it converted to a string for Tcl.
also expand hypothesis number ranges on tclobj math tests
@lehenbauer lehenbauer merged commit 45cf99b into main Nov 23, 2021
@lehenbauer lehenbauer deleted the long-long branch November 23, 2021 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant