File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -167,13 +167,8 @@ public long ReadInt64()
167167
168168 public float ReadHalfFloat ( )
169169 {
170- ushort raw = ReadUInt16 ( ) ;
171-
172- // Swap if stream endianness != host endianness
173- if ( _bigEndian == BitConverter . IsLittleEndian )
174- raw = ( ushort ) ( ( raw << 8 ) | ( raw >> 8 ) ) ;
175-
176- return ( float ) BitConverter . UInt16BitsToHalf ( raw ) ;
170+ ushort bits = ReadUInt16 ( ) ;
171+ return ( float ) BitConverter . UInt16BitsToHalf ( bits ) ;
177172 }
178173
179174 /// <summary>
Original file line number Diff line number Diff line change @@ -200,23 +200,16 @@ public void WriteInt64(long value)
200200 /// <exception cref="IOException">Thrown if there is an issue writing to the stream.</exception>
201201 public void WriteHalfFloat ( float value )
202202 {
203- ushort bits = BitConverter . HalfToUInt16Bits ( ( Half ) value ) ;
204-
205- // Swap if stream endianness != host endianness
206- if ( _bigEndian == BitConverter . IsLittleEndian )
207- bits = ( ushort ) ( ( bits << 8 ) | ( bits >> 8 ) ) ;
208-
209- // write the low and high bits
210- byte lo = ( byte ) ( bits & 0xFF ) ;
211- byte hi = ( byte ) ( bits >> 8 ) ;
212- try
203+ Half half = ( Half ) value ;
204+ if ( _bigEndian )
213205 {
214- _stream . WriteByte ( lo ) ;
215- _stream . WriteByte ( hi ) ;
206+ byte [ ] bytes = BitConverter . GetBytes ( half ) ;
207+ _stream . Write ( bytes . Reverse ( ) . ToArray ( ) , 0 , bytes . Length ) ;
216208 }
217- catch ( Exception e )
209+ else
218210 {
219- throw new IOException ( "An IO exception occurred during write" , e ) ;
211+ byte [ ] bytes = BitConverter . GetBytes ( half ) ;
212+ _stream . Write ( bytes , 0 , bytes . Length ) ;
220213 }
221214 }
222215
You can’t perform that action at this time.
0 commit comments