• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Weird "case" statement behavior (at least to me...), can...

Stats

  • Locked Locked
  • Replies 5
  • Subscribers 143
  • Views 7489
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Weird "case" statement behavior (at least to me...), can someone please explain this to me?

Sheppy
Sheppy over 10 years ago

Hello all,

Today I ran into a problem which toke me a long time to find the cause. It is with the "case" statement. First I show you something that is very simple and that does work:


testString = "one"
case( testString
    ( "one"
        printf("it is 1!\n")
    )
    ( "two"
        printf("it is 2!\n")
    )
    ( t
        printf("it is something else!\n")
    )
)

testString = "two"
case( testString
    ( "one"
        printf("it is 1!\n")
    )
    ( "two"
        printf("it is 2!\n")
    )
    ( t
        printf("it is something else!\n")
    )
)

testString = "bla"
case( testString
    ( "one"
        printf("it is 1!\n")
    )
    ( "two"
        printf("it is 2!\n")
    )
    ( t
        printf("it is something else!\n")
    )
)


When you run this code (just copy-past into CIW...) the output of the case statements is as you would expect:

it is 1!
it is 2!
it is something else!

However, if you specify a variable like so:


testOptions = list( "one" "two" )


And replace the "case" statement with this:


case( testString
    ( nthelem( 1 testOptions )
        printf("it is 1!\n")
    )
    ( nthelem( 2 testOptions )
        printf("it is 2!\n")
    )
    ( t
        printf("it is something else!\n")
    )
)


The output is not what I expected:

it is something else!
it is something else!
it is something else!

Testing the "nthelem" part shows no problem, it perfectly outputs the right string (or whatever you put in the list).

If you do the same with a "cond" statement (using the "nthelem" statement), it works perfectly fine.

My question: what is happening here? Why is the result what it is, and not what I was expecting?

Thanks in advance.

With kind regards,

Sjoerd

  • Cancel
  • Andrew Beckett
    Andrew Beckett over 10 years ago

    Sjoerd,

    That's expected. The target values (the first entry on each branch in the case function) are not evaluated and have to be literal values. So they can be a literal integer, floating point number (not a good idea though, because this invokes floating point equality which is dangerous due to potential rounding errors), strings and symbols. You can also specify multiple values using:

    case(testString
      (("one" "two") printf("1 or 2\n"))
      (("three" "four") printf("3 or 4\n"))
      (t printf("something else\n"))
    )

    So actually when you used the nthelem, a branch will look like this: ((nthelem 1 testOptions) printf("...")) - note that the SKILL parser converts the C-like syntax into LISP-like form internally. So that means it would match 'nthelem, 1 or 'testOptions. You can try out the second form by using testString='nthelem or testString=1 or testString=2 and see what happens!

    If you want the target values to be an evaluated expression, you should use cold() instead.

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Sheppy
    Sheppy over 10 years ago

    Hi Andrew,

    Thanks for the explanation. Is it made this way for speed, or is there an other reason to implement it like this? If it is not evaluating, it is obviously much faster.

    With kind regards,

    Sjoerd

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 10 years ago

    Hi Sjoerd,

    The reason is not for speed. There are a few reasons:

    1. Since you are commonly comparing against literal values, if the expression is evaluated, you'd have to quote (with "'") any symbols in the target expression
    2. The form allows you to have multiple match values in a list (without the list needing to be quoted or using the list() function). This wouldn't be possible if the expression was evaluated - because then you'd probably expect it to be able to match a list. Other languages have the idea of a switch/case statement which can fall through to another (which causes no end of trouble in those languages because of accidentally omitting "break"), but we don't have that here - you have to explicitly list all target matches in one place. Without the multiple-target form, you'd have to repeat the clause multiple times with different targets.
    3. Most other languages only compare literal values (in C you can't compare strings, only ints, floats and enums).
    4. You can easily have the evaluated form using cond() instead...

    In fact you can always write a macro to do what you want. For example:

    (defmacro CCFcaseEval (match @rest clauses)
      `(cond
         ,@(foreach mapcar clause clauses
                    (if (eq (car clause) t)
                      clause
                      `((equal ,match ,(car clause)) ,@(cdr clause))))))

    With this, you can use your code:

    CCFcaseEval( testString
        ( nthelem( 1 testOptions )
            printf("it is 1!\n")
        )
        ( nthelem( 2 testOptions )
            printf("it is 2!\n")
        )
        ( t
            printf("it is something else!\n")
        )
    )

    and it will then do what you want. If you can understand the slight gobbledegook of the macro, you will see that it actually transforms into a cond() statement:

    expandMacro('CCFcaseEval( testString
        ( nthelem( 1 testOptions )
            printf("it is 1!\n")
        )
        ( nthelem( 2 testOptions )
            printf("it is 2!\n")
        )
        ( t
            printf("it is something else!\n")
        )
      )
    )

    This produces:

    cond(
        ((testString == nthelem(1 testOptions))
            printf("it is 1!\n")
        )
        ((testString == nthelem(2 testOptions))
            printf("it is 2!\n")
        )
        (t
            printf("it is something else!\n")
        )
    )

    You probably wish you hadn't asked now!

    Kind Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Sheppy
    Sheppy over 10 years ago

    Hi Andrew,

    Thanks for the very detailed answer, and for the code I most likely will never use ;-).
    I definitely couldn't have come up with this myself. I'll do my best to find an other question that teases you...

    Best regards,

    Sjoerd

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 10 years ago

    Sjoerd,

    Actually I'd rather hope that  you don't use the code! My intention was to explain and illustrate rather than have you use the macro - and to show that lots of things are possible in SKILL if you want.

    There's a great quotation from Peter Norvig (now at Google) in his book Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp:

    "The first step in writing a macro is to recognize that every time you write one, you are defining a new language that is just like Lisp except for your new macro. The programmer who thinks that way will rightfully be extremely frugal in defining macros. Introducing a macro puts much more memory strain on the reader of your program than does introducing a function, variable or data type, so it should not be taken lightly. Introduce macros only when there is a clear need, and when the macro fits in well with your existing system."

    To me this is an example of where using cond() is much more expressive than hiding it via the CCFcaseEval - and is much clearer to the reader what is going on.

    Anyway, feel free find something else to tease me with!

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information