From f6b9c1af956f95bac77de2bc5a1b772d7283805d Mon Sep 17 00:00:00 2001 From: Jubilee <46493976+workingjubilee@users.noreply.github.com> Date: Fri, 17 Sep 2021 16:24:59 -0700 Subject: [PATCH 1/2] Document MSVC doesn't straddle bit-fields --- docs/c-language/c-bit-fields.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/c-language/c-bit-fields.md b/docs/c-language/c-bit-fields.md index 470e6851ae2..db2bcf41a93 100644 --- a/docs/c-language/c-bit-fields.md +++ b/docs/c-language/c-bit-fields.md @@ -73,6 +73,18 @@ cccccccb bbbbaaaa Since the 8086 family of processors stores the low byte of integer values before the high byte, the integer `0x01F2` above would be stored in physical memory as `0xF2` followed by `0x01`. +According to the ISO/IEC 9899:1999 standard for C, also known as C99, an implementation may choose whether a bit field "straddles" two storage instances. Consider the following structure: +``` +struct +{ + unsigned int first : 9; + unsigned int may_straddle : 30; + unsigned int last : 25; +} tricky_bits; +``` + +For an application binary interface where **`unsigned int`** is a 32-bit integer, a standard C implementation could decide to maximally pack these bit fields into two 32-bit integers, even though that would store `tricky_bits.may_straddle` as 23 bits in one 32-bit integer and 7 bits in the next 32-bit integer. For Windows-supported architectures, including 32-bit x86, 64-bit x86, ARM, and ARM64, the ABI convention is to place a bit field into a single storage integer. The Microsoft C/C++ Optimizing Compiler included with the Visual C++ toolset (MSVC) follows this convention, and will store each bit field in the above example in its own 32-bit integer, selecting three 32-bit integers. Using the `sizeof` operator on an instance of `tricky_bits` will return `12`. + **END Microsoft Specific** ## See also From 6649162e30701459fedf6e8bfeca8dbe04772b38 Mon Sep 17 00:00:00 2001 From: Colin Robertson Date: Mon, 20 Sep 2021 13:01:19 -0700 Subject: [PATCH 2/2] Simplify for i18n, grammar checker --- docs/c-language/c-bit-fields.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/c-language/c-bit-fields.md b/docs/c-language/c-bit-fields.md index db2bcf41a93..489273c13b8 100644 --- a/docs/c-language/c-bit-fields.md +++ b/docs/c-language/c-bit-fields.md @@ -73,17 +73,19 @@ cccccccb bbbbaaaa Since the 8086 family of processors stores the low byte of integer values before the high byte, the integer `0x01F2` above would be stored in physical memory as `0xF2` followed by `0x01`. -According to the ISO/IEC 9899:1999 standard for C, also known as C99, an implementation may choose whether a bit field "straddles" two storage instances. Consider the following structure: -``` +The ISO C99 standard lets an implementation choose whether a bit field may straddle two storage instances. Consider this structure, which stores four bit fields that total 64 bits: + +```C struct { unsigned int first : 9; + unsigned int second : 7; unsigned int may_straddle : 30; - unsigned int last : 25; + unsigned int last : 18; } tricky_bits; ``` -For an application binary interface where **`unsigned int`** is a 32-bit integer, a standard C implementation could decide to maximally pack these bit fields into two 32-bit integers, even though that would store `tricky_bits.may_straddle` as 23 bits in one 32-bit integer and 7 bits in the next 32-bit integer. For Windows-supported architectures, including 32-bit x86, 64-bit x86, ARM, and ARM64, the ABI convention is to place a bit field into a single storage integer. The Microsoft C/C++ Optimizing Compiler included with the Visual C++ toolset (MSVC) follows this convention, and will store each bit field in the above example in its own 32-bit integer, selecting three 32-bit integers. Using the `sizeof` operator on an instance of `tricky_bits` will return `12`. +A standard C implementation could pack these bit fields into two 32-bit integers. It might store `tricky_bits.may_straddle` as 16 bits in one 32-bit integer and 14 bits in the next 32-bit integer. The Windows ABI convention packs bit fields into single storage integers, and doesn't straddle storage units. The Microsoft compiler stores each bit field in the above example so it fits completely in a single 32-bit integer. In this case, `first` and `second` are stored in one integer, `may_straddle` is stored in a second integer, and `last` is stored in a third integer. The `sizeof` operator returns `12` on an instance of `tricky_bits`. For more information, see [Padding and alignment of structure members](padding-and-alignment-of-structure-members.md). **END Microsoft Specific**