Peeter Joot's (OLD) Blog.

Math, physics, perl, and programming obscurity.

Archive for September, 2013

Understanding a powerpc subfc, subfe sequence used to compare without branching.

Posted by peeterjoot on September 25, 2013

I was tasked to review some inline assembly, essentially like so:

Uint64 negativeLessThanX(Uint64 v0, Uint64 v1)
{
   Uint64 mask;
   __asm__ volatile ("subfc %0,%2,%1; subfe %0,%0,%0" \
           /* outputs */ : "=r"(mask) /* %0 */        \
     /* inputs */  : "r"(v0),   /* %1 */        \
                     "r"(v1)    /* %2 */        \
           /* clobbers */: "xer"      /* condition registers (CF, ...) */ \
    );
   return mask;
}

This should have the effect of doing:

Uint64 negativeLessThanX(Uint64 v0, Uint64 v1)
{
   Uint64 mask;

   subfc mask,v1,v0
   subfe mask, mask, mask

   return mask;
}

From the powerpc instruction set reference (PowerISA_V2.07_PUBLIC.pdf), our subfc, and subfe instructions are respectively ‘Subtract From Carrying XO-form’, ‘Subtract From Extended XO-form’ :

subfc RT,RA,RB (OE=0 Rc=0)
RT  <- ¬ (RA) + (RB) + 1 
(RT = RB - RA)

subfe RT,RA,RB (OE=0 Rc=0)
RT  <- ¬ (RA) + (RB) + CA

Since we have RA = RB in the subfe, and self plus complement is all bits set, we essentially have

RT  <- 0xFFFFFFFFFFFFFFFF + CA

Let’s walk through this in the debugger to understand it. We have:

(dbx) listi negativeLessThanX
0x100000a24 (negativeLessThanX(unsigned long,unsigned long)+0x24) 7c030010       subfc   r0,r3,r0
0x100000a28 (negativeLessThanX(unsigned long,unsigned long)+0x28) 7c000110       subfe   r0,r0,r0

So, we have r0 = r0 – r3. After this we have a r0 = r0 – r0, but also bringing in the carry flag (CA bit in XER). Let’s see this in the debugger, first with r0=2, r3=1 :

