@@ -502,6 +502,74 @@ public void WriteFloat(void* statePtr, float value)
502502 }
503503 }
504504
505+ internal PrimitiveValue FloatToPrimitiveValue ( float value )
506+ {
507+ if ( format == FormatFloat )
508+ {
509+ Debug . Assert ( sizeInBits == 32 , "FLT state must have sizeInBits=32" ) ;
510+ Debug. Assert ( bitOffset == 0 , "FLT state must be byte-aligned" ) ;
511+ return value;
512+ }
513+ else if ( format = = FormatBit )
514+ {
515+ if ( sizeInBits == 1 )
516+ {
517+ return value >= 0.5f ;
518+ }
519+ else
520+ {
521+ var maxValue = ( 1 << ( int ) sizeInBits ) - 1 ;
522+ return ( int ) ( value * maxValue ) ;
523+ }
524+ }
525+ else if ( format == FormatInt )
526+ {
527+ Debug. Assert ( sizeInBits == 32 , "INT state must have sizeInBits=32" ) ;
528+ Debug. Assert ( bitOffset == 0 , "INT state must be byte-aligned" ) ;
529+ return ( int ) ( value * 2147483647.0f ) ;
530+ }
531+ else if ( format == FormatUInt )
532+ {
533+ Debug. Assert ( sizeInBits == 32 , "UINT state must have sizeInBits=32" ) ;
534+ Debug. Assert ( bitOffset == 0 , "UINT state must be byte-aligned" ) ;
535+ return ( uint ) ( value * 4294967295.0f ) ;
536+ }
537+ else if ( format == FormatShort )
538+ {
539+ Debug. Assert ( sizeInBits == 16 , "SHRT state must have sizeInBits=16" ) ;
540+ Debug. Assert ( bitOffset == 0 , "SHRT state must be byte-aligned" ) ;
541+ return ( short ) ( value * 32768.0f ) ;
542+ }
543+ else if ( format == FormatUShort )
544+ {
545+ Debug. Assert ( sizeInBits == 16 , "USHT state must have sizeInBits=16" ) ;
546+ Debug. Assert ( bitOffset == 0 , "USHT state must be byte-aligned" ) ;
547+ return ( ushort ) ( value * 65535.0f ) ;
548+ }
549+ else if ( format == FormatByte )
550+ {
551+ Debug. Assert ( sizeInBits == 8 , "BYTE state must have sizeInBits=8" ) ;
552+ Debug. Assert ( bitOffset == 0 , "BYTE state must be byte-aligned" ) ;
553+ return ( byte ) ( value * 255.0f ) ;
554+ }
555+ else if ( format == FormatSByte )
556+ {
557+ Debug. Assert ( sizeInBits == 8 , "SBYT state must have sizeInBits=8" ) ;
558+ Debug. Assert ( bitOffset == 0 , "SBYT state must be byte-aligned" ) ;
559+ return ( sbyte ) ( value * 128.0f ) ;
560+ }
561+ else if ( format == FormatDouble )
562+ {
563+ Debug. Assert ( sizeInBits == 64 , "DBL state must have sizeInBits=64" ) ;
564+ Debug. Assert ( bitOffset == 0 , "DBL state must be byte-aligned" ) ;
565+ return value;
566+ }
567+ else
568+ {
569+ throw new Exception( $ "State format '{ format } ' is not supported as floating-point format") ;
570+ }
571+ }
572+
505573 ////REVIEW: This is some bad code duplication here between Read/WriteFloat&Double but given that there's no
506574 //// way to use a type argument here, not sure how to get rid of it.
507575
0 commit comments