Solutions
A
Consider a C implementation of the count leading zero function for 64-bit integers. A leading zero is defined as any '0' digit that appears before the first non-zero digit in the binary representation of a number.
Examples:
Explanation: As Binary = (00000000000000000000000000000000 00000000000000000000000000010000)
Explanation: As Binary =(00000000000000000000000000000000 00000000000000000000000000100001)
The implementation utilizes population count, which counts the number of set bits in the given value.
The source code is listed below, and you shall fill in parts A01 and A02.
Obviously, the above C code listing was incomplete, you shall provide the functioned implementations. A01 and A02 are hexadecimal literals in C. You must obey the following rules when filling them:
You can read the content of Population count and find the section titled "The PopCount routine," then select the constants.
A01 = ?
0x5555555555555555
A02 = ?
0x3333333333333333
B
Consider a C program which converts single precision floating point values to the corresponding bfloat16 floating-point format.
Single-precision (FP32) | bfloat16 as HEX literals |
---|---|
1.200000 | 0x3f99999a |
1.203125 | 0x3f9a0000 |
2.310000 | 0x4013d70a |
2.312500 | 0x40140000 |
3.460000 | 0x405d70a4 |
3.453125 | 0x405d0000 |
5.630000 | 0x40b428f6 |
The source code is listed below, and you shall fill in parts B01 and B02.
Obviously, the above C code listing was incomplete, you shall provide the functioned implementations. B01 and B02 are hexadecimal literals in C. You must obey the following rules when filling them:
Reference: Converting float to int in C
- B01 = ?
0x7F800000- B02 = ?
0x100
C
Assuming that special values such as NaN and INF do not appear during calculations, the following C code attempts to implement single-precision floating-point multiplication in a minimal way. There is also no overflow.
Obviously, the above C code listing was incomplete, you shall provide the functioned implementations. C01, C02, C03, and C04 are decimal integer literals in C. You must obey the following rules when filling them:
Reference: Floating-Point Numbers
- C01 = ?
24- C02 = ?
127- C03 = ?
31- C04 = ?
23
D
Let us endeavor to ascertain endianness at compile time. When the need arises for compile-time endianness determination, it typically falls into one of two distinct use cases:
In most prevalent use cases, the objective is to facilitate the conversion between little-endian and big-endian formats, as well as potentially to and from the host endianness. For this purpose, we shall introduce endian conversion functions, which shall be denoted by the end_
prefix.
You shall provide the functioned implementations. Both D01
and D02
are hexadecimal integer literals, meaning that they should start with 0x
. D03
, D04
, D05
, and D06
are C expressions. You might consider to call end_bswap32
function when it is necessary. You must obey the following rules when filling them:
end_bswap32(n)
to end_bswap32( n )
. Be aware of the spaces! Details determine success or failure.
- D01 = ?
0xff00
- D02 = ?
0xff0000
- D03 = ?
end_bswap32(n)
- D04 = ?
n
- D05 = ?
n
- D06 = ?
end_bswap32(n)
E
Arithmetic overflow manifests when the outcome of a computation cannot be adequately represented within the constraints of the prevailing encoding scheme, consequently falling beyond the range of representable values, thus yielding an erroneous result.
Distinct categories of overflow can be delineated as follows:
c
(answer in HEX) such that c + 0x80
causes NEITHER signed nor unsigned overflow in 8 bits.
- E01 = ?
0x7F
Unsigned overflow will occur forc > 0x80
. Signed overflow can only happen if c is negative (also> 0x80
). Largest is therefore,0x7F
c
(answer in HEX) such that c + 0x71
causes signed overflow, but NOT unsigned overflow in 8 bits.
- E02 = ?
0xF
For signed overflow, need(+) + (+) = (−)
. For no unsigned overflow, need no carryout from MSB. The first(−)
encoding we can reach from0x71
is0x80
.0x80 – 0x71 = 0xF
.
F
The subsequent function, denoted as absf
, yields the absolute value of a single-precision floating-point number. What is the hexadecimal literal representation of the value denoted as F01?
- F01 = ?
0x7fffffff