Unnecessary duplication of work

Marcin 'Qrczak' Kowalczyk qrczak@knm.org.pl
18 Mar 2001 21:10:38 GMT


Consider the following definition.

-------------------------------------------------------------------------
module Cast where

import CTypes
import Char
import Word

cast:: CChar -> Char
cast ch = chr (fromIntegral (fromIntegral ch :: Word8))
-------------------------------------------------------------------------

If chr is replaced by PrelBase.unsafeChr, it generates optimal code
given ghc's primops (at last!). Manuel: this (with unsafeChr) is the
preferred implementation of castCCharToChar for c2hs under ghc >= 4.11.

But with the above, when chr does bounds checking, the int2Word# ->
wordToWord8# -> word2Int# computation is unnecessarily done three
times. I think it would be good if such cases were improved:

==================== STG syntax: ====================
srt: (0,1)
Cast.cast =
    \r [ch]
	case ch of wild {
	  PrelInt.I8# x# ->
	      case int2Word# [x#] of sat_s47A {
		DEFAULT ->
		    case wordToWord8# [sat_s47A] of sat_s47D {
		      DEFAULT ->
			  case word2Int# [sat_s47D] of sat_s47G {
			    DEFAULT ->
				case >=# [sat_s47G 0] of wild1 {
				  PrelBase.False -> PrelErr.error PrelBase.lvl;
				  PrelBase.True ->
				      case int2Word# [x#] of sat_s47L {
					DEFAULT ->
					    case wordToWord8# [sat_s47L] of sat_s47O {
					      DEFAULT ->
						  case word2Int# [sat_s47O] of sat_s47R {
						    DEFAULT ->
							case <=# [sat_s47R 1114111] of wild2 {
							  PrelBase.True ->
							      case int2Word# [x#] of sat_s47W {
								DEFAULT ->
								    case
									wordToWord8# [sat_s47W]
								    of
								    sat_s47Z
								    { DEFAULT ->
									  case
									      word2Int# [sat_s47Z]
									  of
									  sat_s482
									  { DEFAULT ->
										case
										    chr# [sat_s482]
										of
										a
										{ DEFAULT ->
										      PrelBase.C# [a]
										}
									  }
								    }
							      };
							  PrelBase.False ->
							      PrelErr.error PrelBase.lvl;
							}
						  }
					    }
				      };
				}
			  }
		    }
	      };
	};
SRT: [PrelBase.lvl]

-- 
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK