Campus Ambassador Coordinator Around the Sun

Monday Oct 06, 2008

Over the weekend I saw what I considered to be an interesting e-mail on one of the various mail alias I am subscribed to. This e-mail was basically titled 'Error in the Java compiler?' and said the following...

Hallo all,

Why do different results come out with the program 2? From my point of
view A and b must agree. The lines are semantically alike. The operator
++ binds more strongly than +

public class Main {
    public static void main(String[] args) {

        int b, a;

        b = 2;
        b = ++b+b;

        a = 2;
        a = a+++a;

        System.out.println("b: " + b);
        System.out.println("a: " + a);

    }
}

Output:
run:
b: 6
a: 5

The answer to me was quite obvious. The user simply hadn't realised that a++ and ++a are different in so far as a++ will use the current value of a for whatever it is currently doing, then it will add 1 to a. ++a on the other hand will add 1 to a and then use the new value of a.

        2+1 + 3
++b+b = b+1 + b = 6


        2   + 3
a+++a = a++ + a = 5

However, it then turns out that the example provided wasn't really appropriate and as such the user sent a new e-mail, this time with the following example:

This was a bad example from me...

int b, a;

b = 2;
b = ++b*b; // this represent (2+1)*3 that's OK

a = 2;
a = a*++a; // ++a is 3, then is the first a also 3... but this a is 2...

System.out.println("b: " + b);
System.out.println("a: " + a);

Output:
run:
b: 9
a: 6

Naturally this again was not an error in the Java compiler. It was due to the Java specification which says that all expressions are evaluated from left to right. As such, in the second example we evaluate a to be 2 first, and then we times that by ++a, resulting in 6...

2+1 * 3
b = ++b * b = 9

    2 * 2+1
a = a * ++a = 6

The most interesting thing about this whole subject is that if you were to write that kind of code in C it would be completely undefined in terms of what the results would be. The compiler writers may do their best to come up with sensible solutions, but some times you'll get 9, other times you'll get 6 and for that matter some times you could get -99,938 or even 2,394,343 etc.

So - what is the moral of all this - don't write code like that. Do it in two separate statements. That way the code is clearer and the outcome is more predictable.


Sunday Oct 05, 2008

So I was really annoyed that my Mum's Elonex Webbook with Windows XP on was doing CPU frequency scaling out of the box and so was getting battery life nearer 4 hours than the 2 my Ubuntu Webbook was achieving.

I spent a couple of hours this evening searching for ways to get CPU frequency scaling working, and at last I've managed to find the information I was looking for. As such I thought I'd share it with you.

Download, extract and compile the following source code:

$ wget http://www.a110wiki.de/wiki/images/6/65/Cpufreq-2.6.25_backport.tar.bz2
$ tar xfvj Cpufreq-2.6.25_backport.tar.bz2
$ cd cpufreq
$ make

Now you need to create the following directory:

$ sudo mkdir /lib/modules/`uname -r`/cpu

Then copy the compiled kernel module into this direcotry...

$ sudo cp e_powersaver.ko /lib/modules/`uname -r`/cpu/

You're nearly good to go....

$ depmod -ae
$ modprobe e_powersaver

You will now have CPU frequency scaling support. You can add 'e_powersaver' to your /etc/modules file so that it loads automatically every time your computer starts.

Other tools that are useful include:

$ sudo apt-get install cpufrequtils

You can now do things like this:

 $ /usr/bin/cpufreq-info
cpufrequtils 002: cpufreq-info (C) Dominik Brodowski 2004-2006
Report errors and bugs to linux@brodo.de, please.
analyzing CPU 0:
  driver: e_powersaver
  CPUs which need to switch frequency at the same time: 0
  hardware limits: 399 MHz - 1.60 GHz
  available frequency steps: 399 MHz, 499 MHz, 599 MHz, 698 MHz, 798 MHz, 898 MHz, 998 MHz, 1.10 GHz, 1.20 GHz, 1.30 GHz, 1.40 GHz, 1.50 GHz, 1.60 GHz
  available cpufreq governors: ondemand, conservative, userspace, powersave, performance
  current policy: frequency should be within 399 MHz and 1.60 GHz.
                  The governor "ondemand" may decide which speed to use
                  within this range.
  current CPU frequency is 1.60 GHz.

And to change the power modes etc you can do this:

$ sudo cpufreq-set -g powersave
$ sudo cpufreq-set -g conservative

So, I hope this is of some help to someone. I should point out that the original source of this information was http://www.a110wiki.de/wiki/CPU.