-
Notifications
You must be signed in to change notification settings - Fork 8
Add tanh() intrinsic and rev() handler #90
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
97e2d2d
Add tanh() unary intrinsic
t-kalinowski 45c35d3
Add rev() handler
t-kalinowski 00d5d28
Preserve NA logicals in rev() for bind(c) storage
t-kalinowski 93137e3
Booleanize integer-backed logical expressions
t-kalinowski 8de5547
NEWS: tanh() and rev() support
t-kalinowski efc779a
test: rev(rev()) preserves NA for bind(c) logicals
t-kalinowski fa20990
fix: preserve bind(c) logical storage when hoisting rev()
t-kalinowski dd0de4e
fix: booleanize rev() logicals in as.integer/which.max
t-kalinowski 3a623ad
test: r2f symbol attaches r metadata for bind(c) logicals
t-kalinowski e3274db
fix: keep r2f() epilogue for bind(c) logical symbols
t-kalinowski abe48ef
test: rev() preserves NA for bind(c) logical scalars
t-kalinowski 1b4426b
fix: rev() scalar bind(c) logicals preserve NA storage
t-kalinowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # r2f-rev.R | ||
| # Handler for rev() | ||
|
|
||
| r2f_handlers[["rev"]] <- function(args, scope, ..., hoist = NULL) { | ||
| stopifnot(length(args) == 1L) | ||
|
|
||
| x <- r2f(args[[1L]], scope, ..., hoist = hoist) | ||
| if (is.null(x@value)) { | ||
| stop("rev() expects a typed value", call. = FALSE) | ||
| } | ||
|
|
||
| # Scalars (incl. length-1 vectors that are lowered as scalars) reverse to self. | ||
| if (passes_as_scalar(x@value)) { | ||
| return(x) | ||
| } | ||
|
|
||
| if (x@value@rank != 1L) { | ||
| stop("rev() only supports rank 0-1 inputs", call. = FALSE) | ||
| } | ||
|
|
||
| # Fortran array sections require an array designator; hoist array expressions. | ||
| if (is.null(x@value@name)) { | ||
| tmp <- hoist$declare_tmp(mode = x@value@mode, dims = x@value@dims) | ||
| hoist$emit(glue("{tmp@name} = {x}")) | ||
| x <- Fortran(tmp@name, tmp) | ||
| } | ||
|
|
||
| base_name <- x@value@name %||% | ||
| stop("missing array name for rev()", call. = FALSE) | ||
|
|
||
| # External logical args are stored as integer(0/1) and symbol-lowered as `(x/=0)`. | ||
| # Reverse the underlying storage then booleanize the resulting section. | ||
| if (identical(x@value@mode, "logical") && logical_as_int(x@value)) { | ||
| out_val <- Variable("logical", x@value@dims) | ||
| return(Fortran( | ||
| glue("({base_name}(size({base_name}):1:-1) /= 0)"), | ||
| out_val | ||
| )) | ||
| } | ||
|
|
||
| out_val <- Variable(x@value@mode, x@value@dims) | ||
| Fortran(glue("{base_name}(size({base_name}):1:-1)"), out_val) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| test_that("rev() reverses vectors", { | ||
| fn_dbl <- function(x) { | ||
| declare(type(x = double(NA))) | ||
| rev(x) | ||
| } | ||
| expect_quick_identical( | ||
| fn_dbl, | ||
| list(as.double(1:5)), | ||
| list(as.double(c(2, -1, 3, 0))) | ||
| ) | ||
|
|
||
| fn_int <- function(x) { | ||
| declare(type(x = integer(NA))) | ||
| rev(x) | ||
| } | ||
| expect_quick_identical(fn_int, list(as.integer(c(1, 0, -3, 2)))) | ||
|
|
||
| # Logical arguments are passed via the bind(c) interface as integer storage. | ||
| fn_lgl <- function(x) { | ||
| declare(type(x = logical(NA))) | ||
| rev(x) | ||
| } | ||
| expect_quick_identical(fn_lgl, list(c(TRUE, FALSE, TRUE, FALSE))) | ||
| }) | ||
|
|
||
| test_that("rev() hoists array expressions", { | ||
| fn <- function(x) { | ||
| declare(type(x = double(NA))) | ||
| rev(x + 1) | ||
| } | ||
| expect_quick_identical(fn, list(as.double(1:6))) | ||
| }) | ||
|
|
||
| test_that("rev() is a no-op for scalars", { | ||
| fn <- function(x) { | ||
| declare(type(x = double(1))) | ||
| rev(x) | ||
| } | ||
| expect_quick_identical(fn, list(as.double(3))) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the input is a logical vector passed via the bind(c) interface, NA values are stored as
NA_LOGICAL(an integer sentinel). The rev() handler booleanizes the reversed storage with(/= 0), which turns any non‑zero value—includingNA_LOGICAL—into.true.. That meansrev(c(TRUE, NA, FALSE))will returnc(TRUE, TRUE, FALSE)instead of preserving NA. This regression is specific to the new rev() implementation for logical-as-int inputs; consider guarding NA or mapping withmerge(1, 0, ...)plus an explicit NA check.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I fixed this by making rev() on bind(c) logicals reverse the underlying integer storage (0/1/NA) without booleanizing, so NA_LOGICAL is preserved on return (commit 00d5d28). To keep masks/compound expressions correct, I added booleanization at logical use sites for non-symbol expressions like rev(m) (ifelse, logical ops, pack/masks, reduction masks) and added stress tests covering these combinations (commit 93137e3). NEWS updated in 8de5547.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up: added a regression test for nested rev() on external logicals with NA (commit efc779a) and fixed rev() hoisting to preserve bind(c) logical integer storage by propagating logical_as_int into the temporary (commit fa20990). This prevents NA_LOGICAL from being coerced to .true. in rev(rev(m)).