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

Added cast for signed integers. #3623

Merged
merged 1 commit into from
Oct 10, 2023

Conversation

orizi
Copy link
Collaborator

@orizi orizi commented Jul 6, 2023

Stack:

⚠️ Part of a stack created by spr. Do not merge manually using the UI - doing so may have unexpected results.


This change is Reviewable

@orizi orizi requested a review from spapinistarkware July 6, 2023 14:41
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 6ff94d1 to 8f69581 Compare July 6, 2023 16:51
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/fce91b1f branch 2 times, most recently from 881a43d to 67af28e Compare July 6, 2023 17:34
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 8f69581 to 0fe46bc Compare July 6, 2023 17:34
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/fce91b1f branch from 67af28e to cbeda9e Compare July 6, 2023 18:02
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 0fe46bc to 8a74bff Compare July 6, 2023 18:02
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/fce91b1f branch from cbeda9e to bd9f5c7 Compare July 6, 2023 18:37
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch 3 times, most recently from 918bbe4 to 59681b0 Compare July 9, 2023 17:44
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/fce91b1f branch 2 times, most recently from 5601cda to e3e43e2 Compare July 10, 2023 03:27
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 59681b0 to 324274e Compare July 10, 2023 03:27
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 324274e to 8d98a4c Compare July 24, 2023 10:19
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/fce91b1f branch from e3e43e2 to 35333c6 Compare July 24, 2023 10:19
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 8d98a4c to 2b696b0 Compare July 24, 2023 10:48
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/fce91b1f branch from 35333c6 to 5326fe3 Compare July 24, 2023 10:48
Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 2 of 7 files reviewed, 20 unresolved discussions (waiting on @orizi and @spapinistarkware)


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 114 at r6 (raw file):

Previously, orizi wrote…

indeed - fixing.

