Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:00:12+00:00 2026-05-29T09:00:12+00:00

Recently, we discovered odd behavior in some old code. This code has worked for

  • 0

Recently, we discovered odd behavior in some old code. This code has worked for ages, but broke on some platform (XBox 360, PowerPC) with compiler optimizations turned on max. Usually, I’d suspect undefined behavior.

Code looks roughly like this:

#include <stdint.h>
uint32_t sign_extend16(uint32_t val)
{
   return (int32_t)(int16_t)val;
}

It’s part of an emulator so the operation in question shouldn’t be too strange.
Normally, I’d expect this to only consider the lower 16-bits and sign-extend that to 32-bits.
Apparently, this was the behavior it had for ages. On x86_64, GCC gives me this result:

0000000000000000 <sign_extend16>:
   0:   0f bf c7                movswl %di,%eax
   3:   c3                      retq

However, from what I could understand of the standard, converting an unsigned to a signed is not defined should it not be possible to represent the value of the unsigned one with the signed type.

Could it then be possible for the compiler to assume that the unsigned value would have to be in the range of [0, 32767], as any other value would be undefined? In that case, a cast to int16_t and yet another cast to int32_t would do nothing. In this case, would it be legal for the compiler to translate the code to a simple move?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:00:13+00:00Added an answer on May 29, 2026 at 9:00 am

    A conversion between two integer types is never undefined behavior.

    But some integer conversions are implementation defined.

    On integer conversions C says:

    (C99, 6.3.1.3p3) “Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.”

    what does gcc on this case is documented here:

    http://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html

    “For conversion to a type of width N, the value is reduced modulo 2^N to be within range of the type; no signal is raised”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently discovered that some functionality has been changed drastically in the DataGrid since
I recently came across some odd behavior that involves Java's regex engine. When writing
I recently inherited some code that someone else had written. I discovered that everywhere
Having recently discovered this method of development, I'm finding it a rather nice methodology.
I recently discovered that std::strstream has been deprecated in favor of std::stringstream . It's
I recently discovered an issue where people using BB Code to enter links are
This may seem like an odd question, but I need to find a closed-source
I recently discovered that 2 == [2] in JavaScript. As it turns out, this
I recently discovered that the java.lang.String.substring method does not return a new string, but
I recently discovered some of the power and easy of use of dired mode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.