(dbx) stop in negativeLessThanX
[1] stop in negativeLessThanX(unsigned long,unsigned long)
(dbx) c
[1] stopped in negativeLessThanX(unsigned long,unsigned long) at line 6 in file "w.C" ($t1)
    6      __asm__ volatile ("subfc %0,%2,%1; subfe %0,%0,%0" \
(dbx) stepi
stopped in negativeLessThanX(unsigned long,unsigned long) at 0x100000a20 ($t1)
0x100000a20 (negativeLessThanX(unsigned long,unsigned long)+0x20) e86100b8          ld   r3,0xb8(r1)
(dbx)
stopped in negativeLessThanX(unsigned long,unsigned long) at 0x100000a24 ($t1)
0x100000a24 (negativeLessThanX(unsigned long,unsigned long)+0x24) 7c030010       subfc   r0,r3,r0
(dbx) p $r0
0x0000000000000002
(dbx) p $r3
0x0000000000000001
(dbx) p $xer
0x0000000020000002
(dbx) stepi
stopped in negativeLessThanX(unsigned long,unsigned long) at 0x100000a28 ($t1)
0x100000a28 (negativeLessThanX(unsigned long,unsigned long)+0x28) 7c000110       subfe   r0,r0,r0
(dbx) p $r0
0x0000000000000001
(dbx) p $xer
0x0000000020000002
(dbx) stepi
stopped in negativeLessThanX(unsigned long,unsigned long) at 0x100000a2c ($t1)
0x100000a2c (negativeLessThanX(unsigned long,unsigned long)+0x2c) f8010070         std   r0,0x70(r1)
(dbx) p $r0

We see that the subfc does generate r0=1 as expected. The CA bit of the XER is ’34 Carry (CA)’, and out XER value is: 0b[0….]00100000000000000000000000000010. Bits 32, 33 are clear, but CA (34) is set. At first this seems curiously inverted. We have $r0 = v0-v1 > 0, so why is CA set?

How about with v0=1, v1=2:

(dbx)
stopped in negativeLessThanX(unsigned long,unsigned long) at 0x100000a24 ($t1)
0x100000a24 (negativeLessThanX(unsigned long,unsigned long)+0x24) 7c030010       subfc   r0,r3,r0
(dbx) p $r0
0x0000000000000001
(dbx) p $r3
0x0000000000000002
(dbx) stepi
stopped in negativeLessThanX(unsigned long,unsigned long) at 0x100000a28 ($t1)
0x100000a28 (negativeLessThanX(unsigned long,unsigned long)+0x28) 7c000110       subfe   r0,r0,r0
(dbx) p $r0
0xffffffffffffffff
(dbx) p $xer
0x0000000000000013
(dbx) stepi
stopped in negativeLessThanX(unsigned long,unsigned long) at 0x100000a2c ($t1)
0x100000a2c (negativeLessThanX(unsigned long,unsigned long)+0x2c) f8010070         std   r0,0x70(r1)
(dbx) p $r0
0xffffffffffffffff

The intermediate subtraction now produces a -1 (0xffffffffffffffff), but now we have CA clear from the subfc, since we see XER=0b[0….]00000000000000000000000000010011 (with bit 34 clear). Again, this seems backwards.

The trick to understanding this is that the subtract isn’t implemented as a subtraction, but an addition. For 2-1, where we don’t have to borrow, our subfc is actually doing:

~1 + 2 + 1:
 1110
+0010
+0001
=====
10001

No borrow is required, but we do generate a carry when doing this _addition_ operation!

Compare that to the a 1-2 operation:

~2 + 1 + 1:
 1101
+0001
+0001
=====
 1111

Also compare to a 1-1 operation:

~1 + 1 + 1:
 1110
+0001
+0001
=====
10000

We generate a carry (now borrow!) when v0=v1 and v0>v1. We do not generate a carry (CA clear) for v0<v1, so that the end result is that for v0<v1 we have -1, and 0 otherwise.

Advertisements

Posted in C/C++ development and debugging. | Tagged: , , , , , , , | Leave a Comment »

An attempt to understand my reactions to the ambulance chasing reporting of “Democracy Now”

Posted by peeterjoot on September 10, 2013

 

It’s been a while since I’ve listened to the Democracy Now’s podcast.  I like some of the interviews that they give, as well as the historical perspective on certain events.  For example, they’ve had some interesting coverage of the 50th year Martin Luther King speeches and the March on Washington.  I’ve also enjoyed some of their bumper music, such as “Has the bombing begun” by David Rovics, and the Rise Against’s “Hero of War”, a spectacular story of a military recruits transition to an understanding of the reality of warfare.

I often cry a little bit every time I hear Hero of War.  My little brother, now a US marine, has fallen victim to this hero propaganda.  I hope he comes out of his tour in one piece physically and emotionally.  It’s hard to visualize him being forced into a position where he is the one beating and pissing on his “man” or mowing down the girl carrying a dirty makeshift white flag, but I know that is unfortunately in the realm of possibility.  However, I digress.

Unfortunately, before some of the interview covarage that Democracy now does well, one has to put up with or skip over their “war and peace report”.  That is reporting that focuses on just the day to day view, without any sort of big picture illustration, and without providing any sort of context.  I would characterize it as very damaging.  It is easily debatable that this is worse than not reporting in the first place.

I don’t see how one can call Democracy Now alternative media when they do the same sort of sensationalist ambulance chasing that is found in all the mainstream corporate media.  I would characterize this sort of soundbite reporting as both misleading and very damaging.  Inclusion of endless 10-30 second soundbites covering what are essentially traumatic world or domestic events will just harden the hearts of those that listen.  Listening to that is no better than the evening news.  Listening to this sort of reporting makes me involuntarily feel callous.  How else can you respond to a barrage of this sort of reporting?  You are left with the feeling that you can’t do anything about it.  Should you want to have compassion and do something about it, you’d have to quit everything that you are doing and start protesting or organizing.  It would be a life changing event to try to be compassionate under this sort of barrage of crap.

Perhaps that is the goal of sensationalist reporting: Produce an audience that will come back again and again, thinking that it is important to understand what is going on, but at the same time conditioned to do nothing about it.  Produce a zombie automaton audience that is compelled to return for their fix of brains.

Worse than the production of callous behaviour in the listeners is the propaganda aspect of this sort of reporting.  Democracy Now’s war and peace report, unwittingly serves as a propaganda conduit for the warmongers that they channel.  I don’t mind that they include representation of the warmongers, but it needs to be done with commentary.  Otherwise, it essentially gives the listener the impression that it is fact, especially when it is repeated again and again and again.  This is how to mold opinion, and is not how to present a story with intent to inform.  If you are exposed to endless soundbites of Kerry and Obama and other similar gutless warmongers playing out their script, all without any sort of commentary or moderation, then not knowing any better you will start to believe that what they say is factual.  I wasn’t taking explicit notes, but if I had tried to count the number of times that I heard Kerry saying “he had no doubt that Assad was responsible for the chemical weapons attack in east Damascus on 21 August”, I probably would have used up my fingers.

If pressed on this issue, Democracy Now would surely respond that they moderate this barrage of crap with their more in depth interviews.  For example, their interview with Rep. Alan Grayson has an attempt to balance the scales.  Should you listen to that you see Kerry and others called out for their attempts to manipulate the facts, as they attempt to produce a compelling case for US lead bombing for the good of humanity and the world.  They’ve also hosted debates setting pro intervention pundits against those who don’t wish another unjustified war.  There are, however, glaring omissions in coverage that are made obvious by such a debate.  The pro intervention supporter leaves you with the impression that there’s a huge backstory of tyranny by Assad.  I’d like to ask where the specific reporting on that tyranny is.  If Assad is such a bad guy, where is the reporting on the evils that have occurred under his command?  None of that is in the day to day focused mainstream media, nor in Democracy Now’s insipid variety of alternative media.  The only story we get is that of the red line and chemical attacks that serve to justify bombing and eventual invasion.

Posted in Incoherent ramblings | Tagged: , , , , , , , | Leave a Comment »

Air duct cleaning telemarkers. They don’t service tents.

Posted by peeterjoot on September 6, 2013

“Hi, this is Ali from … air duct cleaning services [long pitch].  Do you live in a house that is under 2000 square feet?”

“Yes, I live in a tent.  It is under 2000 square feet.”

“Under 2000 square feet.  Okay.”

“Yes, I live in a tent, it is definitely under 2000 square feet.”

“A tent?” [confused silence]

“Yes, a tent.  It is a nylon structure with a base that you can put a sleeping bag on.” [stated matter of fact in perfect seriousness]

[more confused silence].

“I’m sorry.  Thank you.”

He said something after this that sounded like he may have the phone number mixed up.  Perhaps I’ll get off this calling list.

Posted in Incoherent ramblings | Tagged: , | Leave a Comment »

git checkout atomicity?

Posted by peeterjoot on September 6, 2013

It was mentioned to me that git checkouts could (like CVS) return inconsistent views of a particular checkin (ie. some of the files from one person’s checkin). I’m guessing this is misinformation, but have only limited git experience to back up this believe. My expectation is based on the fact that even the mechanism for checkout runs counter to this possibility, since we checkout a specific commit, not any specific file. For example,

Create a repo

$ mkdir dummy
$ cd dummy
$ git init
Initialized empty Git repository in /home/myuserid/dummy/.git/

Populate it with some files

$ for i in  a b c d ; do echo $i > $i  d; done
$ git add *
$ git commit -a
$ for i in a b c d ; do echo $i >> $i ; done
$ git commit -a

$ git log a
commit fc1eb954f337e1e7e4ce30b21205f3f1a9542a35
Author: myuserid@mydomain.com <myuserid@mydomain.com>
Date:   Fri Sep 6 13:59:10 2013 -0400

    modified:   a
    modified:   b
    modified:   c
    modified:   d

commit 735e11eae4299d8693c22706a5010283cf4f0506
Author: myuserid@mydomain.com <myuserid@mydomain.com>
Date:   Fri Sep 6 13:58:43 2013 -0400

    new file: a
    new file: b
    new file: c
    new file: d

Now checkout a commit (the older one). See how all the files, not just one (say) of them matches the first commit in this repository

$ git checkout 735e11eae4299d8693c22706a5010283cf4f0506
Note: moving to "735e11eae4299d8693c22706a5010283cf4f0506" which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
  git checkout -b <new_branch_name>
HEAD is now at 735e11e... new file: a new file: b new file: c new file: d
$ for i in a b c d ; do cat $i ; done
a
b
c
d

Suppose we checkout the second commit. Now we get all the files from the second commit. It seems to me that “atomicity” is built into the whole concept of g
it checkout:

$ git checkout fc1eb954f337e1e7e4ce30b21205f3f1a9542a35
$ for i in a b c d ; do cat $i ; done
a
a
b
b
c
c
d
d

I’d like to know if it’s possible to label a commit with a more meaningful name than these commit strings that are much like ascii barf.

Posted in Development environment | Tagged: , , , | 2 Comments »

Some links on the Syria war machine effort

Posted by peeterjoot on September 5, 2013

Here’s a few links that I found interesting.

And specifically on Britiam:

Just for fun:

Additions, Sept 6:

Addition: Sept 11:

Posted in Incoherent ramblings | Tagged: | Leave a Comment »

Final version of my phy452.pdf notes posted

Posted by peeterjoot on September 5, 2013

I’d intended to rework the exam problems over the summer and make that the last update to my stat mech notes. However, I ended up studying world events and some other non-mainstream ideas intensively over the summer, and never got around to that final update.

Since I’m starting a new course (condensed matter) soon, I’ll end up having to focus on that, and have now posted a final version of my notes as is.

Since the last update the following additions were made

September 05, 2013 Large volume fermi gas density

May 30, 2013 Bernoulli polynomials and numbers and Euler-MacLauren summation

May 09, 2013 Bose gas specific heat above condensation temperature

May 09, 2013 A dumb expansion of the Fermi-Dirac grand partition function

April 30, 2013 Ultra relativistic spin zero condensation temperature

April 30, 2013 Summary of statistical mechanics relations and helpful formulas

April 24, 2013 Low temperature Fermi gas chemical potential

Posted in Math and Physics Learning. | Tagged: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a Comment »

Large volume Fermi gas density

Posted by peeterjoot on September 5, 2013

[Click here for a PDF of this post with nicer formatting]

Here’s part of a problem from our final exam. I’d intended to redo the whole exam over the summer, but focused my summer study on world events instead. Perhaps I’ll end up eventually doing this, but for now I’ll just post this first part.

Question: Large volume Fermi gas density (2013 final exam pr 1)

Write down the expression for the grand canonical partition function Z_{\mathrm{G}} of an ideal three-dimensional Fermi gas with atoms having mass m at a temperature T and a chemical potential \mu (or equivalently a fugacity z = e^{\beta \mu}). Consider the high temperature “classical limit” of this ideal gas, where z \ll 1 and one gets an effective Boltzmann distribution, and obtain the equation for the density of the particles

\begin{aligned}n = \frac{1}{{V \beta}} \frac{\partial {\ln Z_{\mathrm{G}}}}{\partial {\mu}}\end{aligned} \hspace{\stretch{1}}(1.1)

by converting momentum sums into integrals. Invert this relationship to find the chemical potential \mu as a function of the density n.

Hint: In the limit of a large volume V:

\begin{aligned}\sum_\mathbf{k} \rightarrow V \int \frac{d^3 \mathbf{k}}{(2 \pi)^3}.\end{aligned} \hspace{\stretch{1}}(1.2)

Answer

Since it was specified incorrectly in the original problem, let’s start off by verifing the expression for the number of particles (and hence the number density)

\begin{aligned}\frac{1}{{\beta}} \frac{\partial {}}{\partial {\mu}} \ln Z_{\mathrm{G}} &= \frac{1}{{\beta}} \frac{\partial {}}{\partial {\mu}} \ln \sum_N z^N e^{-\beta E_N} \\ &= \frac{1}{{\beta}} \frac{1}{{\Omega}}\frac{\partial {}}{\partial {\mu}} \sum_N z^N e^{-\beta E_N} \\ &= \frac{1}{{\beta}} \frac{1}{{\Omega}}\sum_N e^{-\beta E_N} \frac{\partial {}}{\partial {\mu}} e^{\mu \beta N} \\ &= \frac{1}{{\Omega}}\sum_N N z^N e^{-\beta E_N} \\ &= \left\langle{{N}}\right\rangle.\end{aligned} \hspace{\stretch{1}}(1.3)

Moving on to the problem, we’ve seen that the Fermion grand canonical partition function can be written

\begin{aligned}Z_{\mathrm{G}} = \prod_\epsilon \left( { 1 + z e^{-\beta \epsilon}} \right),\end{aligned} \hspace{\stretch{1}}(1.4)

so that our density is

\begin{aligned}n &= \frac{N}{V} \\ &= \frac{1}{{ V \beta }}\frac{\partial {}}{\partial {\mu}} \ln \prod_\epsilon \left( { 1 + z e^{-\beta \epsilon}} \right) \\ &= \frac{1}{{ V \beta }}\sum_\epsilon \frac{\partial {}}{\partial {\mu}} \ln \left( { 1 + z e^{-\beta \epsilon}} \right) \\ &= \frac{1}{{ V \beta }}\sum_\epsilon \frac{\partial {z}}{\partial {\mu}}\frac{e^{-\beta \epsilon}}{1 + z e^{-\beta \epsilon}} \\ &= \frac{1}{{ V }}\sum_\epsilon z\frac{e^{-\beta \epsilon}}{1 + z e^{-\beta \epsilon}}.\end{aligned} \hspace{\stretch{1}}(1.5)

In the high temperature classical limit, where z \ll 1 we have

\begin{aligned}n \\ &\approx \frac{1}{ V } \sum_\epsilon z e^{-\beta \epsilon} \\ &\approx z \int \frac{d^3 \mathbf{k}}{(2 \pi)^3}e^{-\beta \epsilon} \\ &= \frac{2 z}{(2 \pi)^2} \int_0^\infty k^2 dk e^{-\beta \frac{\hbar^2 k^2}{2m} } \\ &= \frac{z}{2 \pi^2} \left( { \frac{\beta \hbar^2}{2m} } \right)^{-3/2} \int_0^\infty x^2 e^{-x^2} dx \\ &= \frac{z}{2 \pi^2} \left( { \frac{2m}{\beta \hbar^2} } \right)^{3/2} \frac{\sqrt{\pi}}{4} \\ &= \frac{z}{8 \pi^{3/2}} \frac{\left( {8 \pi^2 m k_{\mathrm{B}} T } \right)^{3/2}}{h^3} \\ &= z \frac{\left( { 2 \pi m k_{\mathrm{B}} T } \right)^{3/2}}{h^3}.\end{aligned} \hspace{\stretch{1}}(1.6)

This is

\begin{aligned}\boxed{n = \frac{z}{\lambda^3},}\end{aligned} \hspace{\stretch{1}}(1.7)

where

\begin{aligned}\lambda = \frac{h}{\sqrt{2 \pi m k_{\mathrm{B}} T}}.\end{aligned} \hspace{\stretch{1}}(1.8)

Inverting for \mu we have

\begin{aligned}\mu = \frac{1}{\beta} \ln z\end{aligned} \hspace{\stretch{1}}(1.10)

or

\begin{aligned}\boxed{\mu = k_{\mathrm{B}} T \ln \left( { n \lambda^3 } \right).}\end{aligned} \hspace{\stretch{1}}(1.10)

Posted in Math and Physics Learning. | Tagged: , , , , , , , | Leave a Comment »