Is it tested?


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 121 at r7 (raw file):

        if cast_type.overflow_above || cast_type.overflow_below {
            // Finding if the detected possible overflow is not actually possible, but a backwards
            // compatibilty based one. TODO(orizi): Remove this check after the

Move TODO to a separate line.


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 121 at r7 (raw file):

        if cast_type.overflow_above || cast_type.overflow_below {
            // Finding if the detected possible overflow is not actually possible, but a backwards
            // compatibilty based one. TODO(orizi): Remove this check after the

Suggestion:

            // Finding if the detected possible overflow is not actually possible, but a backward
            // compatibility based one. TODO(orizi): Remove this check after the

crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 123 at r7 (raw file):

            // compatibilty based one. TODO(orizi): Remove this check after the
            // backwards compatibility is removed at next major sierra version.
            if from_info.signed || to_info.signed || from_info.nbits != to_info.nbits {

Another option is let same_type = from_info == to_info and if (cast_type.overflow_above || cast_type.overflow_below) && !same_type {

Suggestion:

            let backward_compatibility_exception = !from_info.signed && !to_info.signed && from_info.nbits == to_info.nbits;
            if !backward_compatibility_exception {

Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 4 files at r7.
Reviewable status: 2 of 7 files reviewed, 19 unresolved discussions (waiting on @orizi and @spapinistarkware)


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 121 at r7 (raw file):

Previously, liorgold2 wrote…

Move TODO to a separate line.

Never mind.

@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from d9983bb to 38995da Compare October 2, 2023 11:40
Copy link
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 2 of 7 files reviewed, 18 unresolved discussions (waiting on @liorgold2 and @spapinistarkware)


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 114 at r6 (raw file):

Previously, liorgold2 wrote…

Is it tested?

it is - which confuses me on how this did not fail.


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 121 at r7 (raw file):

        if cast_type.overflow_above || cast_type.overflow_below {
            // Finding if the detected possible overflow is not actually possible, but a backwards
            // compatibilty based one. TODO(orizi): Remove this check after the

Done.


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 123 at r7 (raw file):

Previously, liorgold2 wrote…

Another option is let same_type = from_info == to_info and if (cast_type.overflow_above || cast_type.overflow_below) && !same_type {

since the comment in needed anyway, and after the shortened version - i don't think the extra var is needed.

Copy link
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 2 of 7 files reviewed, 18 unresolved discussions (waiting on @liorgold2 and @spapinistarkware)


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 114 at r6 (raw file):

Previously, orizi wrote…

it is - which confuses me on how this did not fail.

oh - got confused with downcast test - added one for upcast.

@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 38995da to 97e3500 Compare October 2, 2023 11:56
Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 5 files at r8, 1 of 2 files at r9.
Reviewable status: 2 of 7 files reviewed, 12 unresolved discussions (waiting on @orizi and @spapinistarkware)


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 114 at r6 (raw file):

Previously, orizi wrote…

oh - got confused with downcast test - added one for upcast.

Thanks


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 48 at r7 (raw file):

Previously, orizi wrote…

indeed the fmt.

Move TODO to a separate line.
If needed add a blank line between. Seems weird that the formatter is intervening this way.


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 121 at r9 (raw file):

        if cast_type.overflow_above || cast_type.overflow_below {
            // Finding if the detected possible overflow is not actually possible, but a backward
            // compatibility based one (as smae type can have no overflow).

Suggestion:

           // compatibility based one (as same type can have no overflow).

Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 5 files at r8, 1 of 2 files at r9, all commit messages.
Reviewable status: 5 of 7 files reviewed, 9 unresolved discussions (waiting on @orizi and @spapinistarkware)

Copy link
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 5 of 7 files reviewed, 9 unresolved discussions (waiting on @liorgold2 and @spapinistarkware)


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 48 at r7 (raw file):

Previously, liorgold2 wrote…

Move TODO to a separate line.
If needed add a blank line between. Seems weird that the formatter is intervening this way.

Done.


crates/cairo-lang-sierra/src/extensions/modules/casts.rs line 121 at r9 (raw file):

        if cast_type.overflow_above || cast_type.overflow_below {
            // Finding if the detected possible overflow is not actually possible, but a backward
            // compatibility based one (as smae type can have no overflow).

Done.

@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 97e3500 to 45ad990 Compare October 2, 2023 16:04
Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r10, all commit messages.
Reviewable status: 5 of 7 files reviewed, 8 unresolved discussions (waiting on @orizi and @spapinistarkware)


crates/cairo-lang-sierra-to-casm/src/invocations/casts.rs line 56 at r10 (raw file):

        }
        CastType { overflow_above: false, overflow_below: true } => {
            add_downcast_overflow_below(&mut casm_builder, value, range_check, &to_values.min)

Add an assert that from type is unsigned. Add an assumption to the documentation of add_downcast_overflow_below.

Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 5 files at r8.
Reviewable status: 6 of 7 files reviewed, 8 unresolved discussions (waiting on @orizi and @spapinistarkware)


corelib/src/test/integer_test.cairo line 940 at r10 (raw file):

}

/// Checks if `a` is trivially castable to `B` and that `b` is castable to `A`, with the same value.

Fix documentation.


corelib/src/test/integer_test.cairo line 967 at r10 (raw file):

    OfAOnly,
    OfBOnly,
}

Suggestion:

/// Describes the type of bound (min/max) in a cast.
///
/// For example, in a `u8 -> u16` cast, the lower bound is equal, and the upper bound
/// of `A` is inside `B`.
#[derive(Drop)]
enum BoundConfig {
    /// The two bounds have the same value.
    Equal,
    /// The bound of A is inside the domain of B.
    AInsideB,
    /// The bound of B is inside the domain of A.
    BInsideA,
}

corelib/src/test/integer_test.cairo line 969 at r10 (raw file):

}

/// Checks that casting from `A` to `B` are valid around the bounds.

or "as-intended"

Suggestion:

/// Checks that castings from `A` to `B` are correct around the bounds.

@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 45ad990 to 18d46f1 Compare October 3, 2023 07:54
Copy link
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 5 of 7 files reviewed, 8 unresolved discussions (waiting on @liorgold2 and @spapinistarkware)


corelib/src/test/integer_test.cairo line 940 at r10 (raw file):

Previously, liorgold2 wrote…

Fix documentation.

Done.


corelib/src/test/integer_test.cairo line 967 at r10 (raw file):

    OfAOnly,
    OfBOnly,
}

Done.


corelib/src/test/integer_test.cairo line 969 at r10 (raw file):

Previously, liorgold2 wrote…

or "as-intended"

Done.


crates/cairo-lang-sierra-to-casm/src/invocations/casts.rs line 56 at r10 (raw file):

Previously, liorgold2 wrote…

Add an assert that from type is unsigned. Add an assumption to the documentation of add_downcast_overflow_below.

Done.

Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 2 files at r11, all commit messages.
Reviewable status: 6 of 7 files reviewed, 7 unresolved discussions (waiting on @spapinistarkware)

Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 2 files at r11.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @orizi and @spapinistarkware)


corelib/src/test/integer_test.cairo line 1013 at r11 (raw file):

/// Checks that castings from `SubType` to `SuperType` are correct around the bounds, where
/// `SubType` is strictly contained in `SuperType`.

Suggestion:

/// `SubType` is strictly contained (in both bounds) in `SuperType`.

Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Dismissed @spapinistarkware from a discussion.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @orizi)

@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch 2 times, most recently from 6ed4910 to 86c192f Compare October 4, 2023 14:32
@orizi orizi changed the base branch from main to sierra-minor-update October 10, 2023 08:13
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 86c192f to 39b81d4 Compare October 10, 2023 08:13
@orizi orizi enabled auto-merge October 10, 2023 08:13
@orizi orizi force-pushed the pr/orizi/orizi/signed-ints/c56d4b74 branch from 39b81d4 to bcfa44c Compare October 10, 2023 08:51
Copy link
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 3 files at r12, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @orizi)

@orizi orizi added this pull request to the merge queue Oct 10, 2023
Merged via the queue into sierra-minor-update with commit af284d4 Oct 10, 2023
@orizi orizi deleted the pr/orizi/orizi/signed-ints/c56d4b74 branch October 10, 2023 11:09
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.

3 participants